Skip to content

Commit 8363f62

Browse files
committed
update chapter8 part1: user program
use builtin target. update Makefile for user.
1 parent 117a549 commit 8363f62

File tree

4 files changed

+23
-53
lines changed

4 files changed

+23
-53
lines changed

chapter8/part1.md

+15-45
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@
1515

1616
所以我们的用户程序基本还是要使用前两章的方法,不同的则是要把系统调用加入进去。
1717

18-
``os/usr`` 目录下使用 Cargo 新建一个二进制项目
18+
``usr`` 目录下使用 Cargo 新建一个二进制项目,然后给文件夹改个名字:
1919

2020
```bash
21-
$ cargo new rust --bin --edition 2018
21+
$ cargo new user --bin
22+
$ mv user rust
2223
```
2324

2425
我们先来看系统调用:
@@ -198,7 +199,7 @@ use buddy_system_allocator::LockedHeap;
198199
static DYNAMIC_ALLOCATOR: LockedHeap = LockedHeap::empty();
199200
```
200201

201-
现在我们可以将每一个含有 ``main`` 函数的 rust 源代码放在 ``usr/rust/src/bin`` 目录下。它们每一个都会被编译成一个独立的可执行文件。
202+
现在我们可以将每一个含有 ``main`` 函数的 Rust 源代码放在 ``usr/rust/src/bin`` 目录下。它们每一个都会被编译成一个独立的可执行文件。
202203

203204
其模板为:
204205

@@ -207,12 +208,11 @@ static DYNAMIC_ALLOCATOR: LockedHeap = LockedHeap::empty();
207208

208209
#![no_std]
209210
#![no_main]
210-
#![feature(alloc)]
211211

212212
extern crate alloc;
213213

214214
#[macro_use]
215-
extern crate rust;
215+
extern crate user;
216216

217217
#[no_mangle]
218218
pub fn main() -> usize {
@@ -229,12 +229,11 @@ pub fn main() -> usize {
229229

230230
#![no_std]
231231
#![no_main]
232-
#![feature(alloc)]
233232

234233
extern crate alloc;
235234

236235
#[macro_use]
237-
extern crate rust;
236+
extern crate user;
238237

239238
#[no_mangle]
240239
pub fn main() -> usize {
@@ -245,48 +244,19 @@ pub fn main() -> usize {
245244
}
246245
```
247246

248-
为了能够编译,我们还需要一个目标三元组,只不过这里,我们不需要再通过链接脚本手动执行内存布局了!
249-
250-
```json
251-
// usr/rust/riscv64-rust.json
252-
253-
{
254-
"llvm-target": "riscv64",
255-
"data-layout": "e-m:e-p:64:64-i64:64-n64-S128",
256-
"target-endian": "little",
257-
"target-pointer-width": "64",
258-
"target-c-int-width": "32",
259-
"os": "none",
260-
"arch": "riscv64",
261-
"cpu": "generic-rv64",
262-
"features": "+m,+a",
263-
"max-atomic-width": "64",
264-
"linker": "rust-lld",
265-
"linker-flavor": "ld.lld",
266-
"executables": true,
267-
"panic-strategy": "abort",
268-
"relocation-model": "static",
269-
"abi-blacklist": [
270-
"cdecl",
271-
"stdcall",
272-
"fastcall",
273-
"vectorcall",
274-
"thiscall",
275-
"aapcs",
276-
"win64",
277-
"sysv64",
278-
"ptx-kernel",
279-
"msp430-interrupt",
280-
"x86-interrupt"
281-
],
282-
"eliminate-frame-pointer": false
283-
}
247+
和内核项目一样,这里也创建一个 `.cargo/config` 文件指定默认的目标三元组。但这次我们就不用自定义链接脚本了,用默认的即可。
248+
249+
```toml
250+
# .cargo/config
251+
252+
[build]
253+
target = "riscv64imac-unknown-none-elf"
284254
```
285255

286256
切换到 ``usr/rust`` 目录,就可以进行交叉编译:
287257

288258
```bash
289-
$ cargo xbuild --target riscv64-rust.json
259+
$ cargo build
290260
```
291261

292-
我们将能够在 ``usr/rust/target/riscv64-rust/debug/hello_world`` 看到我们编译出来的可执行文件,接下来的问题就是如何把它加载到内核中执行了!
262+
我们将能够在 ``usr/rust/target/riscv64imac-unknown-none-elf/debug/hello_world`` 看到我们编译出来的可执行文件,接下来的问题就是如何把它加载到内核中执行了!

chapter9/part1.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
```makefile
1212
# usr/Makefile
1313

14-
target := riscv64
14+
target := riscv64imac-unknown-none-elf
1515
mode := debug
1616
rust_src_dir := rust/src/bin
17-
rust_target_dir := rust/target/$(target)-rust/$(mode)
17+
rust_target_dir := rust/target/$(target)/$(mode)
1818
rust_srcs := $(wildcard $(rust_src_dir)/*.rs)
1919
rust_targets := $(patsubst $(rust_src_dir)/%.rs, $(rust_target_dir)/%, $(rust_srcs))
20-
out_dir := build/$(target)
21-
sfsimg := build/$(target).img
20+
out_dir := build/riscv64
21+
sfsimg := build/riscv64.img
2222
.PHONY: rcore-fs-fuse rust user_img clean
2323

2424

@@ -29,12 +29,12 @@ ifeq ($(shell which rcore-fs-fuse),)
2929
endif
3030

3131
rust:
32-
@cd rust && cargo xbuild --target $(target)-rust.json
32+
@cd rust && cargo build
3333
@echo targets includes $(rust_targets)
3434
@rm -rf $(out_dir)/rust && mkdir -p $(out_dir)/rust
3535
@rm -f $(sfsimg)
3636
@cp $(rust_targets) $(out_dir)/rust
37-
37+
3838
$(sfsimg): rcore-fs-fuse rust
3939
@rcore-fs-fuse --fs sfs $@ $(out_dir) zip
4040

chapter9/part2.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ pub fn getc() -> u8 {
5454
#![no_main]
5555

5656
#[macro_use]
57-
extern crate rust;
57+
extern crate user;
5858

5959
use rust::io::getc;
6060

chapter9/part3.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
extern crate alloc;
1919

2020
#[macro_use]
21-
extern crate rust;
21+
extern crate user;
2222

2323
const LF: u8 = 0x0au8;
2424
const CR: u8 = 0x0du8;

0 commit comments

Comments
 (0)