Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

boot: convert paging from assembly to Zig (take 2) #23

Merged
merged 54 commits into from
May 3, 2024
Merged
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
897e7ef
zasm: add util functions for control and model specific registers
mewmew May 23, 2022
82dc1ae
zasm: update Zig syntax to version 0.11.0
mewmew Apr 15, 2024
23378ed
boot: rewrite assembly version of enable_paging in Zig
mewmew Apr 15, 2024
6460ab1
boot: remove unused print32.asm
mewmew May 23, 2022
ea38491
boot: remove unused protected_mode.asm
mewmew May 23, 2022
0b9c138
boot: remove unused print.asm
mewmew May 23, 2022
7602dd5
zasm: add PageTableEntry and PageTableFlags from x86_64 repo
mewmew May 23, 2022
95adbb1
zasm: update Zig syntax to version 0.11.0 (part 2)
mewmew Apr 15, 2024
4cf2039
boot: ensure that no MMX instructions are used in 32-bit boot code
mewmew May 23, 2022
57f6361
boot: rewrite assembly version of map_frame_buffer in Zig
mewmew May 23, 2022
9428ed1
boot: rewrite assembly version of map_kernel_stack in Zig
mewmew May 23, 2022
03341c2
boot: use dedicated functions to map kernel code and data segment pages
mewmew May 23, 2022
11fa41f
boot: rewrite assembly version of set_up_page_tables in Zig
mewmew May 23, 2022
cef2b14
boot: make paging constants public and move into dedicated source file
mewmew May 24, 2022
da51fe0
makefile: add TODO to remove objcopy hack
mewmew May 25, 2022
1c8db56
boot: add TODO to remove 4xNOP hack in init_long_mode
mewmew May 25, 2022
2ccf788
boot/paging: use const instead of var where applicable
mewmew May 25, 2022
a7453f2
boot/paging: move load of P4 table (Cr3) closer to enable paging code…
mewmew May 25, 2022
1a46edc
boot/paging: assert that long mode active is set after enable_paging
mewmew May 25, 2022
3ced6de
zasm: add TODO to re-enable assert in setAddr
mewmew May 25, 2022
7ffb38c
notes: add logbook entry for ld bug
mewmew May 26, 2022
79fde45
boot: make kernel_data_seg executable for userland code and data (TEMP!)
mewmew Apr 15, 2024
6c37760
update to use Zig 0.12
mewmew Apr 26, 2024
c64bd0b
add TODO for adding guard page in between kernel memory and heap
mewmew Apr 26, 2024
233741f
paging: remove user access from kernel stack and framebuffer
mewmew Apr 26, 2024
ff3a207
map userland
mewmew Apr 26, 2024
4621476
make kernel heap address less hard-coded
mewmew Apr 26, 2024
d1be613
copy userland code into userland segment
mewmew Apr 26, 2024
26d6a3c
switch to userland stack
mewmew Apr 26, 2024
f231798
remove user accessability from kernel code segment
mewmew Apr 27, 2024
20227f4
remove user accessibility from kernel data segment, userland now in u…
mewmew Apr 27, 2024
c4bb0c1
fix C compiler warnings
mewmew Apr 27, 2024
3319c06
tss64: dynamically updated tss64 addr
karlek Apr 27, 2024
35ef412
make: Remove bin/fat32.img remnants
karlek Apr 29, 2024
aa33d53
paging: nice virtual ASCII memory mappings.
karlek Apr 29, 2024
bb9344c
run/debug: Increase memory demands from 128M -> 4G
karlek Apr 29, 2024
9827d8c
debug: ctrl-c now possible in gdb
karlek Apr 29, 2024
ebb27d7
ci: add lld dependency
karlek Apr 29, 2024
cbe7ff0
lint: appease the CI gods
karlek Apr 29, 2024
2c03749
make: Remove last bin/fat32.img sneaky boi
karlek Apr 29, 2024
b1e3739
paging: bootstrap is chonky
karlek May 1, 2024
2c9377c
debug: remove debug.py and use gef-remote
karlek May 1, 2024
eb47f68
qemu: add +smep and +smap capabilities to cpu
karlek May 1, 2024
2b9cc92
long_mode: Actually use the stack..
karlek May 1, 2024
f94d6ca
idt: save _all_ registers and flags
karlek May 1, 2024
42c6374
malloc: print heap status and size on oom
karlek May 1, 2024
687f330
kernel/heap: enable smep
karlek May 1, 2024
5b06ee0
debug: tidy up debug.sh
karlek May 2, 2024
c99bd02
elf2: handle file->data in kernel for SMEP
karlek May 2, 2024
5ff1444
kernel_userland: correctly handle syscalls
karlek May 2, 2024
a539b8d
userland: syscall with system v calling convention
karlek May 2, 2024
9c197c1
kernel_userland: bring userland mem into kernel
karlek May 2, 2024
a989439
userland: actually call sys_print
karlek May 2, 2024
866baf8
kernel: Enable SMAP
karlek May 2, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@ jobs:
- uses: actions/checkout@v3

