Compare commits

..

275 Commits

Author SHA1 Message Date
Yu Chen 21bae8b034 add cargo fmt in Makefile, and exec make fmt
3 years ago
Yu Chen a773a977e7 udpate README
3 years ago
chyyuu d52256baa9
Merge pull request #78 from CL8192/forktree-fix
3 years ago
cl8192 7c83dd49e7 wait child exit in forktree
3 years ago
Yu Chen 9a95c7dcb4 update usr apps: usertests.rs cat.rs. now all apps in usertests can run correctly
3 years ago
chyyuu cc10cd10eb
Merge pull request #77 from CL8192/dev
3 years ago
yuoo655 b732da242d initiatively exit child in forktree
3 years ago
Yu Chen ca997784ab add comments for qemu-exit
3 years ago
Yu Chen a85cb98743 add VIRT_TEST support for qemu exit with exit_code, for CI autotest
3 years ago
Yu Chen 31a2873a61 update rustsbi-qemu 'commit id:c1761f2' :: setup PMP addr[base:0x0010_0000, len: 0x0000_2000] for VIRT_TEST/RTC
3 years ago
Yu Chen 1371aab236 add more apps for test
3 years ago
chyyuu e615ae4483
Merge pull request #45 from DeathWish5/main
3 years ago
chyyuu 18d4a89ab7
Merge branch 'main' into main
3 years ago
Yu Chen c234d1417b fix bug in sbi_shutdown
3 years ago
Yu Chen cf00c3ae5d update github CI for autotest
3 years ago
chyyuu 87cd106b67
Merge pull request #75 from CL8192/main
3 years ago
chyyuu 3d5e9ed9e6
Merge branch 'main' into main
3 years ago
chyyuu 2bd092dd64
Merge pull request #74 from YdrMaster/main
3 years ago
yuoo655 07675fcd00 add ci tests update usertests
3 years ago
YdrMaster c9a288c110 ci: cache qemu
3 years ago
Yu Chen 8e73480c99 cargo fmt
3 years ago
Yu Chen 8657b656e5 rust-toolchain nightly-2020-04-11
3 years ago
DeathWish5 1afa429e93 feat: CI run tests
3 years ago
Yifan Wu 964445e59c Fix #71.
3 years ago
Yifan Wu df36cbe657
Merge pull request #69 from wei-huan/main
3 years ago
RunOS 9d2690f8d9 virtaddr -> usize high 256GB addrspace bug fix
3 years ago
chyyuu a8e4c13e99
Merge pull request #65 from cuishuang/main
3 years ago
cuishuang a70a983497 fix some typos
3 years ago
Yifan Wu ca1d7a07b8 Bump Rust to nightly-2022-04-11 && support debugging in release mode
3 years ago
Yu Chen 3b7c4b1cdc update README for debug info of OS
3 years ago
Yu Chen 91c29d0b70 update CI for build-doc
3 years ago
Yu Chen a61d34e2b1 update CI for build api doc
3 years ago
Yu Chen 858334bc02 update CI for build api doc
3 years ago
Yu Chen b6978bf6ec update index.html
3 years ago
Yu Chen aec0a6ebe5 Merge branch 'main' of github.com:rcore-os/rCore-Tutorial-v3 into main
3 years ago
Yu Chen 34e1a3def8 fix typo in CI for build api doc
3 years ago
Yu Chen d6a3de2510 update CI for build api doc
3 years ago
Yu Chen 3a44decf58 add index.html
3 years ago
Yu Chen 923638023d add CI for build-doc
3 years ago
Yu Chen c009012d85 Merge branch 'ch9' into main
3 years ago
Yifan Wu 21d57c1396 use exclusive_session to eliminate some explicit drops.
3 years ago
Yifan Wu 334d868a5c We should disable sie before trapping back to user.
3 years ago
Yifan Wu fbe8e39b38 Still a lot of bugs :(
3 years ago
Yifan Wu 53034d7c33 Add ns16550a
3 years ago
Yifan Wu d3bd19867c Use latest virtio-drivers && add huge_write_mt but it cannot work now
4 years ago
Yifan Wu 6ef566faac IRQ-based VirtIOBlk Access. Plz wait for the virtio-drivers crate to be updated.
4 years ago
Yifan Wu f0f7f6fcaa Update rustsbi-qemu
4 years ago
Yifan Wu 89b9d7c161 Merge recent updates from ch8
4 years ago
Yifan Wu e6b6251979 Update README.md
4 years ago
Yifan Wu 61152471b7 Add boards/ && clippy
4 years ago
Yifan Wu be2ed8fa37 Update README.md
4 years ago
Yifan Wu 6f09af2c0f Support signal mechanism for ch8(only works on signal-thread apps)
4 years ago
Yifan Wu 48bdefe6b8 Update README.md
4 years ago
Yifan Wu 29d6d26644 Fix cat && add count_lines
4 years ago
Yifan Wu 1c55663478 Small Fix && cargo fmt
4 years ago
Yifan Wu 740730e7f7 Merge recent update from ch7 && cargo clippy
4 years ago
Yifan Wu f65451bc72 Update README.md
4 years ago
Yifan Wu e8686526bb Ref asm&global_asm from core::arch.
4 years ago
Yifan Wu 514c110a28 Bump Rust to nightly-2022-01-19
4 years ago
Yifan Wu 40f37501af Maximum concurrent processes from 40/35->30.
4 years ago
Yifan Wu 24e530935f Update .gitignore
4 years ago
Yifan Wu c358424fae Update README.md
4 years ago
Yifan Wu 2ac621972c Merge recent updates from ch7.
4 years ago
Yifan Wu aa104ecd54 Update README.md.
4 years ago
Yifan Wu d81560a492 Update README.md.
4 years ago
Yifan Wu cba8d9e6d8 Now PageTable::unmap calls PageTable::find_pte instead of PageTable::find_pte_create.
4 years ago
Yifan Wu 72d6b38fca Now PageTable::unmap calls PageTable::find_pte instead of PageTable::find_pte_create.
4 years ago
Yifan Wu cf6d905ac4 Kernel cannot dump now.
4 years ago
Yifan Wu abd9d361e4 Bump to rust nightly-2022-01-01, feature global_asm,asm->stable
4 years ago
DeathWish5 045c47e4ef user: add critical test for software-synchronous tests
4 years ago
DeathWish5 97fdd4f2a2 Merge branch 'main' of https://github.com/rcore-os/rCore-Tutorial-v3 into main
4 years ago
DeathWish5 22db3123d8 user: add peterson algorithm and Eisenberg & McGuire algorithm
4 years ago
Yifan Wu f6b210adbe
Update README.md
4 years ago
Yifan Wu 760615095e
Update README.md
4 years ago
Yifan Wu 06243fac76 Bump to Rust nightly 2021-12-15
4 years ago
Yu Chen 713e78ea91 add condvar in kernel and app
4 years ago
Yu Chen 1c0bbf4404 add user app: sync_sem.rs
4 years ago
Yu Chen 90796450fe fix typo of sys_semaphore_create
4 years ago
Yifan Wu 4ad64e83f6 MutexBlocking works correctly.
4 years ago
Yu Chen 3dc6d9c97c add user app: race_adder with arg
4 years ago
Yifan Wu b77b108a28 Update os/Makefile, rm ... -f -> rm -f ...
4 years ago
Yu Chen 1baf177f9e update .gitignore README.md dev-env-info.md
4 years ago
Yu Chen a3f9b5fea9 update .gitignore
4 years ago
Yu Chen 11a389592a update README.md, dev-env-info.md
4 years ago
Yu Chen 7caf43bbbf add setenv.sh
4 years ago
Yu Chen bb5dca2158 update README.md, dev-env-info.md
4 years ago
Yu Chen 3d2909e990 Merge branch 'ch8' into main
4 years ago
Yifan Wu 2041a7c0d4 Now construction of PA/VA only uses 56/39 bits.
4 years ago
Yifan Wu cd6754a7df rust->nightly-2021-10-15,cargo-binutils->0.3.3
4 years ago
Yifan Wu 5753a09366 Implement mpsc using semaphores.
4 years ago
Yifan Wu aedd7f5612 Add a solution of Philosopher dining problem using Mutex with an illustration.
4 years ago
Yifan Wu b851f8d743 Create threads with a argument. See bin/threads_arg.rs
4 years ago
Yifan Wu 8adfc90db9 Implement sleep using blocking & BinaryHeap.
4 years ago
Yifan Wu 7225254d8a Add MutexBlocking.
4 years ago
Yifan Wu b0fad5aca3 Add MutexSpin and several syscalls.
4 years ago
Yifan Wu 43c6b7cf01 Add race_adder_{atomic,loop}.
4 years ago
Yifan Wu 136e26ae6c User base from 0x0->0x10000; user image size limit from 128MB->16MB; trigger race condition on k210
4 years ago
Yifan Wu 24b3c82b8a Stage2: multiple user threads based on uniprocessor, see new added test race_adder and threads.
4 years ago
Yifan Wu 1493ec9459 Stage1 clear! All applications work but now they are based on threads.
4 years ago
Yifan Wu c599a31dd0 Debugging sys_exec :(
4 years ago
Yifan Wu 6693de9611 Update rustc to newest
4 years ago
Yifan Wu 26bbec6320 Working on ch8
4 years ago
Yifan Wu 91a758d657 Rustc->nightly2021-01-30
4 years ago
Yifan Wu 03ea339e58
Update README.md
4 years ago
Yifan Wu 638eb8666a Update rustc && rustsbi-k210
4 years ago
Yifan Wu bf69560f9b Update rustsbi; huge_write writes 1MiB
4 years ago
Chen 940073e9f3
Merge pull request #26 from felixonmars/patch-1
4 years ago
Felix Yan 03151ac124
Correct typos in drivers/block/sdcard.rs
4 years ago
Yu Chen 46900cc9af update to rustc 1.56.0-nightly (08095fc1f 2021-07-26)
4 years ago
Yu Chen e68f261ed6 update to rustc 1.56.0-nightly (08095fc1f 2021-07-26)
4 years ago
Yifan Wu cf9218113f Update 2021-07-21
4 years ago
Yifan Wu 315e61da1a Rm spin::Mutex except for easy-fs & add new test huge_write & flush cache to disk after a write transaction
4 years ago
Yifan Wu 1590923666 Update README.md
4 years ago
Yifan Wu aaed6006aa Update progress 2021-07-19
4 years ago
Yifan Wu 95431917ed Update README.md
4 years ago
Yifan Wu b5f7fb6c45 Update README.md
4 years ago
Yifan Wu f04110e6e3 Update README.md
4 years ago
Yifan Wu a7b981b14d
Update README.md
4 years ago
Yifan Wu 3a6e4e38f2
Update README.md
4 years ago
Yifan Wu 81559f70b9 Merge branch 'dev' into main
4 years ago
Yifan Wu 0a8bd2c3fd Fixed a bug that the efs lock was not be held correctly
4 years ago
Yifan Wu 01098eb113 Fixed a bug that the efs lock was not be held correctly
4 years ago
Yifan Wu e7f120bab3 Downgrade cargo-binutils to 0.2.0
4 years ago
Yifan Wu 7a36cdb77f Bump rustsbi to 0.2.0-alpha.1[81d53d8]
4 years ago
Yifan Wu f7ed29756c Link small sections in linker
4 years ago
Yifan Wu bfa6a80732 Downgrade cargo-binutils to 0.2.0
4 years ago
Yifan Wu ff69985d79 Downgrade cargo-binutils to 0.2.0
4 years ago
Yifan Wu a57d470edc Bump rustsbi to 0.2.0-alpha.1[81d53d8]
4 years ago
Yifan Wu 2e76499676 Bump rustsbi to 0.2.0-alpha.1[81d53d8]
4 years ago
Yifan Wu 91d4d6d40c Link small sections in linker
4 years ago
Yifan Wu a09429b32c Link small sections in linker
4 years ago
Yifan Wu 60477da9be
Merge pull request #9 from Spxg/main
4 years ago
Spxg a1cda4aa59 panic_handler: update msg format and add column location
4 years ago
Yifan Wu d302a0d616 Merge branch 'dev' into main
4 years ago
Yifan Wu 3554e20dc6 Do not fetch tools when running on qemu.
4 years ago
Yifan Wu d57a160b32 Do not fetch tools when running on qemu.
4 years ago
Yifan Wu 818363f2ca Fix qemu mmio range
4 years ago
Yifan Wu 195816ce2c Fix qemu mmio range
4 years ago
Yifan Wu 613f77c5a4 Remove unused code.
4 years ago
Yifan Wu 230e4442d0 Remove unused code.
4 years ago
Yifan Wu 6298f57a87 Remove DirentBytes
4 years ago
Yifan Wu a589179adc Remove DirentBytes
4 years ago
Yifan Wu 1e2e83e886 Remove unused code.
4 years ago
Yifan Wu 3e47a0dbee Remove unused code.
4 years ago
Yifan Wu a43dbc4e34 Fix k210 alignment issue when push cmdargs when sys_exec
4 years ago
Yifan Wu dd2be93ef0 Fix k210 alignment issue when push cmdargs when sys_exec
4 years ago
Yifan Wu e643af7937 Fix k210 alignment issue when push cmdargs when sys_exec
4 years ago
Yifan Wu e55c5200c5 Merge dev: ch7 updates
4 years ago
Yifan Wu 67372ac84d Merge updates from ch7
4 years ago
Yifan Wu cfa3819bee Add Ubuntu18.04 docker
4 years ago
Yifan Wu 33395156f9 Add Ubuntu18.04 docker
4 years ago
Yifan Wu 3e1c12b6a1 Add sys_dup && support input/output redirection in user_shell
4 years ago
Yifan Wu a7346c96b4 DiskInode sz->128bytes && user heap -> 32KiB
4 years ago
Yifan Wu 8a1c96d963 Add tool: cat
4 years ago
Yifan Wu 01280fc833 Support indirect2 in easy-fs::layout::DiskInode
4 years ago
Yifan Wu 90d351bfe8 Support cmdline_args when sys_exec.
4 years ago
Yifan Wu 4668911483
Merge pull request #6 from ZhangHanDong/main
4 years ago
Yifan Wu 9196963e44
Update Makefile
4 years ago
blackanger 3f5308f46c modify Makefile for Docker
4 years ago
blackanger e0a3933b1c add Dockerfile
4 years ago
Yifan Wu 35cc3d6e2f Fix overflow bug when ceiling va
4 years ago
Yifan Wu b8f1db4aa3 Fix overflow bug when ceiling va
4 years ago
Yifan Wu 1346fb1a1f Refactor easy-fs
4 years ago
Yifan Wu 12c6c53af5 Close all pipes in pipetest.
4 years ago
Yifan Wu e8a0682cf8 Refactor easy-fs
4 years ago
Yifan Wu 40508d68ab Clean easy-fs-fuse
4 years ago
Yifan Wu 920d077a66 Refactor easy-fs.
4 years ago
Yifan Wu 8b27976d23
Merge pull request #5 from Spxg/main
4 years ago
Spxg 84a55c17c4 VirtAddr: fix add with overflow when debug mode
4 years ago
Yifan Wu c6a262d215 Close all pipes in pipetest.
4 years ago
Yifan Wu 69933e2985 Close all pipes in pipetest.
4 years ago
Yifan Wu 00084f5165 Merge branch 'dev' into main
5 years ago
Yifan Wu 87743bac4d Merge branch 'ch7' into dev
5 years ago
Yifan Wu 04114ad949 Remove Any Trait of File
5 years ago
Yifan Wu a5c4f3228e Merge branch 'dev' into main
5 years ago
Yifan Wu 1b6f2c4c1e Fix lock uses in ch5
5 years ago
Yifan Wu 9b65abcfa8 Fix lock uses in ch5
5 years ago
Yifan Wu caac1beb0a Mutex -> RefCell in Processor.
5 years ago
Yifan Wu fd75ac027a Mutex -> RefCell in Processor.
5 years ago
Yifan Wu d97b0a20ab Replace TCB.inner.block with TCB::acquire_inner_lock
5 years ago
Yifan Wu e04394af56 Replace TCB.inner.block with TCB::acquire_inner_lock
5 years ago
Yifan Wu ea4222bed0 Merge branch 'dev' into main
5 years ago
Yifan Wu b28d94b226 Fix other usertests: xstate -> exit_code
5 years ago
Yifan Wu b6626d534b Fix other usertests: xstate -> exit_code
5 years ago
Yifan Wu 0d9024b5bd Merge branch 'dev' into main
5 years ago
Yifan Wu b659f10d22 Fix user_shell
5 years ago
Yifan Wu b8240fac5a Fix user_shell
5 years ago
Yifan Wu 29d02e7442 Small Fix.
5 years ago
Yifan Wu 4b6bd7deaa Merge branch 'dev' into main
5 years ago
Yifan Wu 1cc75ded25 Move kflash.py out of proj.
5 years ago
Yifan Wu dddd04b683 Move kflash.py out of proj.
5 years ago
Yifan Wu c01b3289c6 Merge branch 'dev' into main
5 years ago
Yifan Wu eb5ef8e956 Bump rustsbi to 0.1.1 && make config of qemu/k210 different
5 years ago
Yifan Wu 33373aa20d Bump rustsbi to 0.1.1 && make config of qemu/k210 different
5 years ago
Yifan Wu 5afed009c0 Merge branch 'dev' into main
5 years ago
Yifan Wu 07467287da Fix exit_code in user
5 years ago
Yifan Wu a6c7b52283 Fix exit_code in user
5 years ago
Yifan Wu 654f6eb959 Update os/Makefile && Update rust to 2021-01-30
5 years ago
Yifan Wu 18da8a3879 Update os/Makefile && Update rust to 2021-01-30
5 years ago
Yifan Wu 4f7db8b92e Update os/Makefile && Update rust to 2021-01-30
5 years ago
Yifan Wu 6267c5d922 Do not clone KERNEL_SPACE in mm::init
5 years ago
Yifan Wu 9d7882a73d Do not clone KERNEL_SPACE in mm::init
5 years ago
Yifan Wu 9772373743 Fix os/Makefile: Support macOS
5 years ago
Yifan Wu e588d40d70 Fix os/Makefile: Support macOS
5 years ago
Yifan Wu c14392ec60
Merge pull request #3 from cyyself/main
5 years ago
Yangyu Chen c763a3be96 add env check, write sdcard check and change dd bs to fit macOS
5 years ago
Yu Chen c65ce846ce rust-toochain --> nightly
5 years ago
Yu Chen e9453d7834 rust-toochain --> nightly
5 years ago
Yu Chen 3b8b42ec4c rust-toochain --> nightly
5 years ago
Yifan Wu 05d34106ce Update wyfcyx/rustsbi fb6af33f.
5 years ago
Yifan Wu 1a7261d86d Move some variable name to task_cx to task_cx_ptr2(ch5 ver).
5 years ago
Yifan Wu dbe56c1362 Move some variable name to task_cx to task_cx_ptr2(ch5 ver).
5 years ago
Yifan Wu 12747d71b4 Remove meaningless sstatus::set_sie() when initializing.
5 years ago
Yifan Wu fd638d5388 Remove meaningless sstatus::set_sie() when initializing.
5 years ago
Yifan Wu 208b827b5c Add env.
5 years ago
Yifan Wu b674fd5a77 Remove fs.img.
5 years ago
Yifan Wu fdab87d2ed Update Rust environment initialization.
5 years ago
Yifan Wu 7c0d66c58f Remove some warnings.
5 years ago
Yifan Wu 606abbe6a1 Simple filetest passed on qemu/k210.
5 years ago
Yifan Wu 1bafe9615f Fix virtio_phys_to_virt. Now we can load app from disk on qemu/k210!
5 years ago
Yifan Wu ae9eecf97b Load app from sdcard on K210, but panicked on qemu.
5 years ago
Yifan Wu ea515323d3 Import easy-fs in os && change easy-fs to no_std mode.
5 years ago
Yifan Wu ecccea65a0 Change single file limit from 70KiB to 94KiB & pack apps and list them.
5 years ago
Yifan Wu d13380603b Large/small file I/O test passed.
5 years ago
Yifan Wu dbaa7c8c6c Write Hello, world! to a file and read it!
5 years ago
Yifan Wu 5ea07840ce Eliminate unuseful block reads/writes.
5 years ago
Yifan Wu c351635e2f Create two files and list them.
5 years ago
Yifan Wu 459efec595 We need BlockCache.
5 years ago
Yifan Wu af02e68d19 create & open efs.
5 years ago
Yifan Wu 84900e8b94 Remove block device test.
5 years ago
Yifan Wu 63bccc4e8f Now sleep test sleeps 5secs.
5 years ago
Yifan Wu 914f042617 Add sdcard driver based on k210-rust crates && adjust clock freq.
5 years ago
Yifan Wu f754326d0a virtio-blk worked.
5 years ago
Yifan Wu 8e178c0080 Pipe OK.
5 years ago
Yifan Wu 2e6734027d Rewrite Stdin/Stdout
5 years ago
Yifan Wu f5c029d3db k210 only can use 6M before configuring sdcard.
5 years ago
Yifan Wu 84b10893d4 forktree worked with depth=3 on k210 platform.
5 years ago
Yifan Wu 244c0ee84d Handle backspace for k210/qemu platform && Add lots of user tests.
5 years ago
Yifan Wu 9165e64d1b Update rustsbi-k210.
5 years ago
Yifan Wu 7a1bc49eb3 Update rustsbi-k210 to enable lagacy console_putchar. Adjust alignment of links apps. Run matrix on K210!
5 years ago
Yifan Wu 3a2f89fc67 Avoid panicking when inputting error app name; List all apps when initializing.
5 years ago
Yifan Wu 3642f9c56d Implement many process syscalls.
5 years ago
Yifan Wu 81bef97f09 Add user program initproc/user_shell, allow user programs allocate data on heap.
5 years ago
Yifan Wu eddbc8954c Implement sys_read && allocate pid and kernel stack dynamically.
5 years ago
Yifan Wu 1bc53c0b5f Split TaskManager and Processor.
5 years ago
Yifan Wu 0c12d43b61 Get app data by name.
5 years ago
Yifan Wu 949f2095bb ch4 ok on qemu/k210 && Remove some comments.
5 years ago
Yifan Wu 8ee3671269 Split kernel/user trap handler && Fix user tests.
5 years ago
Yifan Wu f54573ae15 Fetch buffer in user space as a Vec.
5 years ago
Yifan Wu 064f1cb5cb Fetching buffer arguments from user space.
5 years ago
Yifan Wu 3625b7578d Analyse ELF.
5 years ago
Yifan Wu 0011fe9477 Kernel remapped.
5 years ago
Yifan Wu 1256085d36 Frame Allocator OK.
5 years ago
Yifan Wu 2fd70b0ff4 Heap test passed on k210/qemu, heap size = 3M.
5 years ago
Yifan Wu 4b01ff7cc4 Build application at 0x0 in release mode.
5 years ago
Yifan Wu 63aaa9d0a1 Chapter3: power2/3/5 & sleep test worked on k210/qemu based on timer.
5 years ago
Yifan Wu 4590f233b5 sys_yield tests worked on qemu.
5 years ago
Yifan Wu 91043b08cd Switch ok with debug mode apps, implement sys_exit correctly later.
5 years ago
Yifan Wu e552f3cfca Add sys_yield tests and compile them to different location.
5 years ago
Yifan Wu f53f5e4b98 Chapter2: Clear .bss inside application.
5 years ago
Yifan Wu 5e5ed05399 Update rustsbi-qemu && batch worked on qemu/k210.
5 years ago
Yifan Wu 3b920ac8f5 Run hello_world/power one by one in batch mode.
5 years ago
Yifan Wu e9949e5bd4 Try batch but kernel panicked.
5 years ago
Yifan Wu 0d0c7255b6 Auto link multiple applications in kernel.
5 years ago
Yifan Wu f613fa122c Working on app data auto linking.
5 years ago
Yifan Wu d1fcf2fc9f Removed unused cache.
5 years ago
Yifan Wu 7577c4f637 Update .gitignore
5 years ago
Yifan Wu db8fbdc512 Update user/.gitignore.
5 years ago
Yifan Wu db0c174f52 Add first user program hello_world.rs
5 years ago
Yifan Wu c701cd00ce Chapter1: Update panic_handler.
5 years ago
Yifan Wu 49050aca3f Update linker & Hello k210/qemu(release mode).
5 years ago
Yifan Wu 406acaae2c Small update.
5 years ago
Yifan Wu 721d0e5ecf Debug using gdb on qemu.
5 years ago
Yifan Wu 5370f725be Load kernel on qemu/k210.
5 years ago
Yifan Wu 29f4683ad8 Compiled on target platform.
5 years ago

@ -0,0 +1,66 @@
name: Build Rust Doc And Run tests
on: [push]
env:
CARGO_TERM_COLOR: always
jobs:
build-doc:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly-2022-04-11
components: rust-src, llvm-tools-preview
target: riscv64gc-unknown-none-elf
- name: Build doc
run: cd os && cargo doc --no-deps --verbose
- name: Deploy to Github Pages
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./os/target/riscv64gc-unknown-none-elf/doc
destination_dir: ${{ github.ref_name }}
run-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly-2022-04-11
components: rust-src, llvm-tools-preview
target: riscv64gc-unknown-none-elf
- uses: actions-rs/install@v0.1
with:
crate: cargo-binutils
version: latest
use-tool-cache: true
- name: Cache QEMU
uses: actions/cache@v3
with:
path: qemu-7.0.0
key: qemu-7.0.0-x86_64-riscv64
- name: Install QEMU
run: |
sudo apt-get update
sudo apt-get install ninja-build -y
if [ ! -d qemu-7.0.0 ]; then
wget https://download.qemu.org/qemu-7.0.0.tar.xz
tar -xf qemu-7.0.0.tar.xz
cd qemu-7.0.0
./configure --target-list=riscv64-softmmu
make -j
else
cd qemu-7.0.0
fi
sudo make install
qemu-system-riscv64 --version
- name: Run usertests
run: cd os && make run TEST=1
timeout-minutes: 10

14
.gitignore vendored

@ -1 +1,13 @@
.idea/*
.*/*
!.github/*
!.vscode/settings.json
**/target/
**/Cargo.lock
os/src/link_app.S
os/src/linker.ld
os/last-*
os/.gdb_history
tools/
pushall.sh

@ -0,0 +1,10 @@
{
// Prevent "can't find crate for `test`" error on no_std
// Ref: https://github.com/rust-lang/vscode-rust/issues/729
// For vscode-rust plugin users:
"rust.target": "riscv64gc-unknown-none-elf",
"rust.all_targets": false,
// For Rust Analyzer plugin users:
"rust-analyzer.cargo.target": "riscv64gc-unknown-none-elf",
"rust-analyzer.checkOnSave.allTargets": false
}

@ -0,0 +1,40 @@
FROM ubuntu:18.04
LABEL maintainer="dinghao188" \
version="1.1" \
description="ubuntu 18.04 with tools for tsinghua's rCore-Tutorial-V3"
#install some deps
RUN set -x \
&& apt-get update \
&& apt-get install -y curl wget autoconf automake autotools-dev curl libmpc-dev libmpfr-dev libgmp-dev \
gawk build-essential bison flex texinfo gperf libtool patchutils bc xz-utils \
zlib1g-dev libexpat-dev pkg-config libglib2.0-dev libpixman-1-dev git tmux python3
#install rust and qemu
RUN set -x; \
RUSTUP='/root/rustup.sh' \
&& cd $HOME \
#install rust
&& curl https://sh.rustup.rs -sSf > $RUSTUP && chmod +x $RUSTUP \
&& $RUSTUP -y --default-toolchain nightly --profile minimal \
#compile qemu
&& wget https://ftp.osuosl.org/pub/blfs/conglomeration/qemu/qemu-5.0.0.tar.xz \
&& tar xvJf qemu-5.0.0.tar.xz \
&& cd qemu-5.0.0 \
&& ./configure --target-list=riscv64-softmmu,riscv64-linux-user \
&& make -j$(nproc) install \
&& cd $HOME && rm -rf qemu-5.0.0 qemu-5.0.0.tar.xz
#for chinese network
RUN set -x; \
APT_CONF='/etc/apt/sources.list'; \
CARGO_CONF='/root/.cargo/config'; \
BASHRC='/root/.bashrc' \
&& echo 'export RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static' >> $BASHRC \
&& echo 'export RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup' >> $BASHRC \
&& touch $CARGO_CONF \
&& echo '[source.crates-io]' > $CARGO_CONF \
&& echo "replace-with = 'ustc'" >> $CARGO_CONF \
&& echo '[source.ustc]' >> $CARGO_CONF \
&& echo 'registry = "git://mirrors.ustc.edu.cn/crates.io-index"' >> $CARGO_CONF

@ -0,0 +1,10 @@
DOCKER_NAME ?= dinghao188/rcore-tutorial
.PHONY: docker build_docker
docker:
docker run --rm -it --mount type=bind,source=$(shell pwd),destination=/mnt ${DOCKER_NAME}
build_docker:
docker build -t ${DOCKER_NAME} .
fmt:
cd easy-fs; cargo fmt; cd ../easy-fs-fuse cargo fmt; cd ../os ; cargo fmt; cd ../user; cargo fmt; cd ..

@ -1,2 +1,300 @@
# rCore-Tutorial-v3
rCore-Tutorial version 3.
rCore-Tutorial version 3.5. See the [Documentation in Chinese](https://rcore-os.github.io/rCore-Tutorial-Book-v3/).
rCore-Tutorial API Docs. See the [API Docs of Ten OSes ](#OS-API-DOCS)
If you don't know Rust Language and try to learn it, please visit [Rust Learning Resources](https://github.com/rcore-os/rCore/wiki/study-resource-of-system-programming-in-RUST)
Official QQ group number: 735045051
## news
- 25/01/2022: Version 3.6.0 is on the way! Now we directly update the code on chX branches, please periodically check if there are any updates.
## Overview
This project aims to show how to write an **Unix-like OS** running on **RISC-V** platforms **from scratch** in **[Rust](https://www.rust-lang.org/)** for **beginners** without any background knowledge about **computer architectures, assembly languages or operating systems**.
## Features
* Platform supported: `qemu-system-riscv64` simulator or dev boards based on [Kendryte K210 SoC](https://canaan.io/product/kendryteai) such as [Maix Dock](https://www.seeedstudio.com/Sipeed-MAIX-Dock-p-4815.html)
* OS
* concurrency of multiple processes each of which contains mutiple native threads
* preemptive scheduling(Round-Robin algorithm)
* dynamic memory management in kernel
* virtual memory
* a simple file system with a block cache
* an interactive shell in the userspace
* **only 4K+ LoC**
* [A detailed documentation in Chinese](https://rcore-os.github.io/rCore-Tutorial-Book-v3/) in spite of the lack of comments in the code(English version is not available at present)
## Prerequisites
### Install Rust
See [official guide](https://www.rust-lang.org/tools/install).
Install some tools:
```sh
$ rustup target add riscv64gc-unknown-none-elf
$ cargo install cargo-binutils --vers =0.3.3
$ rustup component add llvm-tools-preview
$ rustup component add rust-src
```
### Install Qemu
Here we manually compile and install Qemu 5.0.0. For example, on Ubuntu 18.04:
```sh
# install dependency packages
$ sudo apt install autoconf automake autotools-dev curl libmpc-dev libmpfr-dev libgmp-dev \
gawk build-essential bison flex texinfo gperf libtool patchutils bc \
zlib1g-dev libexpat-dev pkg-config libglib2.0-dev libpixman-1-dev git tmux python3 python3-pip
# download Qemu source code
$ wget https://download.qemu.org/qemu-5.0.0.tar.xz
# extract to qemu-5.0.0/
$ tar xvJf qemu-5.0.0.tar.xz
$ cd qemu-5.0.0
# build
$ ./configure --target-list=riscv64-softmmu,riscv64-linux-user
$ make -j$(nproc)
```
Then, add following contents to `~/.bashrc`(please adjust these paths according to your environment):
```
export PATH=$PATH:/home/shinbokuow/Downloads/built/qemu-5.0.0
export PATH=$PATH:/home/shinbokuow/Downloads/built/qemu-5.0.0/riscv64-softmmu
export PATH=$PATH:/home/shinbokuow/Downloads/built/qemu-5.0.0/riscv64-linux-user
```
Finally, update the current shell:
```sh
$ source ~/.bashrc
```
Now we can check the version of Qemu:
```sh
$ qemu-system-riscv64 --version
QEMU emulator version 5.0.0
Copyright (c) 2003-2020 Fabrice Bellard and the QEMU Project developers
```
### Install RISC-V GNU Embedded Toolchain(including GDB)
Download the compressed file according to your platform From [Sifive website](https://www.sifive.com/software)(Ctrl+F 'toolchain').
Extract it and append the location of the 'bin' directory under its root directory to `$PATH`.
For example, we can check the version of GDB:
```sh
$ riscv64-unknown-elf-gdb --version
GNU gdb (SiFive GDB-Metal 10.1.0-2020.12.7) 10.1
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
```
### Install serial tools(Optional, if you want to run on K210)
```sh
$ pip3 install pyserial
$ sudo apt install python3-serial
```
## Run our project
### Qemu
```sh
$ git clone https://github.com/rcore-os/rCore-Tutorial-v3.git
$ cd rCore-Tutorial-v3/os
$ make run
```
After outputing some debug messages, the kernel lists all the applications available and enter the user shell:
```
/**** APPS ****
mpsc_sem
usertests
pipetest
forktest2
cat
initproc
race_adder_loop
threads_arg
race_adder_mutex_spin
race_adder_mutex_blocking
forktree
user_shell
huge_write
race_adder
race_adder_atomic
threads
stack_overflow
filetest_simple
forktest_simple
cmdline_args
run_pipe_test
forktest
matrix
exit
fantastic_text
sleep_simple
yield
hello_world
pipe_large_test
sleep
phil_din_mutex
**************/
Rust user shell
>>
```
You can run any application except for `initproc` and `user_shell` itself. To run an application, just input its filename and hit enter. `usertests` can run a bunch of applications, thus it is recommended.
Type `Ctrl+a` then `x` to exit Qemu.
### K210
Before chapter 6, you do not need a SD card:
```sh
$ git clone https://github.com/rcore-os/rCore-Tutorial-v3.git
$ cd rCore-Tutorial-v3/os
$ make run BOARD=k210
```
From chapter 6, before running the kernel, we should insert a SD card into PC and manually write the filesystem image to it:
```sh
$ cd rCore-Tutorial-v3/os
$ make sdcard
```
By default it will overwrite the device `/dev/sdb` which is the SD card, but you can provide another location. For example, `make sdcard SDCARD=/dev/sdc`.
After that, remove the SD card from PC and insert it to the slot of K210. Connect the K210 to PC and then:
```sh
$ git clone https://github.com/rcore-os/rCore-Tutorial-v3.git
$ cd rCore-Tutorial-v3/os
$ make run BOARD=k210
```
Type `Ctrl+]` to disconnect from K210.
## Show runtime debug info of OS kernel version
The branch of ch9-log contains a lot of debug info. You could try to run rcore tutorial
for understand the internal behavior of os kernel.
```sh
$ git clone https://github.com/rcore-os/rCore-Tutorial-v3.git
$ cd rCore-Tutorial-v3/os
$ git checkout ch9-log
$ make run
......
[rustsbi] RustSBI version 0.2.0-alpha.10, adapting to RISC-V SBI v0.3
.______ __ __ _______.___________. _______..______ __
| _ \ | | | | / | | / || _ \ | |
| |_) | | | | | | (----`---| |----`| (----`| |_) || |
| / | | | | \ \ | | \ \ | _ < | |
| |\ \----.| `--' |.----) | | | .----) | | |_) || |
| _| `._____| \______/ |_______/ |__| |_______/ |______/ |__|
[rustsbi] Implementation: RustSBI-QEMU Version 0.0.2
[rustsbi-dtb] Hart count: cluster0 with 1 cores
[rustsbi] misa: RV64ACDFIMSU
[rustsbi] mideleg: ssoft, stimer, sext (0x222)
[rustsbi] medeleg: ima, ia, bkpt, la, sa, uecall, ipage, lpage, spage (0xb1ab)
[rustsbi] pmp0: 0x10000000 ..= 0x10001fff (rw-)
[rustsbi] pmp1: 0x2000000 ..= 0x200ffff (rw-)
[rustsbi] pmp2: 0xc000000 ..= 0xc3fffff (rw-)
[rustsbi] pmp3: 0x80000000 ..= 0x8fffffff (rwx)
[rustsbi] enter supervisor 0x80200000
[KERN] rust_main() begin
[KERN] clear_bss() begin
[KERN] clear_bss() end
[KERN] mm::init() begin
[KERN] mm::init_heap() begin
[KERN] mm::init_heap() end
[KERN] mm::init_frame_allocator() begin
[KERN] mm::frame_allocator::lazy_static!FRAME_ALLOCATOR begin
......
```
## Rustdoc
Currently it can only help you view the code since only a tiny part of the code has been documented.
You can open a doc html of `os` using `cargo doc --no-deps --open` under `os` directory.
### OS-API-DOCS
The API Docs for Ten OS
1. [Lib-OS API doc](https://learningos.github.io/rCore-Tutorial-v3/ch1/os/index.html)
1. [Batch-OS API doc](https://learningos.github.io/rCore-Tutorial-v3/ch2/os/index.html)
1. [MultiProg-OS API doc](https://learningos.github.io/rCore-Tutorial-v3/ch3-coop/os/index.html)
1. [TimeSharing-OS API doc](https://learningos.github.io/rCore-Tutorial-v3/ch3/os/index.html)
1. [AddrSpace-OS API doc](https://learningos.github.io/rCore-Tutorial-v3/ch4/os/index.html)
1. [Process-OS API doc](https://learningos.github.io/rCore-Tutorial-v3/ch5/os/index.html)
1. [FileSystem-OS API doc](https://learningos.github.io/rCore-Tutorial-v3/ch6/os/index.html)
1. [IPC-OS API doc](https://learningos.github.io/rCore-Tutorial-v3/ch7/os/index.html)
1. [SyncMutex-OS API doc](https://learningos.github.io/rCore-Tutorial-v3/ch8/os/index.html)
1. [IODevice-OS API doc](https://learningos.github.io/rCore-Tutorial-v3/ch9/os/index.html)
## Working in progress
Our first release 3.5.0 (chapter 1-7) has been published.
There will be 9 chapters in our next release 3.6.0, where 2 new chapters will be added:
* chapter 8: synchronization on a uniprocessor
* chapter 9: I/O devices
Current version is 3.6.0-alpha.1 and we are still working on it.
Here are the updates since 3.5.0:
### Completed
* [x] automatically clean up and rebuild before running our project on a different platform
* [x] fix `power` series application in early chapters, now you can find modulus in the output
* [x] use `UPSafeCell` instead of `RefCell` or `spin::Mutex` in order to access static data structures and adjust its API so that it cannot be borrowed twice at a time(mention `& .exclusive_access().task[0]` in `run_first_task`)
* [x] move `TaskContext` into `TaskControlBlock` instead of restoring it in place on kernel stack(since ch3), eliminating annoying `task_cx_ptr2`
* [x] replace `llvm_asm!` with `asm!`
* [x] expand the fs image size generated by `rcore-fs-fuse` to 128MiB
* [x] add a new test named `huge_write` which evaluates the fs performance(qemu\~500KiB/s k210\~50KiB/s)
* [x] flush all block cache to disk after a fs transaction which involves write operation
* [x] replace `spin::Mutex` with `UPSafeCell` before SMP chapter
* [x] add codes for a new chapter about synchronization & mutual exclusion(uniprocessor only)
* [x] bug fix: we should call `find_pte` rather than `find_pte_create` in `PageTable::unmap`
* [x] clarify: "check validity of level-3 pte in `find_pte` instead of checking it outside this function" should not be a bug
* [x] code of chapter 8: synchronization on a uniprocessor
* [x] switch the code of chapter 6 and chapter 7
* [x] support signal mechanism in chapter 7/8(only works for apps with a single thread)
* [x] Add boards/ directory and support rustdoc, for example you can use `cargo doc --no-deps --open` to view the documentation of a crate
### Todo(High priority)
* [ ] review documentation, current progress: 5/9
* [ ] support user-level sync primitives in chapter 8
* [ ] code of chapter 9: device drivers based on interrupts, including UART and block devices
* [ ] use old fs image optionally, do not always rebuild the image
* [ ] add new system calls: getdents64/fstat
* [ ] shell functionality improvement(to be continued...)
* [ ] give every non-zero process exit code an unique and clear error type
* [ ] effective error handling of mm module
### Todo(Low priority)
* [ ] rewrite practice doc and remove some inproper questions
* [ ] provide smooth debug experience at a Rust source code level
* [ ] format the code using official tools
### Crates
We will add them later.

Binary file not shown.

Binary file not shown.

@ -1,3 +0,0 @@
# Tutorial 第一章测试用例
第一章只需要在 RV64 裸机平台运行一个嵌入式应用,因此只要能够在 SBI 的帮助下在屏幕上输出一行字符串就算成功。

2
ch2/.gitignore vendored

@ -1,2 +0,0 @@
target/*
Cargo.lock

@ -1,21 +0,0 @@
TARGET := riscv64gc-unknown-none-elf
MODE := release
APP_DIR := src/bin
TARGET_DIR := target/$(TARGET)/$(MODE)
APPS := $(wildcard $(APP_DIR)/*.rs)
ELFS := $(patsubst $(APP_DIR)/%.rs, $(TARGET_DIR)/%, $(APPS))
BINS := $(patsubst $(APP_DIR)/%.rs, $(TARGET_DIR)/%.bin, $(APPS))
OBJDUMP := rust-objdump --arch-name=riscv64
OBJCOPY := rust-objcopy --binary-architecture=riscv64
elf:
@cargo build --release
@echo $(APPS)
@echo $(ELFS)
@echo $(BINS)
binary: elf
$(foreach elf, $(ELFS), $(OBJCOPY) $(elf) --strip-all -O binary $(patsubst $(TARGET_DIR)/%, $(TARGET_DIR)/%.bin, $(elf));)
build: binary

@ -1,20 +0,0 @@
# Tutorial 第二章测试用例
第二章我们需要实现一个批处理系统。在 `src/bin` 目录中,我们可以找到三个应用程序:
* `00hello_world`
* `01store_fault`
* `02power`
我们需要按照编号从小到大的顺序去加载并运行它们。
应用被设计为运行在用户模式,批处理系统应运行在监督模式,它们都直接访问物理内存。
三个应用被需要被加载到同一个物理地址。
本章需要实现的系统调用:
* `sys_write` 用于向屏幕输出字符串;
* `sys_exit` 用于告知批处理系统当前应用退出,应切换到下一个应用。
注意:应用 `01store_fault` 会访问非法的物理地址,批处理系统需要杀死它并能够正常运行序列中的下一个应用 `02power`

@ -1,11 +0,0 @@
#![no_std]
#![no_main]
#[macro_use]
extern crate user_lib;
#[no_mangle]
fn main() -> i32 {
println!("Hello, world!");
0
}

@ -1,27 +0,0 @@
#![no_std]
#![no_main]
#[macro_use]
extern crate user_lib;
const SIZE: usize = 10;
const P: u32 = 3;
const STEP: usize = 100000;
const MOD: u32 = 10007;
#[no_mangle]
fn main() -> i32 {
let mut pow = [0u32; SIZE];
let mut index: usize = 0;
pow[index] = 1;
for i in 1..=STEP {
let last = pow[index];
index = (index + 1) % SIZE;
pow[index] = last * P % MOD;
if i % 10000 == 0 {
println!("{}^{}={}", P, i, pow[index]);
}
}
println!("Test power OK!");
0
}

@ -1,29 +0,0 @@
use core::fmt::{self, Write};
use crate::syscall::{STDOUT, sys_write};
struct Stdout;
impl Write for Stdout {
fn write_str(&mut self, s: &str) -> fmt::Result {
sys_write(STDOUT, s.as_bytes());
Ok(())
}
}
pub fn print(args: fmt::Arguments) {
Stdout.write_fmt(args).unwrap();
}
#[macro_export]
macro_rules! print {
($fmt: literal $(, $($arg: tt)+)?) => {
$crate::console::print(format_args!($fmt $(, $($arg)+)?));
}
}
#[macro_export]
macro_rules! println {
($fmt: literal $(, $($arg: tt)+)?) => {
$crate::console::print(format_args!(concat!($fmt, "\n") $(, $($arg)+)?));
}
}

@ -1,10 +0,0 @@
#[panic_handler]
fn panic_handler(panic_info: &core::panic::PanicInfo) -> ! {
let err = panic_info.message().unwrap();
if let Some(location) = panic_info.location() {
println!("Panicked at {}:{}, {}", location.file(), location.line(), err);
} else {
println!("Panicked: {}", err);
}
loop {}
}

@ -1,33 +0,0 @@
#![no_std]
#![feature(llvm_asm)]
#![feature(linkage)]
#![feature(panic_info_message)]
#[macro_use]
pub mod console;
mod syscall;
mod lang_items;
#[no_mangle]
#[link_section = ".text.entry"]
pub extern "C" fn _start() -> ! {
clear_bss();
syscall::sys_exit(main());
panic!("unreachable after sys_exit!");
}
#[linkage = "weak"]
#[no_mangle]
fn main() -> i32 {
panic!("Cannot find main!");
}
fn clear_bss() {
extern "C" {
fn start_bss();
fn end_bss();
}
(start_bss as usize..end_bss as usize).for_each(|addr| {
unsafe { (addr as *mut u8).write_volatile(0); }
});
}

@ -1,28 +0,0 @@
OUTPUT_ARCH(riscv)
ENTRY(_start)
BASE_ADDRESS = 0x80040000;
SECTIONS
{
. = BASE_ADDRESS;
.text : {
*(.text.entry)
*(.text .text.*)
}
.rodata : {
*(.rodata .rodata.*)
}
.data : {
*(.data .data.*)
}
.bss : {
start_bss = .;
*(.bss .bss.*)
end_bss = .;
}
/DISCARD/ : {
*(.eh_frame)
*(.debug*)
}
}

@ -1,25 +0,0 @@
pub const STDOUT: usize = 1;
const SYSCALL_WRITE: usize = 64;
const SYSCALL_EXIT: usize = 93;
fn syscall(id: usize, args: [usize; 3]) -> isize {
let mut ret: isize;
unsafe {
llvm_asm!("ecall"
: "={x10}" (ret)
: "{x10}" (args[0]), "{x11}" (args[1]), "{x12}" (args[2]), "{x17}" (id)
: "memory"
: "volatile"
);
}
ret
}
pub fn sys_write(fd: usize, buffer: &[u8]) -> isize {
syscall(SYSCALL_WRITE, [fd, buffer.as_ptr() as usize, buffer.len()])
}
pub fn sys_exit(xstate: i32) -> isize {
syscall(SYSCALL_EXIT, [xstate as usize, 0, 0])
}

@ -1,7 +0,0 @@
[build]
target = "riscv64gc-unknown-none-elf"
[target.riscv64gc-unknown-none-elf]
rustflags = [
"-Clink-args=-Tsrc/linker.ld",
]

@ -1,9 +0,0 @@
[package]
name = "user_lib"
version = "0.1.0"
authors = ["Yifan Wu <shinbokuow@163.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

@ -1,23 +0,0 @@
TARGET := riscv64gc-unknown-none-elf
MODE := release
APP_DIR := src/bin
TARGET_DIR := target/$(TARGET)/$(MODE)
APPS := $(wildcard $(APP_DIR)/*.rs)
ELFS := $(patsubst $(APP_DIR)/%.rs, $(TARGET_DIR)/%, $(APPS))
BINS := $(patsubst $(APP_DIR)/%.rs, $(TARGET_DIR)/%.bin, $(APPS))
OBJDUMP := rust-objdump --arch-name=riscv64
OBJCOPY := rust-objcopy --binary-architecture=riscv64
elf: $(APPS)
@python3 build.py
binary: elf
$(foreach elf, $(ELFS), $(OBJCOPY) $(elf) --strip-all -O binary $(patsubst $(TARGET_DIR)/%, $(TARGET_DIR)/%.bin, $(elf));)
build: binary
clean:
@cargo clean
.PHONY: elf binary build clean

@ -1,19 +0,0 @@
# Tutorial 第三章测试用例 part1
在 Tutorial 第三章第一阶段中,只需实现一个非抢占式调度的分时多任务系统。在 `src/bin` 目录中,我们可以找到三个应用程序:
* `00write_a`
* `01write_b`
* `02write_c`
我们需要在系统初始化的时候将它们分别加载到一个应用与内核预先约定的物理地址。对于编号 0 <= i <= 2应该将其加载到物理地址 0x80100000+0x20000\*i 处运行。这需要在构建应用程序的时候动态调整 `linker.ld` 中的应用起始地址,参见 `build.py`
应用被设计为运行在用户模式,批处理系统应运行在监督模式,它们都直接访问物理内存。
这一阶段需要实现的系统调用:
* `sys_yield`:用来交出当前应用的 CPU 使用权并切换到下一个应用,等到再次获得 CPU 使用权才能继续向下执行。
阅读应用程序代码可知,每个应用都输出一个字母方阵,但是每输出一行之后就会 `sys_yield`,于是最终应该看到一个字母交错的方阵。
注意:当所有应用通过 `sys_exit` 退出之后,系统也应该退出。

@ -1,25 +0,0 @@
import os
base_address = 0x80100000
step = 0x20000
linker = 'src/linker.ld'
app_id = 0
apps = os.listdir('src/bin')
apps.sort()
for app in apps:
app = app[:app.find('.')]
lines = []
lines_before = []
with open(linker, 'r') as f:
for line in f.readlines():
lines_before.append(line)
line = line.replace(hex(base_address), hex(base_address+step*app_id))
lines.append(line)
with open(linker, 'w+') as f:
f.writelines(lines)
os.system('cargo build --bin %s --release' % app)
print('[build.py] application %s start with address %s' %(app, hex(base_address+step*app_id)))
with open(linker, 'w+') as f:
f.writelines(lines_before)
app_id = app_id + 1

@ -1,21 +0,0 @@
#![no_std]
#![no_main]
#[macro_use]
extern crate user_lib;
use user_lib::sys_yield;
const WIDTH: usize = 10;
const HEIGHT: usize = 5;
#[no_mangle]
fn main() -> i32 {
for i in 0..HEIGHT {
for _ in 0..WIDTH { print!("A"); }
println!(" [{}/{}]", i + 1, HEIGHT);
sys_yield();
}
println!("Test write_a OK!");
0
}

@ -1,21 +0,0 @@
#![no_std]
#![no_main]
#[macro_use]
extern crate user_lib;
use user_lib::sys_yield;
const WIDTH: usize = 10;
const HEIGHT: usize = 2;
#[no_mangle]
fn main() -> i32 {
for i in 0..HEIGHT {
for _ in 0..WIDTH { print!("B"); }
println!(" [{}/{}]", i + 1, HEIGHT);
sys_yield();
}
println!("Test write_b OK!");
0
}

@ -1,21 +0,0 @@
#![no_std]
#![no_main]
#[macro_use]
extern crate user_lib;
use user_lib::sys_yield;
const WIDTH: usize = 10;
const HEIGHT: usize = 3;
#[no_mangle]
fn main() -> i32 {
for i in 0..HEIGHT {
for _ in 0..WIDTH { print!("C"); }
println!(" [{}/{}]", i + 1, HEIGHT);
sys_yield();
}
println!("Test write_c OK!");
0
}

@ -1,10 +0,0 @@
#[panic_handler]
fn panic_handler(panic_info: &core::panic::PanicInfo) -> ! {
let err = panic_info.message().unwrap();
if let Some(location) = panic_info.location() {
println!("Panicked at {}:{}, {}", location.file(), location.line(), err);
} else {
println!("Panicked: {}", err);
}
loop {}
}

@ -1,35 +0,0 @@
#![no_std]
#![feature(llvm_asm)]
#![feature(linkage)]
#![feature(panic_info_message)]
#[macro_use]
pub mod console;
mod syscall;
mod lang_items;
#[no_mangle]
#[link_section = ".text.entry"]
pub extern "C" fn _start() -> ! {
clear_bss();
syscall::sys_exit(main());
panic!("unreachable after sys_exit!");
}
#[linkage = "weak"]
#[no_mangle]
fn main() -> i32 {
panic!("Cannot find main!");
}
fn clear_bss() {
extern "C" {
fn start_bss();
fn end_bss();
}
(start_bss as usize..end_bss as usize).for_each(|addr| {
unsafe { (addr as *mut u8).write_volatile(0); }
});
}
pub use syscall::*;

@ -1,29 +0,0 @@
OUTPUT_ARCH(riscv)
ENTRY(_start)
BASE_ADDRESS = 0x80100000;
SECTIONS
{
. = BASE_ADDRESS;
.text : {
*(.text.entry)
*(.text .text.*)
}
.rodata : {
*(.rodata .rodata.*)
}
.data : {
*(.data .data.*)
}
.bss : {
start_bss = .;
*(.bss .bss.*)
end_bss = .;
}
/DISCARD/ : {
*(.eh_frame)
*(.debug*)
}
}

@ -1,35 +0,0 @@
pub const STDOUT: usize = 1;
const SYSCALL_WRITE: usize = 64;
const SYSCALL_EXIT: usize = 93;
const SYSCALL_YIELD: usize = 124;
const SYSCALL_GET_TIME: usize = 169;
fn syscall(id: usize, args: [usize; 3]) -> isize {
let mut ret: isize;
unsafe {
llvm_asm!("ecall"
: "={x10}" (ret)
: "{x10}" (args[0]), "{x11}" (args[1]), "{x12}" (args[2]), "{x17}" (id)
: "memory"
: "volatile"
);
}
ret
}
pub fn sys_write(fd: usize, buffer: &[u8]) -> isize {
syscall(SYSCALL_WRITE, [fd, buffer.as_ptr() as usize, buffer.len()])
}
pub fn sys_exit(xstate: i32) -> isize {
syscall(SYSCALL_EXIT, [xstate as usize, 0, 0])
}
pub fn sys_yield() -> isize {
syscall(SYSCALL_YIELD, [0, 0, 0])
}
pub fn sys_get_time() -> isize {
syscall(SYSCALL_GET_TIME, [0, 0, 0])
}

@ -1,7 +0,0 @@
[build]
target = "riscv64gc-unknown-none-elf"
[target.riscv64gc-unknown-none-elf]
rustflags = [
"-Clink-args=-Tsrc/linker.ld",
]

1
ch3/.gitignore vendored

@ -1 +0,0 @@
target/*

@ -1,23 +0,0 @@
TARGET := riscv64gc-unknown-none-elf
MODE := release
APP_DIR := src/bin
TARGET_DIR := target/$(TARGET)/$(MODE)
APPS := $(wildcard $(APP_DIR)/*.rs)
ELFS := $(patsubst $(APP_DIR)/%.rs, $(TARGET_DIR)/%, $(APPS))
BINS := $(patsubst $(APP_DIR)/%.rs, $(TARGET_DIR)/%.bin, $(APPS))
OBJDUMP := rust-objdump --arch-name=riscv64
OBJCOPY := rust-objcopy --binary-architecture=riscv64
elf: $(APPS)
@python3 build.py
binary: elf
$(foreach elf, $(ELFS), $(OBJCOPY) $(elf) --strip-all -O binary $(patsubst $(TARGET_DIR)/%, $(TARGET_DIR)/%.bin, $(elf));)
build: binary
clean:
@cargo clean
.PHONY: elf binary build clean

@ -1,19 +0,0 @@
# Tutorial 第三章测试用例 part2
在第三章第二阶段中,需要在第一阶段的基础上实现一个抢占式调度的分时多任务系统。在 `src/bin` 目录下,我们可以找到四个应用程序:
* ``00power_3``
* ``01power_5``
* ``02power_7``
* ``03sleep``
像第一阶段一样,它们也需要被加载到不同的物理地址处运行。
应用被设计为运行在用户模式,批处理系统应运行在监督模式,它们都直接访问物理内存。
这一阶段需要实现的系统调用:
* `sys_get_time`:获取系统当前的毫秒数,在应用 `03sleep` 中使用,可以简单估计所有应用运行的总时间。
阅读应用程序代码可知,三个 `power` 应用分别计算一个质数的幂次对另一个大质数取模的余数,由于次数很高在一个时间片之内无法完成,而它们又不会使用 `sys_yield` 主动交出 CPU 使用权,因此只能由内核进行强制切换。

@ -1,25 +0,0 @@
import os
base_address = 0x80100000
step = 0x20000
linker = 'src/linker.ld'
app_id = 0
apps = os.listdir('src/bin')
apps.sort()
for app in apps:
app = app[:app.find('.')]
lines = []
lines_before = []
with open(linker, 'r') as f:
for line in f.readlines():
lines_before.append(line)
line = line.replace(hex(base_address), hex(base_address+step*app_id))
lines.append(line)
with open(linker, 'w+') as f:
f.writelines(lines)
os.system('cargo build --bin %s --release' % app)
print('[build.py] application %s start with address %s' %(app, hex(base_address+step*app_id)))
with open(linker, 'w+') as f:
f.writelines(lines_before)
app_id = app_id + 1

@ -1,28 +0,0 @@
#![no_std]
#![no_main]
#[macro_use]
extern crate user_lib;
const LEN: usize = 100;
#[no_mangle]
fn main() -> i32 {
let p = 3u64;
let m = 998244353u64;
let iter: usize = 200000;
let mut s = [0u64; LEN];
let mut cur = 0usize;
s[cur] = 1;
for i in 1..=iter {
let next = if cur + 1 == LEN { 0 } else { cur + 1 };
s[next] = s[cur] * p % m;
cur = next;
if i % 10000 == 0 {
println!("power_3 [{}/{}]", i, iter);
}
}
println!("{}^{} = {}", p, iter, s[cur]);
println!("Test power_3 OK!");
0
}

@ -1,28 +0,0 @@
#![no_std]
#![no_main]
#[macro_use]
extern crate user_lib;
const LEN: usize = 100;
#[no_mangle]
fn main() -> i32 {
let p = 5u64;
let m = 998244353u64;
let iter: usize = 140000;
let mut s = [0u64; LEN];
let mut cur = 0usize;
s[cur] = 1;
for i in 1..=iter {
let next = if cur + 1 == LEN { 0 } else { cur + 1 };
s[next] = s[cur] * p % m;
cur = next;
if i % 10000 == 0 {
println!("power_5 [{}/{}]", i, iter);
}
}
println!("{}^{} = {}", p, iter, s[cur]);
println!("Test power_5 OK!");
0
}

@ -1,28 +0,0 @@
#![no_std]
#![no_main]
#[macro_use]
extern crate user_lib;
const LEN: usize = 100;
#[no_mangle]
fn main() -> i32 {
let p = 7u64;
let m = 998244353u64;
let iter: usize = 160000;
let mut s = [0u64; LEN];
let mut cur = 0usize;
s[cur] = 1;
for i in 1..=iter {
let next = if cur + 1 == LEN { 0 } else { cur + 1 };
s[next] = s[cur] * p % m;
cur = next;
if i % 10000 == 0 {
println!("power_7 [{}/{}]", i, iter);
}
}
println!("{}^{} = {}", p, iter, s[cur]);
println!("Test power_7 OK!");
0
}

@ -1,18 +0,0 @@
#![no_std]
#![no_main]
#[macro_use]
extern crate user_lib;
use user_lib::{sys_get_time, sys_yield};
#[no_mangle]
fn main() -> i32 {
let current_timer = sys_get_time();
let wait_for = current_timer + 10000000;
while sys_get_time() < wait_for {
sys_yield();
}
println!("Test sleep OK!");
0
}

@ -1,29 +0,0 @@
use core::fmt::{self, Write};
use crate::syscall::{STDOUT, sys_write};
struct Stdout;
impl Write for Stdout {
fn write_str(&mut self, s: &str) -> fmt::Result {
sys_write(STDOUT, s.as_bytes());
Ok(())
}
}
pub fn print(args: fmt::Arguments) {
Stdout.write_fmt(args).unwrap();
}
#[macro_export]
macro_rules! print {
($fmt: literal $(, $($arg: tt)+)?) => {
$crate::console::print(format_args!($fmt $(, $($arg)+)?));
}
}
#[macro_export]
macro_rules! println {
($fmt: literal $(, $($arg: tt)+)?) => {
$crate::console::print(format_args!(concat!($fmt, "\n") $(, $($arg)+)?));
}
}

@ -1,10 +0,0 @@
#[panic_handler]
fn panic_handler(panic_info: &core::panic::PanicInfo) -> ! {
let err = panic_info.message().unwrap();
if let Some(location) = panic_info.location() {
println!("Panicked at {}:{}, {}", location.file(), location.line(), err);
} else {
println!("Panicked: {}", err);
}
loop {}
}

@ -1,35 +0,0 @@
#![no_std]
#![feature(llvm_asm)]
#![feature(linkage)]
#![feature(panic_info_message)]
#[macro_use]
pub mod console;
mod syscall;
mod lang_items;
#[no_mangle]
#[link_section = ".text.entry"]
pub extern "C" fn _start() -> ! {
clear_bss();
syscall::sys_exit(main());
panic!("unreachable after sys_exit!");
}
#[linkage = "weak"]
#[no_mangle]
fn main() -> i32 {
panic!("Cannot find main!");
}
fn clear_bss() {
extern "C" {
fn start_bss();
fn end_bss();
}
(start_bss as usize..end_bss as usize).for_each(|addr| {
unsafe { (addr as *mut u8).write_volatile(0); }
});
}
pub use syscall::*;

@ -1,29 +0,0 @@
OUTPUT_ARCH(riscv)
ENTRY(_start)
BASE_ADDRESS = 0x80100000;
SECTIONS
{
. = BASE_ADDRESS;
.text : {
*(.text.entry)
*(.text .text.*)
}
.rodata : {
*(.rodata .rodata.*)
}
.data : {
*(.data .data.*)
}
.bss : {
start_bss = .;
*(.bss .bss.*)
end_bss = .;
}
/DISCARD/ : {
*(.eh_frame)
*(.debug*)
}
}

@ -1,35 +0,0 @@
pub const STDOUT: usize = 1;
const SYSCALL_WRITE: usize = 64;
const SYSCALL_EXIT: usize = 93;
const SYSCALL_YIELD: usize = 124;
const SYSCALL_GET_TIME: usize = 169;
fn syscall(id: usize, args: [usize; 3]) -> isize {
let mut ret: isize;
unsafe {
llvm_asm!("ecall"
: "={x10}" (ret)
: "{x10}" (args[0]), "{x11}" (args[1]), "{x12}" (args[2]), "{x17}" (id)
: "memory"
: "volatile"
);
}
ret
}
pub fn sys_write(fd: usize, buffer: &[u8]) -> isize {
syscall(SYSCALL_WRITE, [fd, buffer.as_ptr() as usize, buffer.len()])
}
pub fn sys_exit(xstate: i32) -> isize {
syscall(SYSCALL_EXIT, [xstate as usize, 0, 0])
}
pub fn sys_yield() -> isize {
syscall(SYSCALL_YIELD, [0, 0, 0])
}
pub fn sys_get_time() -> isize {
syscall(SYSCALL_GET_TIME, [0, 0, 0])
}

1
ch4/.gitignore vendored

@ -1 +0,0 @@
target/*

@ -1,3 +0,0 @@
# Tutorial 第四章测试用例
第四章的应用与第三章第二阶段的应用一样,只是它们均被链接到 0x0 开头的地址空间上。这就需要我们利用 CPU 提供的页表机制,为每个应用创造一个虚拟地址空间。

@ -1,29 +0,0 @@
#![no_std]
#![no_main]
#[macro_use]
extern crate user_lib;
const LEN: usize = 100;
static mut S: [u64; LEN] = [0u64; LEN];
#[no_mangle]
unsafe fn main() -> i32 {
let p = 3u64;
let m = 998244353u64;
let iter: usize = 300000;
let mut cur = 0usize;
S[cur] = 1;
for i in 1..=iter {
let next = if cur + 1 == LEN { 0 } else { cur + 1 };
S[next] = S[cur] * p % m;
cur = next;
if i % 10000 == 0 {
println!("power_3 [{}/{}]", i, iter);
}
}
println!("{}^{} = {}(mod {})", p, iter, S[cur], m);
println!("Test power_3 OK!");
0
}

@ -1,29 +0,0 @@
#![no_std]
#![no_main]
#[macro_use]
extern crate user_lib;
const LEN: usize = 100;
static mut S: [u64; LEN] = [0u64; LEN];
#[no_mangle]
unsafe fn main() -> i32 {
let p = 5u64;
let m = 998244353u64;
let iter: usize = 210000;
let mut cur = 0usize;
S[cur] = 1;
for i in 1..=iter {
let next = if cur + 1 == LEN { 0 } else { cur + 1 };
S[next] = S[cur] * p % m;
cur = next;
if i % 10000 == 0 {
println!("power_5 [{}/{}]", i, iter);
}
}
println!("{}^{} = {}(mod {})", p, iter, S[cur], m);
println!("Test power_5 OK!");
0
}

@ -1,29 +0,0 @@
#![no_std]
#![no_main]
#[macro_use]
extern crate user_lib;
const LEN: usize = 100;
static mut S: [u64; LEN] = [0u64; LEN];
#[no_mangle]
unsafe fn main() -> i32 {
let p = 7u64;
let m = 998244353u64;
let iter: usize = 240000;
let mut cur = 0usize;
S[cur] = 1;
for i in 1..=iter {
let next = if cur + 1 == LEN { 0 } else { cur + 1 };
S[next] = S[cur] * p % m;
cur = next;
if i % 10000 == 0 {
println!("power_7 [{}/{}]", i, iter);
}
}
println!("{}^{} = {}(mod {})", p, iter, S[cur], m);
println!("Test power_7 OK!");
0
}

@ -1,18 +0,0 @@
#![no_std]
#![no_main]
#[macro_use]
extern crate user_lib;
use user_lib::{sys_get_time, sys_yield};
#[no_mangle]
fn main() -> i32 {
let current_timer = sys_get_time();
let wait_for = current_timer + 10000000;
while sys_get_time() < wait_for {
sys_yield();
}
println!("Test sleep OK!");
0
}

@ -1,29 +0,0 @@
use core::fmt::{self, Write};
use crate::syscall::{STDOUT, sys_write};
struct Stdout;
impl Write for Stdout {
fn write_str(&mut self, s: &str) -> fmt::Result {
sys_write(STDOUT, s.as_bytes());
Ok(())
}
}
pub fn print(args: fmt::Arguments) {
Stdout.write_fmt(args).unwrap();
}
#[macro_export]
macro_rules! print {
($fmt: literal $(, $($arg: tt)+)?) => {
$crate::console::print(format_args!($fmt $(, $($arg)+)?));
}
}
#[macro_export]
macro_rules! println {
($fmt: literal $(, $($arg: tt)+)?) => {
$crate::console::print(format_args!(concat!($fmt, "\n") $(, $($arg)+)?));
}
}

@ -1,10 +0,0 @@
#[panic_handler]
fn panic_handler(panic_info: &core::panic::PanicInfo) -> ! {
let err = panic_info.message().unwrap();
if let Some(location) = panic_info.location() {
println!("Panicked at {}:{}, {}", location.file(), location.line(), err);
} else {
println!("Panicked: {}", err);
}
loop {}
}

@ -1,25 +0,0 @@
#![no_std]
#![feature(llvm_asm)]
#![feature(linkage)]
#![feature(panic_info_message)]
#[macro_use]
pub mod console;
mod syscall;
mod lang_items;
#[no_mangle]
#[link_section = ".text.entry"]
pub extern "C" fn _start() -> ! {
syscall::sys_exit(main());
panic!("unreachable after sys_exit!");
}
#[linkage = "weak"]
#[no_mangle]
fn main() -> i32 {
panic!("Cannot find main!");
}
pub use syscall::*;

@ -1,29 +0,0 @@
OUTPUT_ARCH(riscv)
ENTRY(_start)
BASE_ADDRESS = 0x0;
SECTIONS
{
. = BASE_ADDRESS;
.text : {
*(.text.entry)
*(.text .text.*)
}
. = ALIGN(4K);
.rodata : {
*(.rodata .rodata.*)
}
. = ALIGN(4K);
.data : {
*(.data .data.*)
}
.bss : {
*(.bss .bss.*)
}
/DISCARD/ : {
*(.eh_frame)
*(.debug*)
}
}

@ -1,35 +0,0 @@
pub const STDOUT: usize = 1;
const SYSCALL_WRITE: usize = 64;
const SYSCALL_EXIT: usize = 93;
const SYSCALL_YIELD: usize = 124;
const SYSCALL_GET_TIME: usize = 169;
fn syscall(id: usize, args: [usize; 3]) -> isize {
let mut ret: isize;
unsafe {
llvm_asm!("ecall"
: "={x10}" (ret)
: "{x10}" (args[0]), "{x11}" (args[1]), "{x12}" (args[2]), "{x17}" (id)
: "memory"
: "volatile"
);
}
ret
}
pub fn sys_write(fd: usize, buffer: &[u8]) -> isize {
syscall(SYSCALL_WRITE, [fd, buffer.as_ptr() as usize, buffer.len()])
}
pub fn sys_exit(xstate: i32) -> isize {
syscall(SYSCALL_EXIT, [xstate as usize, 0, 0])
}
pub fn sys_yield() -> isize {
syscall(SYSCALL_YIELD, [0, 0, 0])
}
pub fn sys_get_time() -> isize {
syscall(SYSCALL_GET_TIME, [0, 0, 0])
}

@ -1,7 +0,0 @@
[build]
target = "riscv64gc-unknown-none-elf"
[target.riscv64gc-unknown-none-elf]
rustflags = [
"-Clink-args=-Tsrc/linker.ld",
]

1
ch5/.gitignore vendored

@ -1 +0,0 @@
target/*

@ -1,10 +0,0 @@
[package]
name = "user_lib"
version = "0.1.0"
authors = ["Yifan Wu <shinbokuow@163.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
buddy_system_allocator = "0.6"

@ -1,23 +0,0 @@
TARGET := riscv64gc-unknown-none-elf
MODE := release
APP_DIR := src/bin
TARGET_DIR := target/$(TARGET)/$(MODE)
APPS := $(wildcard $(APP_DIR)/*.rs)
ELFS := $(patsubst $(APP_DIR)/%.rs, $(TARGET_DIR)/%, $(APPS))
BINS := $(patsubst $(APP_DIR)/%.rs, $(TARGET_DIR)/%.bin, $(APPS))
OBJDUMP := rust-objdump --arch-name=riscv64
OBJCOPY := rust-objcopy --binary-architecture=riscv64
elf: $(APPS)
@cargo build --release
binary: elf
$(foreach elf, $(ELFS), $(OBJCOPY) $(elf) --strip-all -O binary $(patsubst $(TARGET_DIR)/%, $(TARGET_DIR)/%.bin, $(elf));)
build: binary
clean:
@cargo clean
.PHONY: elf binary build clean

@ -1,44 +0,0 @@
#![no_std]
#![no_main]
#[macro_use]
extern crate user_lib;
macro_rules! color_text {
($text:expr, $color:expr) => {{
format_args!("\x1b[{}m{}\x1b[0m", $color, $text)
}};
}
#[no_mangle]
pub fn main() -> i32 {
println!(
"{}{}{}{}{} {}{}{}{} {}{}{}{}{}{}",
color_text!("H", 31),
color_text!("e", 32),
color_text!("l", 33),
color_text!("l", 34),
color_text!("o", 35),
color_text!("R", 36),
color_text!("u", 37),
color_text!("s", 90),
color_text!("t", 91),
color_text!("u", 92),
color_text!("C", 93),
color_text!("o", 94),
color_text!("r", 95),
color_text!("e", 96),
color_text!("!", 97),
);
let text =
"reguler \x1b[4munderline\x1b[24m \x1b[7mreverse\x1b[27m \x1b[9mstrikethrough\x1b[29m";
println!("\x1b[47m{}\x1b[0m", color_text!(text, 30));
for i in 31..38 {
println!("{}", color_text!(text, i));
}
for i in 90..98 {
println!("{}", color_text!(text, i));
}
0
}

@ -1,34 +0,0 @@
#![no_std]
#![no_main]
#[macro_use]
extern crate user_lib;
use user_lib::{fork, wait, exit};
const MAX_CHILD: usize = 40;
#[no_mangle]
pub fn main() -> i32 {
for i in 0..MAX_CHILD {
let pid = fork();
if pid == 0 {
println!("I am child {}", i);
exit(0);
} else {
println!("forked child pid = {}", pid);
}
assert!(pid > 0);
}
let mut exit_code: i32 = 0;
for _ in 0..MAX_CHILD {
if wait(&mut exit_code) <= 0 {
panic!("wait stopped early");
}
}
if wait(&mut exit_code) > 0 {
panic!("wait got too many");
}
println!("forktest pass.");
0
}

@ -1,28 +0,0 @@
#![no_std]
#![no_main]
#[macro_use]
extern crate user_lib;
use user_lib::{fork, getpid, wait};
#[no_mangle]
pub fn main() -> i32 {
assert_eq!(wait(&mut 0i32), -1);
println!("sys_wait without child process test passed!");
println!("parent start, pid = {}!", getpid());
let pid = fork();
if pid == 0 {
// child process
println!("hello child process!");
100
} else {
// parent process
let mut xstate: i32 = 0;
println!("ready waiting on parent process!");
assert_eq!(pid, wait(&mut xstate));
assert_eq!(xstate, 100);
println!("child process pid = {}, exit code = {}", pid, xstate);
0
}
}

@ -1,37 +0,0 @@
#![no_std]
#![no_main]
#[macro_use]
extern crate user_lib;
use user_lib::{sleep, getpid, fork, exit, yield_};
const DEPTH: usize = 4;
fn fork_child(cur: &str, branch: char) {
let mut next = [0u8; DEPTH + 1];
let l = cur.len();
if l >= DEPTH {
return;
}
&mut next[..l].copy_from_slice(cur.as_bytes());
next[l] = branch as u8;
if fork() == 0 {
fork_tree(core::str::from_utf8(&next[..l + 1]).unwrap());
yield_();
exit(0);
}
}
fn fork_tree(cur: &str) {
println!("pid{}: {}", getpid(), cur);
fork_child(cur, '0');
fork_child(cur, '1');
}
#[no_mangle]
pub fn main() -> i32 {
fork_tree("");
sleep(3000);
0
}

@ -1,68 +0,0 @@
#![no_std]
#![no_main]
#[macro_use]
extern crate user_lib;
use user_lib::{fork, wait, yield_, exit, getpid, get_time};
static NUM: usize = 35;
const N: usize = 10;
static P: i32 = 10007;
type Arr = [[i32; N]; N];
fn work(times: isize) {
let mut a: Arr = Default::default();
let mut b: Arr = Default::default();
let mut c: Arr = Default::default();
for i in 0..N {
for j in 0..N {
a[i][j] = 1;
b[i][j] = 1;
}
}
yield_();
println!("pid {} is running ({} times)!.", getpid(), times);
for _ in 0..times {
for i in 0..N {
for j in 0..N {
c[i][j] = 0;
for k in 0..N {
c[i][j] = (c[i][j] + a[i][k] * b[k][j]) % P;
}
}
}
for i in 0..N {
for j in 0..N {
a[i][j] = c[i][j];
b[i][j] = c[i][j];
}
}
}
println!("pid {} done!.", getpid());
exit(0);
}
#[no_mangle]
pub fn main() -> i32 {
for _ in 0..NUM {
let pid = fork();
if pid == 0 {
let current_time = get_time();
let times = (current_time as i32 as isize) * (current_time as i32 as isize) % 1000;
work(times * 10);
}
}
println!("fork ok.");
let mut xstate: i32 = 0;
for _ in 0..NUM {
if wait(&mut xstate) < 0 {
panic!("wait failed.");
}
}
assert!(wait(&mut xstate) < 0);
println!("matrix passed.");
0
}

@ -1,30 +0,0 @@
#![no_std]
#![no_main]
#[macro_use]
extern crate user_lib;
use user_lib::{sleep, exit, get_time, fork, waitpid};
fn sleepy() {
let time: usize = 100;
for i in 0..5 {
sleep(time);
println!("sleep {} x {} msecs.", i + 1, time);
}
exit(0);
}
#[no_mangle]
pub fn main() -> i32 {
let current_time = get_time();
let pid = fork();
let mut xstate: i32 = 0;
if pid == 0 {
sleepy();
}
assert!(waitpid(pid as usize, &mut xstate) == pid && xstate == 0);
println!("use {} msecs.", get_time() - current_time);
println!("sleep pass.");
0
}

@ -1,17 +0,0 @@
#![no_std]
#![no_main]
#[macro_use]
extern crate user_lib;
fn f(d: usize) {
println!("d = {}",d);
f(d + 1);
}
#[no_mangle]
pub fn main() -> i32 {
println!("It should trigger segmentation fault!");
f(0);
0
}

@ -1,70 +0,0 @@
#![no_std]
#![no_main]
extern crate alloc;
#[macro_use]
extern crate user_lib;
const LF: u8 = 0x0au8;
const CR: u8 = 0x0du8;
const DL: u8 = 0x7fu8;
const BS: u8 = 0x08u8;
use alloc::string::String;
use user_lib::{fork, exec, waitpid, yield_};
use user_lib::console::getchar;
#[no_mangle]
pub fn main() -> i32 {
println!("Rust user shell");
let mut line: String = String::new();
print!(">> ");
loop {
let c = getchar();
match c {
LF | CR => {
println!("");
if !line.is_empty() {
line.push('\0');
let pid = fork();
if pid == 0 {
// child process
if exec(line.as_str()) == -1 {
println!("Error when executing!");
return -4;
}
unreachable!();
} else {
let mut xstate: i32 = 0;
let mut exit_pid: isize;
loop {
exit_pid = waitpid(pid as usize, &mut xstate);
if exit_pid == -1 {
yield_();
} else {
assert_eq!(pid, exit_pid);
println!("Shell: Process {} exited with code {}", pid, xstate);
break;
}
}
}
line.clear();
}
print!(">> ");
}
BS | DL => {
if !line.is_empty() {
print!("{}", BS as char);
print!(" ");
print!("{}", BS as char);
line.pop();
}
}
_ => {
print!("{}", c as char);
line.push(c as char);
}
}
}
}

@ -1,40 +0,0 @@
#![no_std]
#![no_main]
#[macro_use]
extern crate user_lib;
static TESTS: &[&str] = &[
"exit\0",
"fantastic_text\0",
"forktest\0",
"forktest2\0",
"forktest_simple\0",
"hello_world\0",
"matrix\0",
"sleep\0",
"sleep_simple\0",
"stack_overflow\0",
"yield\0",
];
use user_lib::{exec, fork, waitpid};
#[no_mangle]
pub fn main() -> i32 {
for test in TESTS {
println!("Usertests: Running {}", test);
let pid = fork();
if pid == 0 {
exec(*test);
panic!("unreachable!");
} else {
let mut xstate: i32 = Default::default();
let wait_pid = waitpid(pid as usize, &mut xstate);
assert_eq!(pid, wait_pid);
println!("\x1b[32mUsertests: Test {} in Process {} exited with code {}\x1b[0m", test, pid, xstate);
}
}
println!("Usertests passed!");
0
}

@ -1,17 +0,0 @@
#![no_std]
#![no_main]
#[macro_use]
extern crate user_lib;
use user_lib::{getpid, yield_};
#[no_mangle]
pub fn main() -> i32 {
println!("Hello, I am process {}.", getpid());
for i in 0..5 {
yield_();
println!("Back in process {}, iteration {}.", getpid(), i);
}
println!("yield pass.");
0
}

@ -1,10 +0,0 @@
#[panic_handler]
fn panic_handler(panic_info: &core::panic::PanicInfo) -> ! {
let err = panic_info.message().unwrap();
if let Some(location) = panic_info.location() {
println!("Panicked at {}:{}, {}", location.file(), location.line(), err);
} else {
println!("Panicked: {}", err);
}
loop {}
}

@ -1,75 +0,0 @@
#![no_std]
#![feature(llvm_asm)]
#![feature(linkage)]
#![feature(panic_info_message)]
#![feature(alloc_error_handler)]
#[macro_use]
pub mod console;
mod syscall;
mod lang_items;
use syscall::*;
use buddy_system_allocator::LockedHeap;
const USER_HEAP_SIZE: usize = 16384;
static mut HEAP_SPACE: [u8; USER_HEAP_SIZE] = [0; USER_HEAP_SIZE];
#[global_allocator]
static HEAP: LockedHeap = LockedHeap::empty();
#[alloc_error_handler]
pub fn handle_alloc_error(layout: core::alloc::Layout) -> ! {
panic!("Heap allocation error, layout = {:?}", layout);
}
#[no_mangle]
#[link_section = ".text.entry"]
pub extern "C" fn _start() -> ! {
unsafe {
HEAP.lock()
.init(HEAP_SPACE.as_ptr() as usize, USER_HEAP_SIZE);
}
exit(main());
}
#[linkage = "weak"]
#[no_mangle]
fn main() -> i32 {
panic!("Cannot find main!");
}
pub fn read(fd: usize, buf: &mut [u8]) -> isize { sys_read(fd, buf) }
pub fn write(fd: usize, buf: &[u8]) -> isize { sys_write(fd, buf) }
pub fn exit(exit_code: i32) -> ! { sys_exit(exit_code); }
pub fn yield_() -> isize { sys_yield() }
pub fn get_time() -> isize { sys_get_time() }
pub fn getpid() -> isize { sys_getpid() }
pub fn fork() -> isize { sys_fork() }
pub fn exec(path: &str) -> isize { sys_exec(path) }
pub fn wait(exit_code: &mut i32) -> isize {
loop {
match sys_waitpid(-1, exit_code as *mut _) {
-2 => { yield_(); }
// -1 or a real pid
exit_pid => return exit_pid,
}
}
}
pub fn waitpid(pid: usize, exit_code: &mut i32) -> isize {
loop {
match sys_waitpid(pid as isize, exit_code as *mut _) {
-2 => { yield_(); }
// -1 or a real pid
exit_pid => return exit_pid,
}
}
}
pub fn sleep(period_ms: usize) {
let start = sys_get_time();
while sys_get_time() < start + period_ms as isize {
sys_yield();
}
}

@ -1,29 +0,0 @@
OUTPUT_ARCH(riscv)
ENTRY(_start)
BASE_ADDRESS = 0x0;
SECTIONS
{
. = BASE_ADDRESS;
.text : {
*(.text.entry)
*(.text .text.*)
}
. = ALIGN(4K);
.rodata : {
*(.rodata .rodata.*)
}
. = ALIGN(4K);
.data : {
*(.data .data.*)
}
.bss : {
*(.bss .bss.*)
}
/DISCARD/ : {
*(.eh_frame)
*(.debug*)
}
}

@ -1,59 +0,0 @@
const SYSCALL_READ: usize = 63;
const SYSCALL_WRITE: usize = 64;
const SYSCALL_EXIT: usize = 93;
const SYSCALL_YIELD: usize = 124;
const SYSCALL_GET_TIME: usize = 169;
const SYSCALL_GETPID: usize = 172;
const SYSCALL_FORK: usize = 220;
const SYSCALL_EXEC: usize = 221;
const SYSCALL_WAITPID: usize = 260;
fn syscall(id: usize, args: [usize; 3]) -> isize {
let mut ret: isize;
unsafe {
llvm_asm!("ecall"
: "={x10}" (ret)
: "{x10}" (args[0]), "{x11}" (args[1]), "{x12}" (args[2]), "{x17}" (id)
: "memory"
: "volatile"
);
}
ret
}
pub fn sys_read(fd: usize, buffer: &mut [u8]) -> isize {
syscall(SYSCALL_READ, [fd, buffer.as_mut_ptr() as usize, buffer.len()])
}
pub fn sys_write(fd: usize, buffer: &[u8]) -> isize {
syscall(SYSCALL_WRITE, [fd, buffer.as_ptr() as usize, buffer.len()])
}
pub fn sys_exit(exit_code: i32) -> ! {
syscall(SYSCALL_EXIT, [exit_code as usize, 0, 0]);
panic!("sys_exit never returns!");
}
pub fn sys_yield() -> isize {
syscall(SYSCALL_YIELD, [0, 0, 0])
}
pub fn sys_get_time() -> isize {
syscall(SYSCALL_GET_TIME, [0, 0, 0])
}
pub fn sys_getpid() -> isize {
syscall(SYSCALL_GETPID, [0, 0, 0])
}
pub fn sys_fork() -> isize {
syscall(SYSCALL_FORK, [0, 0, 0])
}
pub fn sys_exec(path: &str) -> isize {
syscall(SYSCALL_EXEC, [path.as_ptr() as usize, 0, 0])
}
pub fn sys_waitpid(pid: isize, xstatus: *mut i32) -> isize {
syscall(SYSCALL_WAITPID, [pid as usize, xstatus as usize, 0])
}

@ -1,7 +0,0 @@
[build]
target = "riscv64gc-unknown-none-elf"
[target.riscv64gc-unknown-none-elf]
rustflags = [
"-Clink-args=-Tsrc/linker.ld",
]

1
ch6/.gitignore vendored

@ -1 +0,0 @@
target/*

@ -1,10 +0,0 @@
[package]
name = "user_lib"
version = "0.1.0"
authors = ["Yifan Wu <shinbokuow@163.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
buddy_system_allocator = "0.6"

@ -1,23 +0,0 @@
TARGET := riscv64gc-unknown-none-elf
MODE := release
APP_DIR := src/bin
TARGET_DIR := target/$(TARGET)/$(MODE)
APPS := $(wildcard $(APP_DIR)/*.rs)
ELFS := $(patsubst $(APP_DIR)/%.rs, $(TARGET_DIR)/%, $(APPS))
BINS := $(patsubst $(APP_DIR)/%.rs, $(TARGET_DIR)/%.bin, $(APPS))
OBJDUMP := rust-objdump --arch-name=riscv64
OBJCOPY := rust-objcopy --binary-architecture=riscv64
elf: $(APPS)
@cargo build --release
binary: elf
$(foreach elf, $(ELFS), $(OBJCOPY) $(elf) --strip-all -O binary $(patsubst $(TARGET_DIR)/%, $(TARGET_DIR)/%.bin, $(elf));)
build: binary
clean:
@cargo clean
.PHONY: elf binary build clean

@ -1,29 +0,0 @@
#![no_std]
#![no_main]
#[macro_use]
extern crate user_lib;
use user_lib::{fork, yield_, waitpid, exit, wait};
const MAGIC: i32 = -0x10384;
#[no_mangle]
pub fn main() -> i32 {
println!("I am the parent. Forking the child...");
let pid = fork();
if pid == 0 {
println!("I am the child.");
for _ in 0..7 { yield_(); }
exit(MAGIC);
} else {
println!("I am parent, fork a child pid {}", pid);
}
println!("I am the parent, waiting now..");
let mut xstate: i32 = 0;
assert!(waitpid(pid as usize, &mut xstate) == pid && xstate == MAGIC);
assert!(waitpid(pid as usize, &mut xstate) < 0 && wait(&mut xstate) <= 0);
println!("waitpid {} ok.", pid);
println!("exit pass.");
0
}

@ -1,33 +0,0 @@
#![no_std]
#![no_main]
#[macro_use]
extern crate user_lib;
use user_lib::{fork, wait, getpid, exit, sleep, get_time};
static NUM: usize = 30;
#[no_mangle]
pub fn main() -> i32 {
for _ in 0..NUM {
let pid = fork();
if pid == 0 {
let current_time = get_time();
let sleep_length = (current_time as i32 as isize) * (current_time as i32 as isize) % 1000 + 1000;
println!("pid {} sleep for {} ms", getpid(), sleep_length);
sleep(sleep_length as usize);
println!("pid {} OK!", getpid());
exit(0);
}
}
let mut xstate: i32 = 0;
for _ in 0..NUM {
assert!(wait(&mut xstate) > 0);
assert_eq!(xstate, 0);
}
assert!(wait(&mut xstate) < 0);
println!("forktest2 test passed!");
0
}

@ -1,11 +0,0 @@
#![no_std]
#![no_main]
#[macro_use]
extern crate user_lib;
#[no_mangle]
pub fn main() -> i32 {
println!("Hello world from user mode program!");
0
}

@ -1,34 +0,0 @@
#![no_std]
#![no_main]
#[macro_use]
extern crate user_lib;
use user_lib::{
fork,
wait,
exec,
yield_,
};
#[no_mangle]
fn main() -> i32 {
if fork() == 0 {
exec("user_shell\0");
} else {
loop {
let mut exit_code: i32 = 0;
let pid = wait(&mut exit_code);
if pid == -1 {
yield_();
continue;
}
println!(
"[initproc] Released a zombie process, pid={}, exit_code={}",
pid,
exit_code,
);
}
}
0
}

@ -1,19 +0,0 @@
#![no_std]
#![no_main]
#[macro_use]
extern crate user_lib;
use user_lib::{get_time, sleep};
#[no_mangle]
pub fn main() -> i32 {
println!("into sleep test!");
let start = get_time();
println!("current time_msec = {}", start);
sleep(100);
let end = get_time();
println!("time_msec = {} after sleeping 100 ticks, delta = {}ms!", end, end - start);
println!("r_sleep passed!");
0
}

@ -1,70 +0,0 @@
#![no_std]
#![no_main]
extern crate alloc;
#[macro_use]
extern crate user_lib;
const LF: u8 = 0x0au8;
const CR: u8 = 0x0du8;
const DL: u8 = 0x7fu8;
const BS: u8 = 0x08u8;
use alloc::string::String;
use user_lib::{fork, exec, waitpid, yield_};
use user_lib::console::getchar;
#[no_mangle]
pub fn main() -> i32 {
println!("Rust user shell");
let mut line: String = String::new();
print!(">> ");
loop {
let c = getchar();
match c {
LF | CR => {
println!("");
if !line.is_empty() {
line.push('\0');
let pid = fork();
if pid == 0 {
// child process
if exec(line.as_str()) == -1 {
println!("Error when executing!");
return -4;
}
unreachable!();
} else {
let mut xstate: i32 = 0;
let mut exit_pid: isize;
loop {
exit_pid = waitpid(pid as usize, &mut xstate);
if exit_pid == -1 {
yield_();
} else {
assert_eq!(pid, exit_pid);
println!("Shell: Process {} exited with code {}", pid, xstate);
break;
}
}
}
line.clear();
}
print!(">> ");
}
BS | DL => {
if !line.is_empty() {
print!("{}", BS as char);
print!(" ");
print!("{}", BS as char);
line.pop();
}
}
_ => {
print!("{}", c as char);
line.push(c as char);
}
}
}
}

@ -1,40 +0,0 @@
#![no_std]
#![no_main]
#[macro_use]
extern crate user_lib;
static TESTS: &[&str] = &[
"exit\0",
"fantastic_text\0",
"forktest\0",
"forktest2\0",
"forktest_simple\0",
"hello_world\0",
"matrix\0",
"sleep\0",
"sleep_simple\0",
"stack_overflow\0",
"yield\0",
];
use user_lib::{exec, fork, waitpid};
#[no_mangle]
pub fn main() -> i32 {
for test in TESTS {
println!("Usertests: Running {}", test);
let pid = fork();
if pid == 0 {
exec(*test);
panic!("unreachable!");
} else {
let mut xstate: i32 = Default::default();
let wait_pid = waitpid(pid as usize, &mut xstate);
assert_eq!(pid, wait_pid);
println!("\x1b[32mUsertests: Test {} in Process {} exited with code {}\x1b[0m", test, pid, xstate);
}
}
println!("Usertests passed!");
0
}

@ -1,39 +0,0 @@
use core::fmt::{self, Write};
const STDIN: usize = 0;
const STDOUT: usize = 1;
use super::{read, write};
struct Stdout;
impl Write for Stdout {
fn write_str(&mut self, s: &str) -> fmt::Result {
write(STDOUT, s.as_bytes());
Ok(())
}
}
pub fn print(args: fmt::Arguments) {
Stdout.write_fmt(args).unwrap();
}
#[macro_export]
macro_rules! print {
($fmt: literal $(, $($arg: tt)+)?) => {
$crate::console::print(format_args!($fmt $(, $($arg)+)?));
}
}
#[macro_export]
macro_rules! println {
($fmt: literal $(, $($arg: tt)+)?) => {
$crate::console::print(format_args!(concat!($fmt, "\n") $(, $($arg)+)?));
}
}
pub fn getchar() -> u8 {
let mut c = [0u8; 1];
read(STDIN, &mut c);
c[0]
}

@ -1,12 +0,0 @@
use super::exit;
#[panic_handler]
fn panic_handler(panic_info: &core::panic::PanicInfo) -> ! {
let err = panic_info.message().unwrap();
if let Some(location) = panic_info.location() {
println!("Panicked at {}:{}, {}", location.file(), location.line(), err);
} else {
println!("Panicked: {}", err);
}
exit(-1);
}

@ -1,79 +0,0 @@
#![no_std]
#![feature(llvm_asm)]
#![feature(linkage)]
#![feature(panic_info_message)]
#![feature(alloc_error_handler)]
#[macro_use]
pub mod console;
mod syscall;
mod lang_items;
extern crate alloc;
use syscall::*;
use buddy_system_allocator::LockedHeap;
const USER_HEAP_SIZE: usize = 16384;
static mut HEAP_SPACE: [u8; USER_HEAP_SIZE] = [0; USER_HEAP_SIZE];
#[global_allocator]
static HEAP: LockedHeap = LockedHeap::empty();
#[alloc_error_handler]
pub fn handle_alloc_error(layout: core::alloc::Layout) -> ! {
panic!("Heap allocation error, layout = {:?}", layout);
}
#[no_mangle]
#[link_section = ".text.entry"]
pub extern "C" fn _start() -> ! {
unsafe {
HEAP.lock()
.init(HEAP_SPACE.as_ptr() as usize, USER_HEAP_SIZE);
}
exit(main());
}
#[linkage = "weak"]
#[no_mangle]
fn main() -> i32 {
panic!("Cannot find main!");
}
pub fn close(fd: usize) -> isize { sys_close(fd) }
pub fn pipe(pipe_fd: &mut [usize]) -> isize { sys_pipe(pipe_fd) }
pub fn read(fd: usize, buf: &mut [u8]) -> isize { sys_read(fd, buf) }
pub fn write(fd: usize, buf: &[u8]) -> isize { sys_write(fd, buf) }
pub fn exit(exit_code: i32) -> ! { sys_exit(exit_code); }
pub fn yield_() -> isize { sys_yield() }
pub fn get_time() -> isize { sys_get_time() }
pub fn getpid() -> isize { sys_getpid() }
pub fn fork() -> isize { sys_fork() }
pub fn exec(path: &str) -> isize { sys_exec(path) }
pub fn wait(exit_code: &mut i32) -> isize {
loop {
match sys_waitpid(-1, exit_code as *mut _) {
-2 => { yield_(); }
// -1 or a real pid
exit_pid => return exit_pid,
}
}
}
pub fn waitpid(pid: usize, exit_code: &mut i32) -> isize {
loop {
match sys_waitpid(pid as isize, exit_code as *mut _) {
-2 => { yield_(); }
// -1 or a real pid
exit_pid => return exit_pid,
}
}
}
pub fn sleep(period_ms: usize) {
let start = sys_get_time();
while sys_get_time() < start + period_ms as isize {
sys_yield();
}
}

@ -1,69 +0,0 @@
const SYSCALL_CLOSE: usize = 57;
const SYSCALL_PIPE: usize = 59;
const SYSCALL_READ: usize = 63;
const SYSCALL_WRITE: usize = 64;
const SYSCALL_EXIT: usize = 93;
const SYSCALL_YIELD: usize = 124;
const SYSCALL_GET_TIME: usize = 169;
const SYSCALL_GETPID: usize = 172;
const SYSCALL_FORK: usize = 220;
const SYSCALL_EXEC: usize = 221;
const SYSCALL_WAITPID: usize = 260;
fn syscall(id: usize, args: [usize; 3]) -> isize {
let mut ret: isize;
unsafe {
llvm_asm!("ecall"
: "={x10}" (ret)
: "{x10}" (args[0]), "{x11}" (args[1]), "{x12}" (args[2]), "{x17}" (id)
: "memory"
: "volatile"
);
}
ret
}
pub fn sys_close(fd: usize) -> isize {
syscall(SYSCALL_CLOSE, [fd, 0, 0])
}
pub fn sys_pipe(pipe: &mut [usize]) -> isize {
syscall(SYSCALL_PIPE, [pipe.as_mut_ptr() as usize, 0, 0])
}
pub fn sys_read(fd: usize, buffer: &mut [u8]) -> isize {
syscall(SYSCALL_READ, [fd, buffer.as_mut_ptr() as usize, buffer.len()])
}
pub fn sys_write(fd: usize, buffer: &[u8]) -> isize {
syscall(SYSCALL_WRITE, [fd, buffer.as_ptr() as usize, buffer.len()])
}
pub fn sys_exit(exit_code: i32) -> ! {
syscall(SYSCALL_EXIT, [exit_code as usize, 0, 0]);
panic!("sys_exit never returns!");
}
pub fn sys_yield() -> isize {
syscall(SYSCALL_YIELD, [0, 0, 0])
}
pub fn sys_get_time() -> isize {
syscall(SYSCALL_GET_TIME, [0, 0, 0])
}
pub fn sys_getpid() -> isize {
syscall(SYSCALL_GETPID, [0, 0, 0])
}
pub fn sys_fork() -> isize {
syscall(SYSCALL_FORK, [0, 0, 0])
}
pub fn sys_exec(path: &str) -> isize {
syscall(SYSCALL_EXEC, [path.as_ptr() as usize, 0, 0])
}
pub fn sys_waitpid(pid: isize, xstatus: *mut i32) -> isize {
syscall(SYSCALL_WAITPID, [pid as usize, xstatus as usize, 0])
}

@ -0,0 +1,18 @@
# rCore-Tutorial-v3
rCore-Tutorial version 3.x
## Dependency
### Binaries
* rustc: 1.57.0-nightly (e1e9319d9 2021-10-14)
* cargo-binutils: 0.3.3
* qemu: 5.0.0
* rustsbi-lib: 0.2.0-alpha.4
rustsbi-qemu: d4968dd2
rustsbi-k210: b689314e

@ -1,5 +1,5 @@
[package]
name = "user_lib"
name = "easy-fs-fuse"
version = "0.1.0"
authors = ["Yifan Wu <shinbokuow@163.com>"]
edition = "2018"
@ -7,3 +7,6 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
clap = "2.33.3"
easy-fs = { path = "../easy-fs" }
rand = "0.8.0"

@ -0,0 +1,153 @@
use clap::{App, Arg};
use easy_fs::{BlockDevice, EasyFileSystem};
use std::fs::{read_dir, File, OpenOptions};
use std::io::{Read, Seek, SeekFrom, Write};
use std::sync::Arc;
use std::sync::Mutex;
const BLOCK_SZ: usize = 512;
struct BlockFile(Mutex<File>);
impl BlockDevice for BlockFile {
fn read_block(&self, block_id: usize, buf: &mut [u8]) {
let mut file = self.0.lock().unwrap();
file.seek(SeekFrom::Start((block_id * BLOCK_SZ) as u64))
.expect("Error when seeking!");
assert_eq!(file.read(buf).unwrap(), BLOCK_SZ, "Not a complete block!");
}
fn write_block(&self, block_id: usize, buf: &[u8]) {
let mut file = self.0.lock().unwrap();
file.seek(SeekFrom::Start((block_id * BLOCK_SZ) as u64))
.expect("Error when seeking!");
assert_eq!(file.write(buf).unwrap(), BLOCK_SZ, "Not a complete block!");
}
fn handle_irq(&self) { unimplemented!(); }
}
fn main() {
easy_fs_pack().expect("Error when packing easy-fs!");
}
fn easy_fs_pack() -> std::io::Result<()> {
let matches = App::new("EasyFileSystem packer")
.arg(
Arg::with_name("source")
.short("s")
.long("source")
.takes_value(true)
.help("Executable source dir(with backslash)"),
)
.arg(
Arg::with_name("target")
.short("t")
.long("target")
.takes_value(true)
.help("Executable target dir(with backslash)"),
)
.get_matches();
let src_path = matches.value_of("source").unwrap();
let target_path = matches.value_of("target").unwrap();
println!("src_path = {}\ntarget_path = {}", src_path, target_path);
let block_file = Arc::new(BlockFile(Mutex::new({
let f = OpenOptions::new()
.read(true)
.write(true)
.create(true)
.open(format!("{}{}", target_path, "fs.img"))?;
f.set_len(16 * 2048 * 512).unwrap();
f
})));
// 16MiB, at most 4095 files
let efs = EasyFileSystem::create(block_file, 16 * 2048, 1);
let root_inode = Arc::new(EasyFileSystem::root_inode(&efs));
let apps: Vec<_> = read_dir(src_path)
.unwrap()
.into_iter()
.map(|dir_entry| {
let mut name_with_ext = dir_entry.unwrap().file_name().into_string().unwrap();
name_with_ext.drain(name_with_ext.find('.').unwrap()..name_with_ext.len());
name_with_ext
})
.collect();
for app in apps {
// load app data from host file system
let mut host_file = File::open(format!("{}{}", target_path, app)).unwrap();
let mut all_data: Vec<u8> = Vec::new();
host_file.read_to_end(&mut all_data).unwrap();
// create a file in easy-fs
let inode = root_inode.create(app.as_str()).unwrap();
// write data to easy-fs
inode.write_at(0, all_data.as_slice());
}
// list apps
for app in root_inode.ls() {
println!("{}", app);
}
Ok(())
}
#[test]
fn efs_test() -> std::io::Result<()> {
let block_file = Arc::new(BlockFile(Mutex::new({
let f = OpenOptions::new()
.read(true)
.write(true)
.create(true)
.open("target/fs.img")?;
f.set_len(8192 * 512).unwrap();
f
})));
EasyFileSystem::create(block_file.clone(), 4096, 1);
let efs = EasyFileSystem::open(block_file.clone());
let root_inode = EasyFileSystem::root_inode(&efs);
root_inode.create("filea");
root_inode.create("fileb");
for name in root_inode.ls() {
println!("{}", name);
}
let filea = root_inode.find("filea").unwrap();
let greet_str = "Hello, world!";
filea.write_at(0, greet_str.as_bytes());
//let mut buffer = [0u8; 512];
let mut buffer = [0u8; 233];
let len = filea.read_at(0, &mut buffer);
assert_eq!(greet_str, core::str::from_utf8(&buffer[..len]).unwrap(),);
let mut random_str_test = |len: usize| {
filea.clear();
assert_eq!(filea.read_at(0, &mut buffer), 0,);
let mut str = String::new();
use rand;
// random digit
for _ in 0..len {
str.push(char::from('0' as u8 + rand::random::<u8>() % 10));
}
filea.write_at(0, str.as_bytes());
let mut read_buffer = [0u8; 127];
let mut offset = 0usize;
let mut read_str = String::new();
loop {
let len = filea.read_at(offset, &mut read_buffer);
if len == 0 {
break;
}
offset += len;
read_str.push_str(core::str::from_utf8(&read_buffer[..len]).unwrap());
}
assert_eq!(str, read_str);
};
random_str_test(4 * BLOCK_SZ);
random_str_test(8 * BLOCK_SZ + BLOCK_SZ / 2);
random_str_test(100 * BLOCK_SZ);
random_str_test(70 * BLOCK_SZ + BLOCK_SZ / 7);
random_str_test((12 + 128) * BLOCK_SZ);
random_str_test(400 * BLOCK_SZ);
random_str_test(1000 * BLOCK_SZ);
random_str_test(2000 * BLOCK_SZ);
Ok(())
}

@ -0,0 +1,3 @@
.idea/
target/
Cargo.lock

@ -1,5 +1,5 @@
[package]
name = "user_lib"
name = "easy-fs"
version = "0.1.0"
authors = ["Yifan Wu <shinbokuow@163.com>"]
edition = "2018"
@ -7,3 +7,8 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
spin = "0.7.0"
lazy_static = { version = "1.4.0", features = ["spin_no_std"] }
[profile.release]
debug = true

@ -0,0 +1,69 @@
use super::{get_block_cache, BlockDevice, BLOCK_SZ};
use alloc::sync::Arc;
type BitmapBlock = [u64; 64];
const BLOCK_BITS: usize = BLOCK_SZ * 8;
pub struct Bitmap {
start_block_id: usize,
blocks: usize,
}
/// Return (block_pos, bits64_pos, inner_pos)
fn decomposition(mut bit: usize) -> (usize, usize, usize) {
let block_pos = bit / BLOCK_BITS;
bit %= BLOCK_BITS;
(block_pos, bit / 64, bit % 64)
}
impl Bitmap {
pub fn new(start_block_id: usize, blocks: usize) -> Self {
Self {
start_block_id,
blocks,
}
}
pub fn alloc(&self, block_device: &Arc<dyn BlockDevice>) -> Option<usize> {
for block_id in 0..self.blocks {
let pos = get_block_cache(
block_id + self.start_block_id as usize,
Arc::clone(block_device),
)
.lock()
.modify(0, |bitmap_block: &mut BitmapBlock| {
if let Some((bits64_pos, inner_pos)) = bitmap_block
.iter()
.enumerate()
.find(|(_, bits64)| **bits64 != u64::MAX)
.map(|(bits64_pos, bits64)| (bits64_pos, bits64.trailing_ones() as usize))
{
// modify cache
bitmap_block[bits64_pos] |= 1u64 << inner_pos;
Some(block_id * BLOCK_BITS + bits64_pos * 64 + inner_pos as usize)
} else {
None
}
});
if pos.is_some() {
return pos;
}
}
None
}
pub fn dealloc(&self, block_device: &Arc<dyn BlockDevice>, bit: usize) {
let (block_pos, bits64_pos, inner_pos) = decomposition(bit);
get_block_cache(block_pos + self.start_block_id, Arc::clone(block_device))
.lock()
.modify(0, |bitmap_block: &mut BitmapBlock| {
assert!(bitmap_block[bits64_pos] & (1u64 << inner_pos) > 0);
bitmap_block[bits64_pos] -= 1u64 << inner_pos;
});
}
pub fn maximum(&self) -> usize {
self.blocks * BLOCK_BITS
}
}

@ -0,0 +1,139 @@
use super::{BlockDevice, BLOCK_SZ};
use alloc::collections::VecDeque;
use alloc::sync::Arc;
use lazy_static::*;
use spin::Mutex;
pub struct BlockCache {
cache: [u8; BLOCK_SZ],
block_id: usize,
block_device: Arc<dyn BlockDevice>,
modified: bool,
}
impl BlockCache {
/// Load a new BlockCache from disk.
pub fn new(block_id: usize, block_device: Arc<dyn BlockDevice>) -> Self {
let mut cache = [0u8; BLOCK_SZ];
block_device.read_block(block_id, &mut cache);
Self {
cache,
block_id,
block_device,
modified: false,
}
}
fn addr_of_offset(&self, offset: usize) -> usize {
&self.cache[offset] as *const _ as usize
}
pub fn get_ref<T>(&self, offset: usize) -> &T
where
T: Sized,
{
let type_size = core::mem::size_of::<T>();
assert!(offset + type_size <= BLOCK_SZ);
let addr = self.addr_of_offset(offset);
unsafe { &*(addr as *const T) }
}
pub fn get_mut<T>(&mut self, offset: usize) -> &mut T
where
T: Sized,
{
let type_size = core::mem::size_of::<T>();
assert!(offset + type_size <= BLOCK_SZ);
self.modified = true;
let addr = self.addr_of_offset(offset);
unsafe { &mut *(addr as *mut T) }
}
pub fn read<T, V>(&self, offset: usize, f: impl FnOnce(&T) -> V) -> V {
f(self.get_ref(offset))
}
pub fn modify<T, V>(&mut self, offset: usize, f: impl FnOnce(&mut T) -> V) -> V {
f(self.get_mut(offset))
}
pub fn sync(&mut self) {
if self.modified {
self.modified = false;
self.block_device.write_block(self.block_id, &self.cache);
}
}
}
impl Drop for BlockCache {
fn drop(&mut self) {
self.sync()
}
}
const BLOCK_CACHE_SIZE: usize = 16;
pub struct BlockCacheManager {
queue: VecDeque<(usize, Arc<Mutex<BlockCache>>)>,
}
impl BlockCacheManager {
pub fn new() -> Self {
Self {
queue: VecDeque::new(),
}
}
pub fn get_block_cache(
&mut self,
block_id: usize,
block_device: Arc<dyn BlockDevice>,
) -> Arc<Mutex<BlockCache>> {
if let Some(pair) = self.queue.iter().find(|pair| pair.0 == block_id) {
Arc::clone(&pair.1)
} else {
// substitute
if self.queue.len() == BLOCK_CACHE_SIZE {
// from front to tail
if let Some((idx, _)) = self
.queue
.iter()
.enumerate()
.find(|(_, pair)| Arc::strong_count(&pair.1) == 1)
{
self.queue.drain(idx..=idx);
} else {
panic!("Run out of BlockCache!");
}
}
// load block into mem and push back
let block_cache = Arc::new(Mutex::new(BlockCache::new(
block_id,
Arc::clone(&block_device),
)));
self.queue.push_back((block_id, Arc::clone(&block_cache)));
block_cache
}
}
}
lazy_static! {
pub static ref BLOCK_CACHE_MANAGER: Mutex<BlockCacheManager> =
Mutex::new(BlockCacheManager::new());
}
pub fn get_block_cache(
block_id: usize,
block_device: Arc<dyn BlockDevice>,
) -> Arc<Mutex<BlockCache>> {
BLOCK_CACHE_MANAGER
.lock()
.get_block_cache(block_id, block_device)
}
pub fn block_cache_sync_all() {
let manager = BLOCK_CACHE_MANAGER.lock();
for (_, cache) in manager.queue.iter() {
cache.lock().sync();
}
}

@ -0,0 +1,7 @@
use core::any::Any;
pub trait BlockDevice: Send + Sync + Any {
fn read_block(&self, block_id: usize, buf: &mut [u8]);
fn write_block(&self, block_id: usize, buf: &[u8]);
fn handle_irq(&self);
}

@ -0,0 +1,147 @@
use super::{
block_cache_sync_all, get_block_cache, Bitmap, BlockDevice, DiskInode, DiskInodeType, Inode,
SuperBlock,
};
use crate::BLOCK_SZ;
use alloc::sync::Arc;
use spin::Mutex;
pub struct EasyFileSystem {
pub block_device: Arc<dyn BlockDevice>,
pub inode_bitmap: Bitmap,
pub data_bitmap: Bitmap,
inode_area_start_block: u32,
data_area_start_block: u32,
}
type DataBlock = [u8; BLOCK_SZ];
impl EasyFileSystem {
pub fn create(
block_device: Arc<dyn BlockDevice>,
total_blocks: u32,
inode_bitmap_blocks: u32,
) -> Arc<Mutex<Self>> {
// calculate block size of areas & create bitmaps
let inode_bitmap = Bitmap::new(1, inode_bitmap_blocks as usize);
let inode_num = inode_bitmap.maximum();
let inode_area_blocks =
((inode_num * core::mem::size_of::<DiskInode>() + BLOCK_SZ - 1) / BLOCK_SZ) as u32;
let inode_total_blocks = inode_bitmap_blocks + inode_area_blocks;
let data_total_blocks = total_blocks - 1 - inode_total_blocks;
let data_bitmap_blocks = (data_total_blocks + 4096) / 4097;
let data_area_blocks = data_total_blocks - data_bitmap_blocks;
let data_bitmap = Bitmap::new(
(1 + inode_bitmap_blocks + inode_area_blocks) as usize,
data_bitmap_blocks as usize,
);
let mut efs = Self {
block_device: Arc::clone(&block_device),
inode_bitmap,
data_bitmap,
inode_area_start_block: 1 + inode_bitmap_blocks,
data_area_start_block: 1 + inode_total_blocks + data_bitmap_blocks,
};
// clear all blocks
for i in 0..total_blocks {
get_block_cache(i as usize, Arc::clone(&block_device))
.lock()
.modify(0, |data_block: &mut DataBlock| {
for byte in data_block.iter_mut() {
*byte = 0;
}
});
}
// initialize SuperBlock
get_block_cache(0, Arc::clone(&block_device)).lock().modify(
0,
|super_block: &mut SuperBlock| {
super_block.initialize(
total_blocks,
inode_bitmap_blocks,
inode_area_blocks,
data_bitmap_blocks,
data_area_blocks,
);
},
);
// write back immediately
// create a inode for root node "/"
assert_eq!(efs.alloc_inode(), 0);
let (root_inode_block_id, root_inode_offset) = efs.get_disk_inode_pos(0);
get_block_cache(root_inode_block_id as usize, Arc::clone(&block_device))
.lock()
.modify(root_inode_offset, |disk_inode: &mut DiskInode| {
disk_inode.initialize(DiskInodeType::Directory);
});
block_cache_sync_all();
Arc::new(Mutex::new(efs))
}
pub fn open(block_device: Arc<dyn BlockDevice>) -> Arc<Mutex<Self>> {
// read SuperBlock
get_block_cache(0, Arc::clone(&block_device))
.lock()
.read(0, |super_block: &SuperBlock| {
assert!(super_block.is_valid(), "Error loading EFS!");
let inode_total_blocks =
super_block.inode_bitmap_blocks + super_block.inode_area_blocks;
let efs = Self {
block_device,
inode_bitmap: Bitmap::new(1, super_block.inode_bitmap_blocks as usize),
data_bitmap: Bitmap::new(
(1 + inode_total_blocks) as usize,
super_block.data_bitmap_blocks as usize,
),
inode_area_start_block: 1 + super_block.inode_bitmap_blocks,
data_area_start_block: 1 + inode_total_blocks + super_block.data_bitmap_blocks,
};
Arc::new(Mutex::new(efs))
})
}
pub fn root_inode(efs: &Arc<Mutex<Self>>) -> Inode {
let block_device = Arc::clone(&efs.lock().block_device);
// acquire efs lock temporarily
let (block_id, block_offset) = efs.lock().get_disk_inode_pos(0);
// release efs lock
Inode::new(block_id, block_offset, Arc::clone(efs), block_device)
}
pub fn get_disk_inode_pos(&self, inode_id: u32) -> (u32, usize) {
let inode_size = core::mem::size_of::<DiskInode>();
let inodes_per_block = (BLOCK_SZ / inode_size) as u32;
let block_id = self.inode_area_start_block + inode_id / inodes_per_block;
(
block_id,
(inode_id % inodes_per_block) as usize * inode_size,
)
}
pub fn get_data_block_id(&self, data_block_id: u32) -> u32 {
self.data_area_start_block + data_block_id
}
pub fn alloc_inode(&mut self) -> u32 {
self.inode_bitmap.alloc(&self.block_device).unwrap() as u32
}
/// Return a block ID not ID in the data area.
pub fn alloc_data(&mut self) -> u32 {
self.data_bitmap.alloc(&self.block_device).unwrap() as u32 + self.data_area_start_block
}
pub fn dealloc_data(&mut self, block_id: u32) {
get_block_cache(block_id as usize, Arc::clone(&self.block_device))
.lock()
.modify(0, |data_block: &mut DataBlock| {
data_block.iter_mut().for_each(|p| {
*p = 0;
})
});
self.data_bitmap.dealloc(
&self.block_device,
(block_id - self.data_area_start_block) as usize,
)
}
}

@ -0,0 +1,409 @@
use super::{get_block_cache, BlockDevice, BLOCK_SZ};
use alloc::sync::Arc;
use alloc::vec::Vec;
use core::fmt::{Debug, Formatter, Result};
const EFS_MAGIC: u32 = 0x3b800001;
const INODE_DIRECT_COUNT: usize = 28;
const NAME_LENGTH_LIMIT: usize = 27;
const INODE_INDIRECT1_COUNT: usize = BLOCK_SZ / 4;
const INODE_INDIRECT2_COUNT: usize = INODE_INDIRECT1_COUNT * INODE_INDIRECT1_COUNT;
const DIRECT_BOUND: usize = INODE_DIRECT_COUNT;
const INDIRECT1_BOUND: usize = DIRECT_BOUND + INODE_INDIRECT1_COUNT;
#[allow(unused)]
const INDIRECT2_BOUND: usize = INDIRECT1_BOUND + INODE_INDIRECT2_COUNT;
#[repr(C)]
pub struct SuperBlock {
magic: u32,
pub total_blocks: u32,
pub inode_bitmap_blocks: u32,
pub inode_area_blocks: u32,
pub data_bitmap_blocks: u32,
pub data_area_blocks: u32,
}
impl Debug for SuperBlock {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
f.debug_struct("SuperBlock")
.field("total_blocks", &self.total_blocks)
.field("inode_bitmap_blocks", &self.inode_bitmap_blocks)
.field("inode_area_blocks", &self.inode_area_blocks)
.field("data_bitmap_blocks", &self.data_bitmap_blocks)
.field("data_area_blocks", &self.data_area_blocks)
.finish()
}
}
impl SuperBlock {
pub fn initialize(
&mut self,
total_blocks: u32,
inode_bitmap_blocks: u32,
inode_area_blocks: u32,
data_bitmap_blocks: u32,
data_area_blocks: u32,
) {
*self = Self {
magic: EFS_MAGIC,
total_blocks,
inode_bitmap_blocks,
inode_area_blocks,
data_bitmap_blocks,
data_area_blocks,
}
}
pub fn is_valid(&self) -> bool {
self.magic == EFS_MAGIC
}
}
#[derive(PartialEq)]
pub enum DiskInodeType {
File,
Directory,
}
type IndirectBlock = [u32; BLOCK_SZ / 4];
type DataBlock = [u8; BLOCK_SZ];
#[repr(C)]
pub struct DiskInode {
pub size: u32,
pub direct: [u32; INODE_DIRECT_COUNT],
pub indirect1: u32,
pub indirect2: u32,
type_: DiskInodeType,
}
impl DiskInode {
/// indirect1 and indirect2 block are allocated only when they are needed.
pub fn initialize(&mut self, type_: DiskInodeType) {
self.size = 0;
self.direct.iter_mut().for_each(|v| *v = 0);
self.indirect1 = 0;
self.indirect2 = 0;
self.type_ = type_;
}
pub fn is_dir(&self) -> bool {
self.type_ == DiskInodeType::Directory
}
#[allow(unused)]
pub fn is_file(&self) -> bool {
self.type_ == DiskInodeType::File
}
/// Return block number correspond to size.
pub fn data_blocks(&self) -> u32 {
Self::_data_blocks(self.size)
}
fn _data_blocks(size: u32) -> u32 {
(size + BLOCK_SZ as u32 - 1) / BLOCK_SZ as u32
}
/// Return number of blocks needed include indirect1/2.
pub fn total_blocks(size: u32) -> u32 {
let data_blocks = Self::_data_blocks(size) as usize;
let mut total = data_blocks as usize;
// indirect1
if data_blocks > INODE_DIRECT_COUNT {
total += 1;
}
// indirect2
if data_blocks > INDIRECT1_BOUND {
total += 1;
// sub indirect1
total +=
(data_blocks - INDIRECT1_BOUND + INODE_INDIRECT1_COUNT - 1) / INODE_INDIRECT1_COUNT;
}
total as u32
}
pub fn blocks_num_needed(&self, new_size: u32) -> u32 {
assert!(new_size >= self.size);
Self::total_blocks(new_size) - Self::total_blocks(self.size)
}
pub fn get_block_id(&self, inner_id: u32, block_device: &Arc<dyn BlockDevice>) -> u32 {
let inner_id = inner_id as usize;
if inner_id < INODE_DIRECT_COUNT {
self.direct[inner_id]
} else if inner_id < INDIRECT1_BOUND {
get_block_cache(self.indirect1 as usize, Arc::clone(block_device))
.lock()
.read(0, |indirect_block: &IndirectBlock| {
indirect_block[inner_id - INODE_DIRECT_COUNT]
})
} else {
let last = inner_id - INDIRECT1_BOUND;
let indirect1 = get_block_cache(self.indirect2 as usize, Arc::clone(block_device))
.lock()
.read(0, |indirect2: &IndirectBlock| {
indirect2[last / INODE_INDIRECT1_COUNT]
});
get_block_cache(indirect1 as usize, Arc::clone(block_device))
.lock()
.read(0, |indirect1: &IndirectBlock| {
indirect1[last % INODE_INDIRECT1_COUNT]
})
}
}
pub fn increase_size(
&mut self,
new_size: u32,
new_blocks: Vec<u32>,
block_device: &Arc<dyn BlockDevice>,
) {
let mut current_blocks = self.data_blocks();
self.size = new_size;
let mut total_blocks = self.data_blocks();
let mut new_blocks = new_blocks.into_iter();
// fill direct
while current_blocks < total_blocks.min(INODE_DIRECT_COUNT as u32) {
self.direct[current_blocks as usize] = new_blocks.next().unwrap();
current_blocks += 1;
}
// alloc indirect1
if total_blocks > INODE_DIRECT_COUNT as u32 {
if current_blocks == INODE_DIRECT_COUNT as u32 {
self.indirect1 = new_blocks.next().unwrap();
}
current_blocks -= INODE_DIRECT_COUNT as u32;
total_blocks -= INODE_DIRECT_COUNT as u32;
} else {
return;
}
// fill indirect1
get_block_cache(self.indirect1 as usize, Arc::clone(block_device))
.lock()
.modify(0, |indirect1: &mut IndirectBlock| {
while current_blocks < total_blocks.min(INODE_INDIRECT1_COUNT as u32) {
indirect1[current_blocks as usize] = new_blocks.next().unwrap();
current_blocks += 1;
}
});
// alloc indirect2
if total_blocks > INODE_INDIRECT1_COUNT as u32 {
if current_blocks == INODE_INDIRECT1_COUNT as u32 {
self.indirect2 = new_blocks.next().unwrap();
}
current_blocks -= INODE_INDIRECT1_COUNT as u32;
total_blocks -= INODE_INDIRECT1_COUNT as u32;
} else {
return;
}
// fill indirect2 from (a0, b0) -> (a1, b1)
let mut a0 = current_blocks as usize / INODE_INDIRECT1_COUNT;
let mut b0 = current_blocks as usize % INODE_INDIRECT1_COUNT;
let a1 = total_blocks as usize / INODE_INDIRECT1_COUNT;
let b1 = total_blocks as usize % INODE_INDIRECT1_COUNT;
// alloc low-level indirect1
get_block_cache(self.indirect2 as usize, Arc::clone(block_device))
.lock()
.modify(0, |indirect2: &mut IndirectBlock| {
while (a0 < a1) || (a0 == a1 && b0 < b1) {
if b0 == 0 {
indirect2[a0] = new_blocks.next().unwrap();
}
// fill current
get_block_cache(indirect2[a0] as usize, Arc::clone(block_device))
.lock()
.modify(0, |indirect1: &mut IndirectBlock| {
indirect1[b0] = new_blocks.next().unwrap();
});
// move to next
b0 += 1;
if b0 == INODE_INDIRECT1_COUNT {
b0 = 0;
a0 += 1;
}
}
});
}
/// Clear size to zero and return blocks that should be deallocated.
///
/// We will clear the block contents to zero later.
pub fn clear_size(&mut self, block_device: &Arc<dyn BlockDevice>) -> Vec<u32> {
let mut v: Vec<u32> = Vec::new();
let mut data_blocks = self.data_blocks() as usize;
self.size = 0;
let mut current_blocks = 0usize;
// direct
while current_blocks < data_blocks.min(INODE_DIRECT_COUNT) {
v.push(self.direct[current_blocks]);
self.direct[current_blocks] = 0;
current_blocks += 1;
}
// indirect1 block
if data_blocks > INODE_DIRECT_COUNT {
v.push(self.indirect1);
data_blocks -= INODE_DIRECT_COUNT;
current_blocks = 0;
} else {
return v;
}
// indirect1
get_block_cache(self.indirect1 as usize, Arc::clone(block_device))
.lock()
.modify(0, |indirect1: &mut IndirectBlock| {
while current_blocks < data_blocks.min(INODE_INDIRECT1_COUNT) {
v.push(indirect1[current_blocks]);
//indirect1[current_blocks] = 0;
current_blocks += 1;
}
});
self.indirect1 = 0;
// indirect2 block
if data_blocks > INODE_INDIRECT1_COUNT {
v.push(self.indirect2);
data_blocks -= INODE_INDIRECT1_COUNT;
} else {
return v;
}
// indirect2
assert!(data_blocks <= INODE_INDIRECT2_COUNT);
let a1 = data_blocks / INODE_INDIRECT1_COUNT;
let b1 = data_blocks % INODE_INDIRECT1_COUNT;
get_block_cache(self.indirect2 as usize, Arc::clone(block_device))
.lock()
.modify(0, |indirect2: &mut IndirectBlock| {
// full indirect1 blocks
for entry in indirect2.iter_mut().take(a1) {
v.push(*entry);
get_block_cache(*entry as usize, Arc::clone(block_device))
.lock()
.modify(0, |indirect1: &mut IndirectBlock| {
for entry in indirect1.iter() {
v.push(*entry);
}
});
}
// last indirect1 block
if b1 > 0 {
v.push(indirect2[a1]);
get_block_cache(indirect2[a1] as usize, Arc::clone(block_device))
.lock()
.modify(0, |indirect1: &mut IndirectBlock| {
for entry in indirect1.iter().take(b1) {
v.push(*entry);
}
});
//indirect2[a1] = 0;
}
});
self.indirect2 = 0;
v
}
pub fn read_at(
&self,
offset: usize,
buf: &mut [u8],
block_device: &Arc<dyn BlockDevice>,
) -> usize {
let mut start = offset;
let end = (offset + buf.len()).min(self.size as usize);
if start >= end {
return 0;
}
let mut start_block = start / BLOCK_SZ;
let mut read_size = 0usize;
loop {
// calculate end of current block
let mut end_current_block = (start / BLOCK_SZ + 1) * BLOCK_SZ;
end_current_block = end_current_block.min(end);
// read and update read size
let block_read_size = end_current_block - start;
let dst = &mut buf[read_size..read_size + block_read_size];
get_block_cache(
self.get_block_id(start_block as u32, block_device) as usize,
Arc::clone(block_device),
)
.lock()
.read(0, |data_block: &DataBlock| {
let src = &data_block[start % BLOCK_SZ..start % BLOCK_SZ + block_read_size];
dst.copy_from_slice(src);
});
read_size += block_read_size;
// move to next block
if end_current_block == end {
break;
}
start_block += 1;
start = end_current_block;
}
read_size
}
/// File size must be adjusted before.
pub fn write_at(
&mut self,
offset: usize,
buf: &[u8],
block_device: &Arc<dyn BlockDevice>,
) -> usize {
let mut start = offset;
let end = (offset + buf.len()).min(self.size as usize);
assert!(start <= end);
let mut start_block = start / BLOCK_SZ;
let mut write_size = 0usize;
loop {
// calculate end of current block
let mut end_current_block = (start / BLOCK_SZ + 1) * BLOCK_SZ;
end_current_block = end_current_block.min(end);
// write and update write size
let block_write_size = end_current_block - start;
get_block_cache(
self.get_block_id(start_block as u32, block_device) as usize,
Arc::clone(block_device),
)
.lock()
.modify(0, |data_block: &mut DataBlock| {
let src = &buf[write_size..write_size + block_write_size];
let dst = &mut data_block[start % BLOCK_SZ..start % BLOCK_SZ + block_write_size];
dst.copy_from_slice(src);
});
write_size += block_write_size;
// move to next block
if end_current_block == end {
break;
}
start_block += 1;
start = end_current_block;
}
write_size
}
}
#[repr(C)]
pub struct DirEntry {
name: [u8; NAME_LENGTH_LIMIT + 1],
inode_number: u32,
}
pub const DIRENT_SZ: usize = 32;
impl DirEntry {
pub fn empty() -> Self {
Self {
name: [0u8; NAME_LENGTH_LIMIT + 1],
inode_number: 0,
}
}
pub fn new(name: &str, inode_number: u32) -> Self {
let mut bytes = [0u8; NAME_LENGTH_LIMIT + 1];
bytes[..name.len()].copy_from_slice(name.as_bytes());
Self {
name: bytes,
inode_number,
}
}
pub fn as_bytes(&self) -> &[u8] {
unsafe { core::slice::from_raw_parts(self as *const _ as usize as *const u8, DIRENT_SZ) }
}
pub fn as_bytes_mut(&mut self) -> &mut [u8] {
unsafe { core::slice::from_raw_parts_mut(self as *mut _ as usize as *mut u8, DIRENT_SZ) }
}
pub fn name(&self) -> &str {
let len = (0usize..).find(|i| self.name[*i] == 0).unwrap();
core::str::from_utf8(&self.name[..len]).unwrap()
}
pub fn inode_number(&self) -> u32 {
self.inode_number
}
}

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save