tomoyuki-nakabayashi / MyPlayGround

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[BlogOS]メモリ管理追加

tomoyuki-nakabayashi opened this issue · comments

https://os.phil-opp.com/first-edition/

1st editionのメモリ管理を参考にしよう。

https://github.com/thepowersgang/rust_os

これやばいな。一人でやってんのか。

bootimageの中身を覗いてみよう。ブートイメージを自作したい。

xargo build --target x86_64-blog_os
   Compiling volatile v0.2.4
error: Error loading target specification: Could not find specification for target "x86_64-blog_os"
  |
  = help: Use `--print target-list` for a list of built-in targets

error: Could not compile `volatile`.

To learn more, run the command again with --verbose.

上になったが、↓で解決した。

export RUST_TARGET_PATH=`pwd`

alloc周りがずいぶん変わっている。まとめるか。

rust-lang/rust#51540

alloc_handlerを使うようになった。このあたりはpanicと一緒ね。

undefined reference to `memcpy'

ふむ…。

raw memoryの操作はrlibcにある。

[dependencies]
rlibc = "1.0"
extern crate rlibc;

む、sectionがalignmentされなくなった。

objdump -h build/kernel-x86_64.bin

build/kernel-x86_64.bin:     file format elf64-x86-64

Sections:
Idx Name          Size      VMA               LMA               File off  Algn
  0 .rodata       00017000  0000000000100000  0000000000100000  00001000  2**12
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  1 .debug_gdb_scripts 00000a3a  0000000000117000  0000000000117000  00018000  2**0
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  2 .eh_frame     00000078  0000000000117a40  0000000000117a40  00018a40  2**3
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  3 .text         00034000  0000000000118000  0000000000118000  00019000  2**12
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
  4 .data         00007000  000000000014c000  000000000014c000  0004d000  2**12
                  CONTENTS, ALLOC, LOAD, DATA
  5 .bss          00007000  0000000000153000  0000000000153000  00054000  2**12
                  ALLOC
  6 .got          00001000  000000000015a000  000000000015a000  00054000  2**12
                  CONTENTS, ALLOC, LOAD, DATA
  7 .got.plt      00001000  000000000015b000  000000000015b000  00055000  2**12
                  CONTENTS, ALLOC, LOAD, DATA
  8 .data.rel.ro  00000000  000000000015c000  000000000015c000  00056000  2**12
                  CONTENTS, ALLOC, LOAD, DATA
  9 .gcc_except_table 00000000  000000000015c000  000000000015c000  00056000  2**12

eh_frameというセクションが増えているな。

stackトレースのための領域らしい。
rlibcが使うのかね?

https://github.com/phil-opp/rust-once/blob/master/src/lib.rs

#[macro_export]
macro_rules! assert_has_not_been_called {
    () => {
        assert_has_not_been_called!("assertion failed: has_run == false");
    };
    ($($arg:tt)+) => {{
        fn assert_has_not_been_called() {
            use $crate::__core::sync::atomic::{AtomicBool, ATOMIC_BOOL_INIT, Ordering};
            static CALLED: AtomicBool = ATOMIC_BOOL_INIT;
            let called = CALLED.swap(true, Ordering::Relaxed);
            assert!(called == false, $($arg)+);
        }
        assert_has_not_been_called();
    }};
}

なるほど。AtomicBoolを作って、callされたことがあるかどうかをassertすると。
swapでfalse(初期値)が返ってきた場合だけassert!()で落ちない。