- name: install dependencies
run: sudo apt install -y nasm clang mtools
run: sudo apt install -y nasm clang mtools lld

- name: install zig
run: sudo snap install zig --classic --beta
44 changes: 28 additions & 16 deletions Makefile
Original file line number Diff line number Diff line change
@@ -18,9 +18,25 @@ bin/kernel-userland.o: src/kernel/userland.asm | bin
-o $@ \
$<

# -O ReleaseFast
bin/boot_zig.o: src/boot/boot.zig | bin
zig build-obj -target x86_64-freestanding-gnu -static -I./src/kernel -mno-red-zone -femit-bin=$@ $<
# NOTE: use `-fcompiler-rt` to include __zig_probe_stack symbol when linking
# (see https://github.com/ziglang/zig/issues/6817).
bin/%_32_zig.o: src/boot/%_32.zig | bin
@zig build-obj \
--cache-dir bin/zig-cache \
-I src/kernel \
-mno-red-zone \
-fcompiler-rt \
-static \
-target x86-freestanding-gnu \
-O Debug \
-mcpu=i386 \
-femit-bin=$@ \
$<

# TODO: remove objcopy hack when we figure out a way to emit ELF64 objets
# containing 32-bit code in Zig (NOTE: how is this done in C??).
bin/%_elf64_zig.o: bin/%_32_zig.o
@objcopy --output-target elf64-x86-64 $< $@

# --nmagic
# Turn off page alignment of sections, and disable linking against shared
@@ -31,8 +47,10 @@ bin/boot_zig.o: src/boot/boot.zig | bin
# --no-warn-rwx-segments
# Allow use of RWX segments.

bin/kernel.elf: bin/boot_zig.o bin/boot.o bin/kernel-userland.o bin/kernel.o bin/libhello.o bin/libfloof.a | bin
ld \
# NOTE: using ld.lld instead of ld as work-around for bug in ld (for details,
# see https://github.com/karlek/lappis/pull/19/commits/9cf1a8a7d40c21c6a23c1bb0412a7f49f3f1b211#r883015557).
bin/kernel.elf: bin/boot_elf64_zig.o bin/paging_elf64_zig.o bin/boot.o bin/kernel-userland.o bin/kernel.o bin/libhello.o bin/libfloof.a | bin
ld.lld \
--nmagic \
--output $@ \
--script linker.ld \
@@ -43,8 +61,8 @@ bin/kernel.elf: bin/boot_zig.o bin/boot.o bin/kernel-userland.o bin/kernel.o bin
# TODO: When ubuntu-latest on github actions is updated, add this to remove nag.
# --no-warn-rwx-segments
# Allow use of RWX segments.
bin/kernel.dbg: bin/boot_zig.o bin/boot.o bin/kernel-userland.o bin/kernel.o bin/libhello.o bin/libfloof.a | bin
ld \
bin/kernel.dbg: bin/boot_elf64_zig.o bin/paging_elf64_zig.o bin/boot.o bin/kernel-userland.o bin/kernel.o bin/libhello.o bin/libfloof.a | bin
ld.lld \
--output $@ \
--script linker.ld \
$^
@@ -142,12 +160,6 @@ bin/zipfs.zip: fs/userland.elf | bin
bin/zipfs.img: bin/zipfs.zip | bin
dd if=bin/zipfs.zip of=$@ bs=1M conv=sync

