mirror of
https://github.com/phil-opp/blog_os.git
synced 2025-12-16 14:27:49 +00:00
Compare commits
405 Commits
post-08
...
121dc393f8
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
121dc393f8 | ||
|
|
08a6e13df4 | ||
|
|
569232322e | ||
|
|
94149c93f9 | ||
|
|
34195ec8b7 | ||
|
|
27b56832c1 | ||
|
|
194fa3c3fe | ||
|
|
9799487962 | ||
|
|
a7c5741ab5 | ||
|
|
0da8a4bc6b | ||
|
|
d43aa8aebf | ||
|
|
2049f23abb | ||
|
|
6de3aeaac3 | ||
|
|
a26d6c1b54 | ||
|
|
dc03bde076 | ||
|
|
3a988748de | ||
|
|
aa4f62187e | ||
|
|
e8f6e7300d | ||
|
|
889ceba24a | ||
|
|
69fe1aed12 | ||
|
|
d849a7dea5 | ||
|
|
bb2c57e1b4 | ||
|
|
bc7335ce6c | ||
|
|
722093e2c4 | ||
|
|
ba7b36aa21 | ||
|
|
6dc837db7c | ||
|
|
4dd9c6bce7 | ||
|
|
28c9558a7a | ||
|
|
4f6558c021 | ||
|
|
f63e0dacca | ||
|
|
bb3beb22da | ||
|
|
9ea911b0bb | ||
|
|
39ee714093 | ||
|
|
3743997479 | ||
|
|
2870ac4e13 | ||
|
|
9c546d4899 | ||
|
|
486975e4dc | ||
|
|
c250dd0a91 | ||
|
|
e59a2d1249 | ||
|
|
268b5b6856 | ||
|
|
2fdc7c3b5c | ||
|
|
9bcbf17073 | ||
|
|
d54e691261 | ||
|
|
be58db8907 | ||
|
|
bd148f9bf2 | ||
|
|
1550e83094 | ||
|
|
b654651a85 | ||
|
|
64aeb0e98e | ||
|
|
cd244cc604 | ||
|
|
bf7cf4c65d | ||
|
|
06f064aefa | ||
|
|
6b51fa467e | ||
|
|
4343f9be6a | ||
|
|
f5b5251092 | ||
|
|
92f197c2e5 | ||
|
|
af909cab1e | ||
|
|
b6437c5cc1 | ||
|
|
515ab6f1bf | ||
|
|
5fc6566d45 | ||
|
|
ee550a64e0 | ||
|
|
70714bc27d | ||
|
|
f0149947cf | ||
|
|
a249900aed | ||
|
|
1893caa212 | ||
|
|
09a3ac4f80 | ||
|
|
e56cd71689 | ||
|
|
a859eddacf | ||
|
|
506a1d1961 | ||
|
|
17e1ae2508 | ||
|
|
6b4666fbaf | ||
|
|
e41ff18ea0 | ||
|
|
f345162ee5 | ||
|
|
371f86175b | ||
|
|
0af790efa6 | ||
|
|
b0ec3ba71f | ||
|
|
8452f2617a | ||
|
|
01f42c65e8 | ||
|
|
235b56a8e3 | ||
|
|
71e6fb1c40 | ||
|
|
3c1db5e79f | ||
|
|
e3b19929b7 | ||
|
|
f2d93a4e27 | ||
|
|
0e1efb591c | ||
|
|
2359915246 | ||
|
|
31d907a1f2 | ||
|
|
6c161ff3e0 | ||
|
|
1acc894846 | ||
|
|
62378b25df | ||
|
|
66d0d5d11c | ||
|
|
3508e8a058 | ||
|
|
51caf33232 | ||
|
|
b5d4b9b212 | ||
|
|
d8478d537b | ||
|
|
7653ffcb40 | ||
|
|
3c2b79b1f0 | ||
|
|
64264ce6ec | ||
|
|
20dce91c5b | ||
|
|
56be80d62b | ||
|
|
b19100a021 | ||
|
|
8f20817ea3 | ||
|
|
60664c8876 | ||
|
|
abb90be792 | ||
|
|
d21dbc7c58 | ||
|
|
ca120eed8c | ||
|
|
98c9de82b5 | ||
|
|
3532a12264 | ||
|
|
a6a3e30275 | ||
|
|
ffd34a58f9 | ||
|
|
4bcfdf4b03 | ||
|
|
1ced86e362 | ||
|
|
2e5107d6e8 | ||
|
|
c79bf064c8 | ||
|
|
4b0c11a7de | ||
|
|
9783bf99ca | ||
|
|
71a70a3b95 | ||
|
|
59639c8fe7 | ||
|
|
ccc507a243 | ||
|
|
d57a9d3f1f | ||
|
|
63be7cda6b | ||
|
|
722c5579de | ||
|
|
8ca3aa3846 | ||
|
|
380e54d6b8 | ||
|
|
31d3eef0da | ||
|
|
0404f437f9 | ||
|
|
7e05d80506 | ||
|
|
ce20cc8661 | ||
|
|
162071d611 | ||
|
|
180e6ba36c | ||
|
|
ed93cfb926 | ||
|
|
3a230d0624 | ||
|
|
ca3dfc719c | ||
|
|
76282b9981 | ||
|
|
93844bee41 | ||
|
|
bc618ad457 | ||
|
|
c1133b0948 | ||
|
|
92238b15b0 | ||
|
|
f7b5e22f68 | ||
|
|
6742a16d50 | ||
|
|
624596adf1 | ||
|
|
340cbaba36 | ||
|
|
231dfacf3b | ||
|
|
4e08f97310 | ||
|
|
ff72429a55 | ||
|
|
9bf21a0392 | ||
|
|
aa800ddc83 | ||
|
|
487b130c85 | ||
|
|
deb95c5ead | ||
|
|
d820839541 | ||
|
|
df3a464cec | ||
|
|
d28ad91407 | ||
|
|
6ebeb28fdb | ||
|
|
c2ef594a78 | ||
|
|
43b5f835ab | ||
|
|
f48eed487c | ||
|
|
d2e7a4a970 | ||
|
|
91790d8007 | ||
|
|
287fed94ef | ||
|
|
6bbaf9de16 | ||
|
|
93cb9dfdbe | ||
|
|
9af48bae17 | ||
|
|
4cec056568 | ||
|
|
b0cb401b3b | ||
|
|
d215eb07c2 | ||
|
|
69c7df8a30 | ||
|
|
b7835f3976 | ||
|
|
472ddcc696 | ||
|
|
bd4487cefa | ||
|
|
e68f7a1d31 | ||
|
|
832ab43bdd | ||
|
|
cc8ecaadc2 | ||
|
|
c66758ccad | ||
|
|
e7fd27b648 | ||
|
|
cecab1a310 | ||
|
|
f54a2d7574 | ||
|
|
9809ea57ae | ||
|
|
e6c4c63c00 | ||
|
|
ed543a4591 | ||
|
|
b567d06108 | ||
|
|
8b90c04c62 | ||
|
|
0d661fcfdd | ||
|
|
384902502c | ||
|
|
5e510e41ee | ||
|
|
6f12524245 | ||
|
|
0db15e5ee5 | ||
|
|
9828ec0eff | ||
|
|
44ab90f92d | ||
|
|
7a5d91cb96 | ||
|
|
491889a237 | ||
|
|
a2f07828e4 | ||
|
|
d95da1d511 | ||
|
|
1eab81f58d | ||
|
|
d94b9513e7 | ||
|
|
7bfacb39c4 | ||
|
|
3c28d7f364 | ||
|
|
02c36bae29 | ||
|
|
70deb3168e | ||
|
|
83b67df8af | ||
|
|
714d9cef7d | ||
|
|
7b0d8f05ba | ||
|
|
3735ae51c9 | ||
|
|
f230ee4258 | ||
|
|
a5d409b777 | ||
|
|
a430d5ef06 | ||
|
|
39f14b6d0b | ||
|
|
dd1daf2652 | ||
|
|
f4331daa25 | ||
|
|
9a415c85b9 | ||
|
|
79a0172ace | ||
|
|
921dd54207 | ||
|
|
c7cc718ae3 | ||
|
|
8fc746555d | ||
|
|
3a6d3153a4 | ||
|
|
9fb6c1d0bd | ||
|
|
5ed04baab0 | ||
|
|
00fedc801e | ||
|
|
0f74db4812 | ||
|
|
93fd330ab9 | ||
|
|
3ad5f117c2 | ||
|
|
d1678f5a96 | ||
|
|
002d6f255f | ||
|
|
6c3bf0b10f | ||
|
|
7a792f5cb0 | ||
|
|
93b4dcf434 | ||
|
|
821dd2adb4 | ||
|
|
d636939b51 | ||
|
|
9b7326541e | ||
|
|
4f234b67ef | ||
|
|
7381e11f3c | ||
|
|
a9fe65a0ce | ||
|
|
2001814119 | ||
|
|
a5c50e7408 | ||
|
|
70a52c291d | ||
|
|
c56bfa27e4 | ||
|
|
55aec9ebf3 | ||
|
|
2e1d132a9a | ||
|
|
63e8577d77 | ||
|
|
75d826bf69 | ||
|
|
45be3f0648 | ||
|
|
055c560a7a | ||
|
|
e87044a7ee | ||
|
|
08d2289dad | ||
|
|
7c84dbaa1d | ||
|
|
882c83f9de | ||
|
|
869a69e531 | ||
|
|
817267e51c | ||
|
|
9e75078dab | ||
|
|
ed3af8e984 | ||
|
|
1d1075b35e | ||
|
|
41f38d92f2 | ||
|
|
8eb44dcbd5 | ||
|
|
3ac5318c94 | ||
|
|
e94a489a31 | ||
|
|
f392d8a7f5 | ||
|
|
c7bc26d8ee | ||
|
|
9da9ecb515 | ||
|
|
6c1594ea24 | ||
|
|
f226ba0c3f | ||
|
|
94aa2c4093 | ||
|
|
c1fc7171fb | ||
|
|
259742a5e3 | ||
|
|
cf527e9ce7 | ||
|
|
c891acb234 | ||
|
|
6876e82fc5 | ||
|
|
b9a0702bc0 | ||
|
|
84ddda6a3f | ||
|
|
2919cbdefc | ||
|
|
f85664fd6b | ||
|
|
059ea76848 | ||
|
|
a65573b061 | ||
|
|
2f085b7310 | ||
|
|
385004cab2 | ||
|
|
cc713e4570 | ||
|
|
9696612b2b | ||
|
|
b3066e9a78 | ||
|
|
d5d7db0de6 | ||
|
|
66c3a0de76 | ||
|
|
45375d1f1b | ||
|
|
96d9de76b0 | ||
|
|
f56e4d24e3 | ||
|
|
8d3d712cef | ||
|
|
4897e9c4d5 | ||
|
|
5cced71fb0 | ||
|
|
7ec727f69f | ||
|
|
5f66b437d5 | ||
|
|
8059c229c5 | ||
|
|
0694f29946 | ||
|
|
bc2099d31d | ||
|
|
211ec3898b | ||
|
|
889c0771d6 | ||
|
|
2227fa434f | ||
|
|
e74d9753f2 | ||
|
|
23d554548a | ||
|
|
9fc71547d7 | ||
|
|
caa9d8b0e4 | ||
|
|
091f7ef153 | ||
|
|
29512ddd31 | ||
|
|
38d606b4d1 | ||
|
|
a7943e7e55 | ||
|
|
ae75d8b209 | ||
|
|
bcdadec5b6 | ||
|
|
e0d5cdd625 | ||
|
|
17f8866264 | ||
|
|
3f95494ae8 | ||
|
|
a7ef4012d0 | ||
|
|
cabbbec72f | ||
|
|
6f07c2b666 | ||
|
|
1dd7f03a7a | ||
|
|
e89c77398d | ||
|
|
66c4eae9cf | ||
|
|
f3a7689f33 | ||
|
|
e721878e4b | ||
|
|
14d0e07b3e | ||
|
|
f2eb6cec9a | ||
|
|
9651eb7a3b | ||
|
|
bc4ddcef05 | ||
|
|
89e3e2b190 | ||
|
|
004282138c | ||
|
|
4580b85fe2 | ||
|
|
8c575388cf | ||
|
|
4792ec41b1 | ||
|
|
df75f7f4e8 | ||
|
|
5cf3884396 | ||
|
|
e5b6ba38ac | ||
|
|
f429a8ab03 | ||
|
|
d7484ab48b | ||
|
|
06fc63028a | ||
|
|
d4623419b0 | ||
|
|
417c44159e | ||
|
|
ebbc6d55d2 | ||
|
|
c0367074ac | ||
|
|
48e2175bac | ||
|
|
45e1e99390 | ||
|
|
b5793c34c6 | ||
|
|
67ee45090d | ||
|
|
64d1a587e7 | ||
|
|
fbe279831e | ||
|
|
375d4d0479 | ||
|
|
7796d4c14a | ||
|
|
78e4b22a2f | ||
|
|
a867450e3b | ||
|
|
b5ee44621c | ||
|
|
24a9e7abd2 | ||
|
|
180c77d1f4 | ||
|
|
247af45791 | ||
|
|
fd0646fecf | ||
|
|
c8821cb226 | ||
|
|
2e531850b8 | ||
|
|
65dbb5ac48 | ||
|
|
16cf7e8e42 | ||
|
|
19556d9e68 | ||
|
|
338833262c | ||
|
|
77800d9212 | ||
|
|
be618df7f5 | ||
|
|
ff85a2e502 | ||
|
|
74675346c9 | ||
|
|
e9344ae046 | ||
|
|
4acf12bb69 | ||
|
|
161d5fe7be | ||
|
|
9bf4ea7341 | ||
|
|
a1bf5651fc | ||
|
|
763228c859 | ||
|
|
770af27d75 | ||
|
|
3e59283c19 | ||
|
|
6146ccba2d | ||
|
|
b0e1527a95 | ||
|
|
cb4410c84e | ||
|
|
98b5976656 | ||
|
|
9335386928 | ||
|
|
7c30d62f33 | ||
|
|
61683bccda | ||
|
|
e1ec5159b8 | ||
|
|
7b7d19592f | ||
|
|
e387c0b6b8 | ||
|
|
d5abc119f3 | ||
|
|
59da6e5620 | ||
|
|
ef1cc0ed4f | ||
|
|
90f0caec1a | ||
|
|
5c0fb63f33 | ||
|
|
036a8e7608 | ||
|
|
10c4d0509d | ||
|
|
57998ea4f8 | ||
|
|
ef9a629ddc | ||
|
|
d974cf5200 | ||
|
|
36d6c6d0e9 | ||
|
|
ff49104764 | ||
|
|
03e43da9f9 | ||
|
|
e696d65b60 | ||
|
|
ebf626061f | ||
|
|
c3d023ad40 | ||
|
|
0a10b3e784 | ||
|
|
e0f66a8196 | ||
|
|
e5dfbd4b23 | ||
|
|
5ad2962389 | ||
|
|
a56e22b6fc | ||
|
|
67f536d7c6 | ||
|
|
741224411b | ||
|
|
818417d119 | ||
|
|
f272785861 | ||
|
|
5d807ee622 | ||
|
|
90c3cdf0f3 | ||
|
|
2bc233b2f6 | ||
|
|
051b23f577 | ||
|
|
f2bc2d33f0 | ||
|
|
38a121a887 | ||
|
|
8b380f0692 |
@@ -1,5 +1,5 @@
|
|||||||
[unstable]
|
[unstable]
|
||||||
build-std = ["core", "compiler_builtins"]
|
build-std = ["core", "compiler_builtins", "alloc"]
|
||||||
build-std-features = ["compiler-builtins-mem"]
|
build-std-features = ["compiler-builtins-mem"]
|
||||||
|
|
||||||
[build]
|
[build]
|
||||||
|
|||||||
34
Cargo.lock
generated
34
Cargo.lock
generated
@@ -20,6 +20,7 @@ version = "0.1.0"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"bootloader",
|
"bootloader",
|
||||||
"lazy_static",
|
"lazy_static",
|
||||||
|
"linked_list_allocator",
|
||||||
"pc-keyboard",
|
"pc-keyboard",
|
||||||
"pic8259",
|
"pic8259",
|
||||||
"spin",
|
"spin",
|
||||||
@@ -43,6 +44,24 @@ dependencies = [
|
|||||||
"spin",
|
"spin",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "linked_list_allocator"
|
||||||
|
version = "0.9.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d0b725207570aa16096962d0b20c79f8a543df2280bd3c903022b9b0b4d7ea68"
|
||||||
|
dependencies = [
|
||||||
|
"spinning_top",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "lock_api"
|
||||||
|
version = "0.4.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0382880606dff6d15c9476c416d18690b72742aa7b605bb6dd6ec9030fbf07eb"
|
||||||
|
dependencies = [
|
||||||
|
"scopeguard",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "pc-keyboard"
|
name = "pc-keyboard"
|
||||||
version = "0.7.0"
|
version = "0.7.0"
|
||||||
@@ -58,12 +77,27 @@ dependencies = [
|
|||||||
"x86_64",
|
"x86_64",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "scopeguard"
|
||||||
|
version = "1.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "spin"
|
name = "spin"
|
||||||
version = "0.5.2"
|
version = "0.5.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
|
checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "spinning_top"
|
||||||
|
version = "0.2.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "75adad84ee84b521fb2cca2d4fd0f1dab1d8d026bda3c5bea4ca63b5f9f9293c"
|
||||||
|
dependencies = [
|
||||||
|
"lock_api",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "uart_16550"
|
name = "uart_16550"
|
||||||
version = "0.2.14"
|
version = "0.2.14"
|
||||||
|
|||||||
13
Cargo.toml
13
Cargo.toml
@@ -13,13 +13,14 @@ name = "stack_overflow"
|
|||||||
harness = false
|
harness = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
bootloader = "0.9"
|
bootloader = { version = "0.9", features = ["map_physical_memory"] }
|
||||||
volatile = "0.2.6"
|
volatile = "0.2.6"
|
||||||
spin = "0.5.2"
|
spin = "0.5.2"
|
||||||
x86_64 = "0.14.2"
|
x86_64 = "0.14.2"
|
||||||
uart_16550 = "0.2.0"
|
uart_16550 = "0.2.0"
|
||||||
pic8259 = "0.10.1"
|
pic8259 = "0.10.1"
|
||||||
pc-keyboard = "0.7.0"
|
pc-keyboard = "0.7.0"
|
||||||
|
linked_list_allocator = "0.9.0"
|
||||||
|
|
||||||
[dependencies.lazy_static]
|
[dependencies.lazy_static]
|
||||||
version = "1.0"
|
version = "1.0"
|
||||||
@@ -28,7 +29,11 @@ features = ["spin_no_std"]
|
|||||||
|
|
||||||
[package.metadata.bootimage]
|
[package.metadata.bootimage]
|
||||||
test-args = [
|
test-args = [
|
||||||
"-device", "isa-debug-exit,iobase=0xf4,iosize=0x04", "-serial", "stdio",
|
"-device",
|
||||||
"-display", "none"
|
"isa-debug-exit,iobase=0xf4,iosize=0x04",
|
||||||
|
"-serial",
|
||||||
|
"stdio",
|
||||||
|
"-display",
|
||||||
|
"none",
|
||||||
]
|
]
|
||||||
test-success-exit-code = 33 # (0x10 << 1) | 1
|
test-success-exit-code = 33 # (0x10 << 1) | 1
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
# Blog OS (Introduction to Paging)
|
# Blog OS (Allocator Designs)
|
||||||
|
|
||||||
[](https://github.com/phil-opp/blog_os/actions?query=workflow%3A%22Code%22+branch%3Apost-08)
|
[](https://github.com/phil-opp/blog_os/actions?query=workflow%3A%22Code%22+branch%3Apost-11)
|
||||||
|
|
||||||
This repository contains the source code for the [Introduction to Paging][post] post of the [Writing an OS in Rust](https://os.phil-opp.com) series.
|
This repository contains the source code for the [Allocator Designs][post] post of the [Writing an OS in Rust](https://os.phil-opp.com) series.
|
||||||
|
|
||||||
[post]: https://os.phil-opp.com/paging-introduction/
|
[post]: https://os.phil-opp.com/allocator-designs/
|
||||||
|
|
||||||
**Check out the [master branch](https://github.com/phil-opp/blog_os) for more information.**
|
**Check out the [master branch](https://github.com/phil-opp/blog_os) for more information.**
|
||||||
|
|
||||||
|
|||||||
82
src/allocator.rs
Normal file
82
src/allocator.rs
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
use alloc::alloc::{GlobalAlloc, Layout};
|
||||||
|
use core::ptr::null_mut;
|
||||||
|
use fixed_size_block::FixedSizeBlockAllocator;
|
||||||
|
use x86_64::{
|
||||||
|
VirtAddr,
|
||||||
|
structures::paging::{
|
||||||
|
FrameAllocator, Mapper, Page, PageTableFlags, Size4KiB, mapper::MapToError,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
pub mod bump;
|
||||||
|
pub mod fixed_size_block;
|
||||||
|
pub mod linked_list;
|
||||||
|
|
||||||
|
pub const HEAP_START: usize = 0x_4444_4444_0000;
|
||||||
|
pub const HEAP_SIZE: usize = 100 * 1024; // 100 KiB
|
||||||
|
|
||||||
|
#[global_allocator]
|
||||||
|
static ALLOCATOR: Locked<FixedSizeBlockAllocator> = Locked::new(FixedSizeBlockAllocator::new());
|
||||||
|
|
||||||
|
pub fn init_heap(
|
||||||
|
mapper: &mut impl Mapper<Size4KiB>,
|
||||||
|
frame_allocator: &mut impl FrameAllocator<Size4KiB>,
|
||||||
|
) -> Result<(), MapToError<Size4KiB>> {
|
||||||
|
let page_range = {
|
||||||
|
let heap_start = VirtAddr::new(HEAP_START as u64);
|
||||||
|
let heap_end = heap_start + HEAP_SIZE - 1u64;
|
||||||
|
let heap_start_page = Page::containing_address(heap_start);
|
||||||
|
let heap_end_page = Page::containing_address(heap_end);
|
||||||
|
Page::range_inclusive(heap_start_page, heap_end_page)
|
||||||
|
};
|
||||||
|
|
||||||
|
for page in page_range {
|
||||||
|
let frame = frame_allocator
|
||||||
|
.allocate_frame()
|
||||||
|
.ok_or(MapToError::FrameAllocationFailed)?;
|
||||||
|
let flags = PageTableFlags::PRESENT | PageTableFlags::WRITABLE;
|
||||||
|
unsafe { mapper.map_to(page, frame, flags, frame_allocator)?.flush() };
|
||||||
|
}
|
||||||
|
|
||||||
|
unsafe {
|
||||||
|
ALLOCATOR.lock().init(HEAP_START, HEAP_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Dummy;
|
||||||
|
|
||||||
|
unsafe impl GlobalAlloc for Dummy {
|
||||||
|
unsafe fn alloc(&self, _layout: Layout) -> *mut u8 {
|
||||||
|
null_mut()
|
||||||
|
}
|
||||||
|
|
||||||
|
unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {
|
||||||
|
panic!("dealloc should be never called")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A wrapper around spin::Mutex to permit trait implementations.
|
||||||
|
pub struct Locked<A> {
|
||||||
|
inner: spin::Mutex<A>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<A> Locked<A> {
|
||||||
|
pub const fn new(inner: A) -> Self {
|
||||||
|
Locked {
|
||||||
|
inner: spin::Mutex::new(inner),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn lock(&self) -> spin::MutexGuard<A> {
|
||||||
|
self.inner.lock()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Align the given address `addr` upwards to alignment `align`.
|
||||||
|
///
|
||||||
|
/// Requires that `align` is a power of two.
|
||||||
|
fn align_up(addr: usize, align: usize) -> usize {
|
||||||
|
(addr + align - 1) & !(align - 1)
|
||||||
|
}
|
||||||
61
src/allocator/bump.rs
Normal file
61
src/allocator/bump.rs
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
use super::{Locked, align_up};
|
||||||
|
use alloc::alloc::{GlobalAlloc, Layout};
|
||||||
|
use core::ptr;
|
||||||
|
|
||||||
|
pub struct BumpAllocator {
|
||||||
|
heap_start: usize,
|
||||||
|
heap_end: usize,
|
||||||
|
next: usize,
|
||||||
|
allocations: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl BumpAllocator {
|
||||||
|
/// Creates a new empty bump allocator.
|
||||||
|
pub const fn new() -> Self {
|
||||||
|
BumpAllocator {
|
||||||
|
heap_start: 0,
|
||||||
|
heap_end: 0,
|
||||||
|
next: 0,
|
||||||
|
allocations: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Initializes the bump allocator with the given heap bounds.
|
||||||
|
///
|
||||||
|
/// This method is unsafe because the caller must ensure that the given
|
||||||
|
/// memory range is unused. Also, this method must be called only once.
|
||||||
|
pub unsafe fn init(&mut self, heap_start: usize, heap_size: usize) {
|
||||||
|
self.heap_start = heap_start;
|
||||||
|
self.heap_end = heap_start.saturating_add(heap_size);
|
||||||
|
self.next = heap_start;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
unsafe impl GlobalAlloc for Locked<BumpAllocator> {
|
||||||
|
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
|
||||||
|
let mut bump = self.lock(); // get a mutable reference
|
||||||
|
|
||||||
|
let alloc_start = align_up(bump.next, layout.align());
|
||||||
|
let alloc_end = match alloc_start.checked_add(layout.size()) {
|
||||||
|
Some(end) => end,
|
||||||
|
None => return ptr::null_mut(),
|
||||||
|
};
|
||||||
|
|
||||||
|
if alloc_end > bump.heap_end {
|
||||||
|
ptr::null_mut() // out of memory
|
||||||
|
} else {
|
||||||
|
bump.next = alloc_end;
|
||||||
|
bump.allocations += 1;
|
||||||
|
alloc_start as *mut u8
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {
|
||||||
|
let mut bump = self.lock(); // get a mutable reference
|
||||||
|
|
||||||
|
bump.allocations -= 1;
|
||||||
|
if bump.allocations == 0 {
|
||||||
|
bump.next = bump.heap_start;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
109
src/allocator/fixed_size_block.rs
Normal file
109
src/allocator/fixed_size_block.rs
Normal file
@@ -0,0 +1,109 @@
|
|||||||
|
use super::Locked;
|
||||||
|
use alloc::alloc::{GlobalAlloc, Layout};
|
||||||
|
use core::{
|
||||||
|
mem,
|
||||||
|
ptr::{self, NonNull},
|
||||||
|
};
|
||||||
|
|
||||||
|
/// The block sizes to use.
|
||||||
|
///
|
||||||
|
/// The sizes must each be power of 2 because they are also used as
|
||||||
|
/// the block alignment (alignments must be always powers of 2).
|
||||||
|
const BLOCK_SIZES: &[usize] = &[8, 16, 32, 64, 128, 256, 512, 1024, 2048];
|
||||||
|
|
||||||
|
/// Choose an appropriate block size for the given layout.
|
||||||
|
///
|
||||||
|
/// Returns an index into the `BLOCK_SIZES` array.
|
||||||
|
fn list_index(layout: &Layout) -> Option<usize> {
|
||||||
|
let required_block_size = layout.size().max(layout.align());
|
||||||
|
BLOCK_SIZES.iter().position(|&s| s >= required_block_size)
|
||||||
|
}
|
||||||
|
|
||||||
|
struct ListNode {
|
||||||
|
next: Option<&'static mut ListNode>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct FixedSizeBlockAllocator {
|
||||||
|
list_heads: [Option<&'static mut ListNode>; BLOCK_SIZES.len()],
|
||||||
|
fallback_allocator: linked_list_allocator::Heap,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FixedSizeBlockAllocator {
|
||||||
|
/// Creates an empty FixedSizeBlockAllocator.
|
||||||
|
pub const fn new() -> Self {
|
||||||
|
const EMPTY: Option<&'static mut ListNode> = None;
|
||||||
|
FixedSizeBlockAllocator {
|
||||||
|
list_heads: [EMPTY; BLOCK_SIZES.len()],
|
||||||
|
fallback_allocator: linked_list_allocator::Heap::empty(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Initialize the allocator with the given heap bounds.
|
||||||
|
///
|
||||||
|
/// This function is unsafe because the caller must guarantee that the given
|
||||||
|
/// heap bounds are valid and that the heap is unused. This method must be
|
||||||
|
/// called only once.
|
||||||
|
pub unsafe fn init(&mut self, heap_start: usize, heap_size: usize) {
|
||||||
|
unsafe {
|
||||||
|
self.fallback_allocator.init(heap_start, heap_size);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Allocates using the fallback allocator.
|
||||||
|
fn fallback_alloc(&mut self, layout: Layout) -> *mut u8 {
|
||||||
|
match self.fallback_allocator.allocate_first_fit(layout) {
|
||||||
|
Ok(ptr) => ptr.as_ptr(),
|
||||||
|
Err(_) => ptr::null_mut(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
unsafe impl GlobalAlloc for Locked<FixedSizeBlockAllocator> {
|
||||||
|
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
|
||||||
|
let mut allocator = self.lock();
|
||||||
|
match list_index(&layout) {
|
||||||
|
Some(index) => {
|
||||||
|
match allocator.list_heads[index].take() {
|
||||||
|
Some(node) => {
|
||||||
|
allocator.list_heads[index] = node.next.take();
|
||||||
|
node as *mut ListNode as *mut u8
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
// no block exists in list => allocate new block
|
||||||
|
let block_size = BLOCK_SIZES[index];
|
||||||
|
// only works if all block sizes are a power of 2
|
||||||
|
let block_align = block_size;
|
||||||
|
let layout = Layout::from_size_align(block_size, block_align).unwrap();
|
||||||
|
allocator.fallback_alloc(layout)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => allocator.fallback_alloc(layout),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
|
||||||
|
let mut allocator = self.lock();
|
||||||
|
match list_index(&layout) {
|
||||||
|
Some(index) => {
|
||||||
|
let new_node = ListNode {
|
||||||
|
next: allocator.list_heads[index].take(),
|
||||||
|
};
|
||||||
|
// verify that block has size and alignment required for storing node
|
||||||
|
assert!(mem::size_of::<ListNode>() <= BLOCK_SIZES[index]);
|
||||||
|
assert!(mem::align_of::<ListNode>() <= BLOCK_SIZES[index]);
|
||||||
|
let new_node_ptr = ptr as *mut ListNode;
|
||||||
|
unsafe {
|
||||||
|
new_node_ptr.write(new_node);
|
||||||
|
allocator.list_heads[index] = Some(&mut *new_node_ptr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
let ptr = NonNull::new(ptr).unwrap();
|
||||||
|
unsafe {
|
||||||
|
allocator.fallback_allocator.deallocate(ptr, layout);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
151
src/allocator/linked_list.rs
Normal file
151
src/allocator/linked_list.rs
Normal file
@@ -0,0 +1,151 @@
|
|||||||
|
use super::{Locked, align_up};
|
||||||
|
use alloc::alloc::{GlobalAlloc, Layout};
|
||||||
|
use core::{mem, ptr};
|
||||||
|
|
||||||
|
struct ListNode {
|
||||||
|
size: usize,
|
||||||
|
next: Option<&'static mut ListNode>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ListNode {
|
||||||
|
const fn new(size: usize) -> Self {
|
||||||
|
ListNode { size, next: None }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn start_addr(&self) -> usize {
|
||||||
|
self as *const Self as usize
|
||||||
|
}
|
||||||
|
|
||||||
|
fn end_addr(&self) -> usize {
|
||||||
|
self.start_addr() + self.size
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct LinkedListAllocator {
|
||||||
|
head: ListNode,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl LinkedListAllocator {
|
||||||
|
/// Creates an empty LinkedListAllocator.
|
||||||
|
pub const fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
head: ListNode::new(0),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Initialize the allocator with the given heap bounds.
|
||||||
|
///
|
||||||
|
/// This function is unsafe because the caller must guarantee that the given
|
||||||
|
/// heap bounds are valid and that the heap is unused. This method must be
|
||||||
|
/// called only once.
|
||||||
|
pub unsafe fn init(&mut self, heap_start: usize, heap_size: usize) {
|
||||||
|
unsafe {
|
||||||
|
self.add_free_region(heap_start, heap_size);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Adds the given memory region to the front of the list.
|
||||||
|
unsafe fn add_free_region(&mut self, addr: usize, size: usize) {
|
||||||
|
// ensure that the freed region is capable of holding ListNode
|
||||||
|
assert_eq!(align_up(addr, mem::align_of::<ListNode>()), addr);
|
||||||
|
assert!(size >= mem::size_of::<ListNode>());
|
||||||
|
|
||||||
|
// create a new list node and append it at the start of the list
|
||||||
|
let mut node = ListNode::new(size);
|
||||||
|
node.next = self.head.next.take();
|
||||||
|
let node_ptr = addr as *mut ListNode;
|
||||||
|
unsafe {
|
||||||
|
node_ptr.write(node);
|
||||||
|
self.head.next = Some(&mut *node_ptr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Looks for a free region with the given size and alignment and removes
|
||||||
|
/// it from the list.
|
||||||
|
///
|
||||||
|
/// Returns a tuple of the list node and the start address of the allocation.
|
||||||
|
fn find_region(&mut self, size: usize, align: usize) -> Option<(&'static mut ListNode, usize)> {
|
||||||
|
// reference to current list node, updated for each iteration
|
||||||
|
let mut current = &mut self.head;
|
||||||
|
// look for a large enough memory region in linked list
|
||||||
|
while let Some(ref mut region) = current.next {
|
||||||
|
if let Ok(alloc_start) = Self::alloc_from_region(®ion, size, align) {
|
||||||
|
// region suitable for allocation -> remove node from list
|
||||||
|
let next = region.next.take();
|
||||||
|
let ret = Some((current.next.take().unwrap(), alloc_start));
|
||||||
|
current.next = next;
|
||||||
|
return ret;
|
||||||
|
} else {
|
||||||
|
// region not suitable -> continue with next region
|
||||||
|
current = current.next.as_mut().unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// no suitable region found
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Try to use the given region for an allocation with given size and alignment.
|
||||||
|
///
|
||||||
|
/// Returns the allocation start address on success.
|
||||||
|
fn alloc_from_region(region: &ListNode, size: usize, align: usize) -> Result<usize, ()> {
|
||||||
|
let alloc_start = align_up(region.start_addr(), align);
|
||||||
|
let alloc_end = alloc_start.checked_add(size).ok_or(())?;
|
||||||
|
|
||||||
|
if alloc_end > region.end_addr() {
|
||||||
|
// region too small
|
||||||
|
return Err(());
|
||||||
|
}
|
||||||
|
|
||||||
|
let excess_size = region.end_addr() - alloc_end;
|
||||||
|
if excess_size > 0 && excess_size < mem::size_of::<ListNode>() {
|
||||||
|
// rest of region too small to hold a ListNode (required because the
|
||||||
|
// allocation splits the region in a used and a free part)
|
||||||
|
return Err(());
|
||||||
|
}
|
||||||
|
|
||||||
|
// region suitable for allocation
|
||||||
|
Ok(alloc_start)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Adjust the given layout so that the resulting allocated memory
|
||||||
|
/// region is also capable of storing a `ListNode`.
|
||||||
|
///
|
||||||
|
/// Returns the adjusted size and alignment as a (size, align) tuple.
|
||||||
|
fn size_align(layout: Layout) -> (usize, usize) {
|
||||||
|
let layout = layout
|
||||||
|
.align_to(mem::align_of::<ListNode>())
|
||||||
|
.expect("adjusting alignment failed")
|
||||||
|
.pad_to_align();
|
||||||
|
let size = layout.size().max(mem::size_of::<ListNode>());
|
||||||
|
(size, layout.align())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
unsafe impl GlobalAlloc for Locked<LinkedListAllocator> {
|
||||||
|
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
|
||||||
|
// perform layout adjustments
|
||||||
|
let (size, align) = LinkedListAllocator::size_align(layout);
|
||||||
|
let mut allocator = self.lock();
|
||||||
|
|
||||||
|
if let Some((region, alloc_start)) = allocator.find_region(size, align) {
|
||||||
|
let alloc_end = alloc_start.checked_add(size).expect("overflow");
|
||||||
|
let excess_size = region.end_addr() - alloc_end;
|
||||||
|
if excess_size > 0 {
|
||||||
|
unsafe {
|
||||||
|
allocator.add_free_region(alloc_end, excess_size);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
alloc_start as *mut u8
|
||||||
|
} else {
|
||||||
|
ptr::null_mut()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
|
||||||
|
// perform layout adjustments
|
||||||
|
let (size, _) = LinkedListAllocator::size_align(layout);
|
||||||
|
|
||||||
|
unsafe { self.lock().add_free_region(ptr as usize, size) }
|
||||||
|
}
|
||||||
|
}
|
||||||
12
src/lib.rs
12
src/lib.rs
@@ -5,10 +5,13 @@
|
|||||||
#![test_runner(crate::test_runner)]
|
#![test_runner(crate::test_runner)]
|
||||||
#![reexport_test_harness_main = "test_main"]
|
#![reexport_test_harness_main = "test_main"]
|
||||||
|
|
||||||
|
extern crate alloc;
|
||||||
use core::panic::PanicInfo;
|
use core::panic::PanicInfo;
|
||||||
|
|
||||||
|
pub mod allocator;
|
||||||
pub mod gdt;
|
pub mod gdt;
|
||||||
pub mod interrupts;
|
pub mod interrupts;
|
||||||
|
pub mod memory;
|
||||||
pub mod serial;
|
pub mod serial;
|
||||||
pub mod vga_buffer;
|
pub mod vga_buffer;
|
||||||
|
|
||||||
@@ -70,10 +73,15 @@ pub fn hlt_loop() -> ! {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
use bootloader::{BootInfo, entry_point};
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
entry_point!(test_kernel_main);
|
||||||
|
|
||||||
/// Entry point for `cargo xtest`
|
/// Entry point for `cargo xtest`
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
#[unsafe(no_mangle)]
|
fn test_kernel_main(_boot_info: &'static BootInfo) -> ! {
|
||||||
pub extern "C" fn _start() -> ! {
|
|
||||||
init();
|
init();
|
||||||
test_main();
|
test_main();
|
||||||
hlt_loop();
|
hlt_loop();
|
||||||
|
|||||||
45
src/main.rs
45
src/main.rs
@@ -4,20 +4,51 @@
|
|||||||
#![test_runner(blog_os::test_runner)]
|
#![test_runner(blog_os::test_runner)]
|
||||||
#![reexport_test_harness_main = "test_main"]
|
#![reexport_test_harness_main = "test_main"]
|
||||||
|
|
||||||
|
extern crate alloc;
|
||||||
|
|
||||||
|
use alloc::{boxed::Box, rc::Rc, vec, vec::Vec};
|
||||||
use blog_os::println;
|
use blog_os::println;
|
||||||
|
use bootloader::{BootInfo, entry_point};
|
||||||
use core::panic::PanicInfo;
|
use core::panic::PanicInfo;
|
||||||
|
|
||||||
#[unsafe(no_mangle)]
|
entry_point!(kernel_main);
|
||||||
pub extern "C" fn _start() -> ! {
|
|
||||||
use x86_64::registers::control::Cr3;
|
|
||||||
println!("Hello World{}", "!");
|
|
||||||
|
|
||||||
|
fn kernel_main(boot_info: &'static BootInfo) -> ! {
|
||||||
|
use blog_os::allocator;
|
||||||
|
use blog_os::memory::{self, BootInfoFrameAllocator};
|
||||||
|
use x86_64::VirtAddr;
|
||||||
|
|
||||||
|
println!("Hello World{}", "!");
|
||||||
blog_os::init();
|
blog_os::init();
|
||||||
|
|
||||||
let (level_4_page_table, _) = Cr3::read();
|
let phys_mem_offset = VirtAddr::new(boot_info.physical_memory_offset);
|
||||||
|
let mut mapper = unsafe { memory::init(phys_mem_offset) };
|
||||||
|
let mut frame_allocator = unsafe { BootInfoFrameAllocator::init(&boot_info.memory_map) };
|
||||||
|
|
||||||
|
allocator::init_heap(&mut mapper, &mut frame_allocator).expect("heap initialization failed");
|
||||||
|
|
||||||
|
// allocate a number on the heap
|
||||||
|
let heap_value = Box::new(41);
|
||||||
|
println!("heap_value at {:p}", heap_value);
|
||||||
|
|
||||||
|
// create a dynamically sized vector
|
||||||
|
let mut vec = Vec::new();
|
||||||
|
for i in 0..500 {
|
||||||
|
vec.push(i);
|
||||||
|
}
|
||||||
|
println!("vec at {:p}", vec.as_slice());
|
||||||
|
|
||||||
|
// create a reference counted vector -> will be freed when count reaches 0
|
||||||
|
let reference_counted = Rc::new(vec![1, 2, 3]);
|
||||||
|
let cloned_reference = reference_counted.clone();
|
||||||
println!(
|
println!(
|
||||||
"Level 4 page table at: {:?}",
|
"current reference count is {}",
|
||||||
level_4_page_table.start_address()
|
Rc::strong_count(&cloned_reference)
|
||||||
|
);
|
||||||
|
core::mem::drop(reference_counted);
|
||||||
|
println!(
|
||||||
|
"reference count is {} now",
|
||||||
|
Rc::strong_count(&cloned_reference)
|
||||||
);
|
);
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|||||||
86
src/memory.rs
Normal file
86
src/memory.rs
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
use bootloader::bootinfo::{MemoryMap, MemoryRegionType};
|
||||||
|
use x86_64::{
|
||||||
|
PhysAddr, VirtAddr,
|
||||||
|
structures::paging::{FrameAllocator, OffsetPageTable, PageTable, PhysFrame, Size4KiB},
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Initialize a new OffsetPageTable.
|
||||||
|
///
|
||||||
|
/// This function is unsafe because the caller must guarantee that the
|
||||||
|
/// complete physical memory is mapped to virtual memory at the passed
|
||||||
|
/// `physical_memory_offset`. Also, this function must be only called once
|
||||||
|
/// to avoid aliasing `&mut` references (which is undefined behavior).
|
||||||
|
pub unsafe fn init(physical_memory_offset: VirtAddr) -> OffsetPageTable<'static> {
|
||||||
|
unsafe {
|
||||||
|
let level_4_table = active_level_4_table(physical_memory_offset);
|
||||||
|
OffsetPageTable::new(level_4_table, physical_memory_offset)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns a mutable reference to the active level 4 table.
|
||||||
|
///
|
||||||
|
/// This function is unsafe because the caller must guarantee that the
|
||||||
|
/// complete physical memory is mapped to virtual memory at the passed
|
||||||
|
/// `physical_memory_offset`. Also, this function must be only called once
|
||||||
|
/// to avoid aliasing `&mut` references (which is undefined behavior).
|
||||||
|
unsafe fn active_level_4_table(physical_memory_offset: VirtAddr) -> &'static mut PageTable {
|
||||||
|
use x86_64::registers::control::Cr3;
|
||||||
|
|
||||||
|
let (level_4_table_frame, _) = Cr3::read();
|
||||||
|
|
||||||
|
let phys = level_4_table_frame.start_address();
|
||||||
|
let virt = physical_memory_offset + phys.as_u64();
|
||||||
|
let page_table_ptr: *mut PageTable = virt.as_mut_ptr();
|
||||||
|
|
||||||
|
unsafe { &mut *page_table_ptr }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A FrameAllocator that always returns `None`.
|
||||||
|
pub struct EmptyFrameAllocator;
|
||||||
|
|
||||||
|
unsafe impl FrameAllocator<Size4KiB> for EmptyFrameAllocator {
|
||||||
|
fn allocate_frame(&mut self) -> Option<PhysFrame> {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A FrameAllocator that returns usable frames from the bootloader's memory map.
|
||||||
|
pub struct BootInfoFrameAllocator {
|
||||||
|
memory_map: &'static MemoryMap,
|
||||||
|
next: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl BootInfoFrameAllocator {
|
||||||
|
/// Create a FrameAllocator from the passed memory map.
|
||||||
|
///
|
||||||
|
/// This function is unsafe because the caller must guarantee that the passed
|
||||||
|
/// memory map is valid. The main requirement is that all frames that are marked
|
||||||
|
/// as `USABLE` in it are really unused.
|
||||||
|
pub unsafe fn init(memory_map: &'static MemoryMap) -> Self {
|
||||||
|
BootInfoFrameAllocator {
|
||||||
|
memory_map,
|
||||||
|
next: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns an iterator over the usable frames specified in the memory map.
|
||||||
|
fn usable_frames(&self) -> impl Iterator<Item = PhysFrame> {
|
||||||
|
// get usable regions from memory map
|
||||||
|
let regions = self.memory_map.iter();
|
||||||
|
let usable_regions = regions.filter(|r| r.region_type == MemoryRegionType::Usable);
|
||||||
|
// map each region to its address range
|
||||||
|
let addr_ranges = usable_regions.map(|r| r.range.start_addr()..r.range.end_addr());
|
||||||
|
// transform to an iterator of frame start addresses
|
||||||
|
let frame_addresses = addr_ranges.flat_map(|r| r.step_by(4096));
|
||||||
|
// create `PhysFrame` types from the start addresses
|
||||||
|
frame_addresses.map(|addr| PhysFrame::containing_address(PhysAddr::new(addr)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
unsafe impl FrameAllocator<Size4KiB> for BootInfoFrameAllocator {
|
||||||
|
fn allocate_frame(&mut self) -> Option<PhysFrame> {
|
||||||
|
let frame = self.usable_frames().nth(self.next);
|
||||||
|
self.next += 1;
|
||||||
|
frame
|
||||||
|
}
|
||||||
|
}
|
||||||
70
tests/heap_allocation.rs
Normal file
70
tests/heap_allocation.rs
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
#![no_std]
|
||||||
|
#![no_main]
|
||||||
|
#![feature(custom_test_frameworks)]
|
||||||
|
#![test_runner(blog_os::test_runner)]
|
||||||
|
#![reexport_test_harness_main = "test_main"]
|
||||||
|
|
||||||
|
extern crate alloc;
|
||||||
|
|
||||||
|
use alloc::{boxed::Box, vec::Vec};
|
||||||
|
use blog_os::allocator::HEAP_SIZE;
|
||||||
|
use bootloader::{BootInfo, entry_point};
|
||||||
|
use core::panic::PanicInfo;
|
||||||
|
|
||||||
|
entry_point!(main);
|
||||||
|
|
||||||
|
fn main(boot_info: &'static BootInfo) -> ! {
|
||||||
|
use blog_os::allocator;
|
||||||
|
use blog_os::memory::{self, BootInfoFrameAllocator};
|
||||||
|
use x86_64::VirtAddr;
|
||||||
|
|
||||||
|
blog_os::init();
|
||||||
|
let phys_mem_offset = VirtAddr::new(boot_info.physical_memory_offset);
|
||||||
|
let mut mapper = unsafe { memory::init(phys_mem_offset) };
|
||||||
|
let mut frame_allocator = unsafe { BootInfoFrameAllocator::init(&boot_info.memory_map) };
|
||||||
|
allocator::init_heap(&mut mapper, &mut frame_allocator).expect("heap initialization failed");
|
||||||
|
|
||||||
|
test_main();
|
||||||
|
loop {}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test_case]
|
||||||
|
fn simple_allocation() {
|
||||||
|
let heap_value_1 = Box::new(41);
|
||||||
|
let heap_value_2 = Box::new(13);
|
||||||
|
assert_eq!(*heap_value_1, 41);
|
||||||
|
assert_eq!(*heap_value_2, 13);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test_case]
|
||||||
|
fn large_vec() {
|
||||||
|
let n = 1000;
|
||||||
|
let mut vec = Vec::new();
|
||||||
|
for i in 0..n {
|
||||||
|
vec.push(i);
|
||||||
|
}
|
||||||
|
assert_eq!(vec.iter().sum::<u64>(), (n - 1) * n / 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test_case]
|
||||||
|
fn many_boxes() {
|
||||||
|
for i in 0..HEAP_SIZE {
|
||||||
|
let x = Box::new(i);
|
||||||
|
assert_eq!(*x, i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test_case]
|
||||||
|
fn many_boxes_long_lived() {
|
||||||
|
let long_lived = Box::new(1); // new
|
||||||
|
for i in 0..HEAP_SIZE {
|
||||||
|
let x = Box::new(i);
|
||||||
|
assert_eq!(*x, i);
|
||||||
|
}
|
||||||
|
assert_eq!(*long_lived, 1); // new
|
||||||
|
}
|
||||||
|
|
||||||
|
#[panic_handler]
|
||||||
|
fn panic(info: &PanicInfo) -> ! {
|
||||||
|
blog_os::test_panic_handler(info)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user