From 29e28bc9c67378d0d9a7174dec6a0b541fb7f4d5 Mon Sep 17 00:00:00 2001 From: Tianhao Wang Date: Fri, 10 Mar 2023 19:26:31 +0100 Subject: a minimal working demo on bare metal --- src/arch/mod.rs | 1 + src/arch/x86_64/asm/io_port.s | 71 +++++++++++++++++++++++++++++++++++++++++ src/arch/x86_64/asm/startup.s | 12 +++---- src/arch/x86_64/cpu.rs | 0 src/arch/x86_64/io_port.rs | 0 src/arch/x86_64/linker.ld | 74 +++++++++++++++++++++++++++++++++++++++++++ src/arch/x86_64/mod.rs | 5 +++ src/arch/x86_64/pic.rs | 0 src/arch/x86_64/pit.rs | 0 src/arch/x86_64/plugbox.rs | 0 10 files changed, 157 insertions(+), 6 deletions(-) create mode 100644 src/arch/mod.rs create mode 100644 src/arch/x86_64/cpu.rs create mode 100644 src/arch/x86_64/io_port.rs create mode 100644 src/arch/x86_64/linker.ld create mode 100644 src/arch/x86_64/mod.rs create mode 100644 src/arch/x86_64/pic.rs create mode 100644 src/arch/x86_64/pit.rs create mode 100644 src/arch/x86_64/plugbox.rs (limited to 'src/arch') diff --git a/src/arch/mod.rs b/src/arch/mod.rs new file mode 100644 index 0000000..2a99bf5 --- /dev/null +++ b/src/arch/mod.rs @@ -0,0 +1 @@ +pub mod x86_64; diff --git a/src/arch/x86_64/asm/io_port.s b/src/arch/x86_64/asm/io_port.s index e69de29..9ddc4e6 100644 --- a/src/arch/x86_64/asm/io_port.s +++ b/src/arch/x86_64/asm/io_port.s @@ -0,0 +1,71 @@ +;*************************************************************************** +;* Operating-System Construction * +;*---------------------------------------------------------------------------* +;* * +;* I O _ P O R T * +;* * +;*---------------------------------------------------------------------------* +;* The functions defined here encapsulate the machine instructions 'in' and * +;* 'out' for class IO_Port. * +;***************************************************************************** + +; EXPORTED FUNCTIONS + +[GLOBAL outb] +[GLOBAL outw] +[GLOBAL inb] +[GLOBAL inw] + +; FUNCTION IMPLEMENTATIONS + +[SECTION .text] + +; OUTB: Byte-wise output via an I/O port. +; +; C prototype: void outb (int port, int value); + +outb: + push rbp + mov rbp, rsp + mov rdx, rdi + mov rax, rsi + out dx, al + pop rbp + ret + +; OUTW: Word-wise output via an I/O port. +; +; C prototype: void outw (int port, int value); + +outw: + push rbp + mov rbp, rsp + mov rdx, rdi + mov rax, rsi + out dx, ax + pop rbp + ret + +; INB: Byte-wise input via an I/O port. +; +; C prototype: unsigned char inb (int port); + +inb: + push rbp + mov rbp, rsp + mov rdx, rdi + in al, dx + pop rbp + ret + +; INW: Word-wise input via an I/O port. +; +; C prototype: unsigned short inw (int port); + +inw: + push rbp + mov rbp, rsp + mov rdx, rdi + in ax, dx + pop rbp + ret diff --git a/src/arch/x86_64/asm/startup.s b/src/arch/x86_64/asm/startup.s index 4c696e1..7b5bbff 100644 --- a/src/arch/x86_64/asm/startup.s +++ b/src/arch/x86_64/asm/startup.s @@ -17,7 +17,7 @@ ; Constants ; -; stack for the main function +; stack for the main function (renamed to _entry()) STACKSIZE: equ 65536 ; video memory base address @@ -59,8 +59,8 @@ pagetable_end: equ 0x200000 [GLOBAL _ZdlPvm] ; functions from the C parts of the system -[EXTERN main] -[EXTERN guardian] +[EXTERN _entry] +;[EXTERN guardian] ; addresses provided by the compiler [EXTERN ___BSS_START__] @@ -204,7 +204,7 @@ fill_tables3_done: ; system start, part 2 (in 64-bit Long Mode) ; ; This code clears the BSS segment and initializes IDT and PICs. Then the -; constructors of global C++ objects are called, and finally main() is run. +; constructors of global C++ objects are called, and finally _entry() is run. ; longmode_start: @@ -234,7 +234,7 @@ clear_bss: ;mov cr4, rax call _init ; call constructors of global objects - call main ; call the OS kernel's C / C++ part + call _entry ; call the OS kernel's C / C++ part call _fini ; call destructors cli ; Usually we should not get here. hlt @@ -279,7 +279,7 @@ wrapper_body: ; pass interrupt number as the first parameter mov rdi, rax - call guardian +; call guardian ; restore volatile registers pop r11 diff --git a/src/arch/x86_64/cpu.rs b/src/arch/x86_64/cpu.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/arch/x86_64/io_port.rs b/src/arch/x86_64/io_port.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/arch/x86_64/linker.ld b/src/arch/x86_64/linker.ld new file mode 100644 index 0000000..7a03770 --- /dev/null +++ b/src/arch/x86_64/linker.ld @@ -0,0 +1,74 @@ +SECTIONS +{ + . = 0x100000; /* system's start address */ + + .text : + { + *(".text") + *(".text.*") + *(".text$") + *(".init") + *(".fini") + *(".gnu.linkonce.*") + } + + .init_array : + { + PROVIDE_HIDDEN (__init_array_start = .); + KEEP (*(SORT(.init_array.*))) + KEEP (*(.init_array)) + KEEP (*(".ctors")) + KEEP (*(".ctor")) + PROVIDE_HIDDEN (__init_array_end = .); + } + + .fini_array : + { + PROVIDE_HIDDEN (__fini_array_start = .); + KEEP (*(SORT(.fini_array.*))) + KEEP (*(.fini_array)) + KEEP (*(".dtors")) + KEEP (*(".dtor")) + PROVIDE_HIDDEN (__fini_array_end = .); + } + + .data : + { + *(".data") + *(".data$") + *(".rodata") + *(".rodata.*") + *(".got") + *(".got.plt") + *(".eh_frame") + *(".eh_fram") + *(".jcr") + *(".note.*") + } + + .bss : + { + ___BSS_START__ = .; + *(".bss") + *(".bss.*") + ___BSS_END__ = .; + } + + /* global page table for 64-bit long mode */ + .global_pagetable ALIGN(4096) (NOLOAD) : + { + *(".global_pagetable") + } + +/* + /DISCARD/ : + { + *(".note") + *(".comment") + *(".debug_line") + *(".debug_info") + *(".debug_abbrev") + *(".debug_aranges") + } +*/ +} diff --git a/src/arch/x86_64/mod.rs b/src/arch/x86_64/mod.rs new file mode 100644 index 0000000..ef06437 --- /dev/null +++ b/src/arch/x86_64/mod.rs @@ -0,0 +1,5 @@ +pub mod io_port; +pub mod pic; +pub mod pit; +pub mod plugbox; +pub mod cpu; diff --git a/src/arch/x86_64/pic.rs b/src/arch/x86_64/pic.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/arch/x86_64/pit.rs b/src/arch/x86_64/pit.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/arch/x86_64/plugbox.rs b/src/arch/x86_64/plugbox.rs new file mode 100644 index 0000000..e69de29 -- cgit v1.2.3-70-g09d2