bin/fat32.img: | bin
@dd if=/dev/zero of=$@ count=50 bs=1M conv=sync
@mkfs.vfat -F 32 $@
@mcopy -i $@ -s fs/* ::
@mdir -i $@ -s

bin:
@mkdir -p $@

@@ -165,12 +177,12 @@ bin/libfloof.a: src/kernel/rust/src/lib.rs
cd src/kernel/rust; cargo build -Zbuild-std ; cargo build
mv src/kernel/rust/target/os/debug/libfloof.a bin

build: bin/kernel.iso bin/kernel.dbg bin/zipfs.img bin/fat32.img
build: bin/kernel.iso bin/kernel.dbg bin/zipfs.img

debug: bin/kernel.iso bin/kernel.dbg bin/zipfs.img bin/fat32.img
debug: bin/kernel.iso bin/kernel.dbg bin/zipfs.img
./debug.sh

run: bin/kernel.iso bin/zipfs.img bin/fat32.img
run: bin/kernel.iso bin/zipfs.img
./run.sh

bin/kernel.iso: bin/kernel.elf grub.cfg | bin
12 changes: 4 additions & 8 deletions debug.gdb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
source debug.py

set disassembly-flavor intel

target remote localhost:1234
symbol-file bin/kernel.dbg

source ../gef/gef.py
source ../gdb-pt-dump/pt.py

gef-remote --qemu-user localhost 1234
# Any break points should be set here.
# break main
break enter_userland
@@ -13,8 +14,3 @@ break yay_userland

# Run until the program first breakpoint.
continue

# Add break points that will be added after our first breakpoint has been hit.
# break irq_timer
# break *irq_timer+24
# break not_implemented_0
140 changes: 0 additions & 140 deletions debug.py

This file was deleted.

17 changes: 9 additions & 8 deletions debug.sh
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
#!/bin/bash

set -e
set -ex

cat /dev/null > /tmp/serial.log
cat /dev/null > /tmp/serial.raw
kitty --class lappis-serial-raw -e fish -c 'tail -f /tmp/serial.raw' &
kitty --class lappis-serial-raw -e tail -f /tmp/serial.raw &
kitty --class lappis-serial-log -e tail -f /tmp/serial.log &
sleep 1

kitty --class qemu-starter \
kitty --class lappis-qemu \
qemu-system-x86_64 \
-cpu qemu64,+smep,+smap \
-no-reboot \
-no-shutdown \
-S \
-gdb tcp::1234 \
-d int \
-m size=128M \
-m size=4G \
-monitor stdio \
-serial file:/tmp/serial.log \
-serial file:/tmp/serial.raw \
-drive media=disk,index=0,file=bin/zipfs.img,format=raw,if=ide \
-drive media=disk,index=1,file=bin/fat32.img,format=raw,if=ide \
-cdrom bin/kernel.iso &

gdb \
sleep 1

kitty --class lappis-gdb -e gdb \
--quiet \
-command=debug.gdb

ps ax | grep kitty | grep lappis-serial-log | awk '{print $1}' | xargs kill
ps ax | grep kitty | grep lappis-serial-raw | awk '{print $1}' | xargs kill
ps ax | grep qemu-system-x86_64 | grep kernel\.iso | awk '{print $1}' | xargs kill
ps ax | grep kitty | grep lappis-qemu | awk '{print $1}' | xargs kill
6 changes: 6 additions & 0 deletions notes/logbook.md
Original file line number Diff line number Diff line change
@@ -11,3 +11,9 @@ Glitch art a la 1.44 MB style.
*earlyclobber get's the clobb*

Fix interrupt clobbering registers _semi-deterministically_.

# 2022-05-26

*Shellcoding on rollerblades like your in an 80is Hackers movie*

A bug in `ld` caused the NX bit of page table entries to be discarded.
3 changes: 1 addition & 2 deletions run.sh
Original file line number Diff line number Diff line change
@@ -13,11 +13,10 @@ qemu-system-x86_64 \
-no-reboot \
-no-shutdown \
-d int \
-m size=128M \
-m size=4G \
-serial file:/tmp/serial.log \
-serial file:/tmp/serial.raw \
-drive media=disk,index=0,file=bin/zipfs.img,format=raw,if=ide \
-drive media=disk,index=1,file=bin/fat32.img,format=raw,if=ide \
-monitor stdio \
-cdrom bin/kernel.iso

File renamed without changes.
Loading