Make nulldrv a small C++ program

This commit is contained in:
Justin C. Miller
2019-03-24 13:44:25 -07:00
parent b18243f098
commit ed3f9410a6
8 changed files with 134 additions and 67 deletions

View File

@@ -14,10 +14,10 @@ source = "../assets/fonts/tamsyn8x16r.psf"
[[files]] [[files]]
dest = "nulldrv1" dest = "nulldrv1"
source = "host/nulldrv" source = "user/nulldrv"
executable = true executable = true
[[files]] [[files]]
dest = "nulldrv2" dest = "nulldrv2"
source = "host/nulldrv" source = "user/nulldrv"
executable = true executable = true

View File

@@ -56,9 +56,10 @@ boot:
nulldrv: nulldrv:
kind: exe kind: exe
target: host target: user
output: nulldrv output: nulldrv
source: source:
- src/drivers/nulldrv/main.cpp
- src/drivers/nulldrv/main.s - src/drivers/nulldrv/main.s
elf: elf:

View File

@@ -179,7 +179,7 @@ build $builddir/fatroot/efi/boot/bootx64.efi : cp $builddir/boot/boot.efi
build $builddir/fatroot/initrd.img : makerd ${srcroot}/assets/initrd.toml | $ build $builddir/fatroot/initrd.img : makerd ${srcroot}/assets/initrd.toml | $
${builddir}/native/makerd $ ${builddir}/native/makerd $
${builddir}/host/nulldrv ${builddir}/user/nulldrv
build $builddir/popcorn.img : makefat | $ build $builddir/popcorn.img : makefat | $
$builddir/fatroot/initrd.img $ $builddir/fatroot/initrd.img $

View File

@@ -0,0 +1,49 @@
{% extends "target.default.j2" %}
{% block binaries %}
cc = ${srcroot}/sysroot/bin/clang
cxx = ${srcroot}/sysroot/bin/clang++
ld = ${srcroot}/sysroot/bin/x86_64-elf-ld
ar = ${srcroot}/sysroot/bin/x86_64-elf-ar
nasm = ${srcroot}/sysroot/bin/nasm
objcopy = ${srcroot}/sysroot/bin/x86_64-elf-objcopy
{% endblock %}
{% block variables %}
ccflags = $ccflags $
-nostdlib $
-nodefaultlibs $
-fno-builtin $
-mno-sse $
-fno-omit-frame-pointer $
-mno-red-zone $
-g $
-mcmodel=large $
-D__ELF__ $
-D__POPCORN__ $
-isystem${srcroot}/sysroot/include $
--sysroot="${srcroot}/sysroot"
cxxflags = $cxxflags $
-fno-exceptions $
-fno-rtti $
-isystem${srcroot}/sysroot/include/c++/v1
ldflags = $ldflags $
-g $
-nostdlib $
-znocombreloc $
-Bsymbolic $
-nostartfiles $
-Bstatic $
--sysroot="${srcroot}/sysroot" $
-L "${srcroot}/sysroot/lib" $
libs = $libs $
-lc
{% endblock %}
# vim: ft=ninja et ts=4 sts=4 sw=4

View File

@@ -1 +1,22 @@
int main(int argc, const char **argv) { return 0; } #include <stdint.h>
#include <stdlib.h>
extern "C" {
int32_t getpid();
void sleep(uint64_t til);
void debug();
int main(int, const char **);
}
int
main(int argc, const char **argv)
{
int32_t pid = getpid();
debug();
for (int i = 1; i < 5; ++i)
sleep(i*10);
debug();
return 0;
}

View File

@@ -1,68 +1,63 @@
section .bss section .bss
mypid: resq 1 mymessage:
mychild: resq 1 resq 1024
extern main
section .text section .text
global getpid
getpid:
push rbp
mov rbp, rsp
mov rax, 5 ; getpid syscall
syscall ; pid is now already in rax, so just return
pop rbp
ret
global debug
debug:
push rbp
mov rbp, rsp
mov rax, 1 ; debug syscall
syscall
pop rbp
ret
global sleep
sleep:
push rbp
mov rbp, rsp
mov rax, 4 ; sleep syscall
syscall
pop rbp
ret
__localexit:
push rbp
mov rbp, rsp
mov rax, 9 ; exit syscall
syscall
jmp __localexit ; shouldn't get here
global _start global _start
_start: _start:
xor rbp, rbp ; Sentinel rbp xor rbp, rbp ; Sentinel rbp
push rbp
push rbp
mov rbp, rsp
mov rax, 5 ; GETPID syscall mov rdi, 0
syscall ; int 0xee mov rsi, 0
mov [mypid], rax call main
mov rax, 8 ; FORK syscall mov rdi, rax
syscall ; int 0xee call __localexit
mov [mychild], rax
mov r12, [mypid]
mov r13, [mychild]
mov rax, 1 ; DEBUG syscall
syscall ; int 0xee
cmp r13, 0
je .doexit
cmp r12, 1
je .dosend
jne .doreceive
.preloop:
mov r11, 0 ; counter
mov rbx, 20 ; sleep timeout
.loop:
mov rax, 1 ; MESSAGE syscall
;mov rax, 0 ; NOOP syscall
;syscall
syscall ; int 0xee
inc r11
cmp r11, 2
jle .loop
mov rax, 4 ; SLEEP syscall
; syscall
syscall ; int 0xee
add rbx, 20
mov r11, 0
jmp .loop
.dosend:
mov rax, 6 ; SEND syscall
mov rdi, 2 ; target is pid 2
syscall ; int 0xee
jmp .preloop
.doreceive:
mov rax, 7 ; RECEIVE syscall
mov rdi, 1 ; source is pid 2
syscall ; int 0xee
jmp .preloop
.doexit:
mov rax, 9 ; EXIT syscall
syscall

View File

@@ -46,6 +46,7 @@ apic::apic(uint32_t *base) :
lapic::lapic(uint32_t *base, isr spurious) : lapic::lapic(uint32_t *base, isr spurious) :
apic(base) apic(base)
{ {
// TODO: This causes a "reserved" page fault under KVM
apic_write(m_base, 0xf0, static_cast<uint32_t>(spurious)); apic_write(m_base, 0xf0, static_cast<uint32_t>(spurious));
log::info(logs::apic, "LAPIC created, base %lx", m_base); log::info(logs::apic, "LAPIC created, base %lx", m_base);
} }

View File

@@ -79,10 +79,10 @@ syscall_dispatch(uintptr_t return_rsp, cpu_state &regs)
{ {
cons->set_color(11); cons->set_color(11);
cons->printf("\nProcess %d: Received SLEEP syscall\n", p->pid); cons->printf("\nProcess %d: Received SLEEP syscall\n", p->pid);
cons->printf("Sleeping until %lu\n", regs.rbx); cons->printf("Sleeping until %lu\n", regs.rdi);
cons->set_color(); cons->set_color();
p->wait_on_time(regs.rbx); p->wait_on_time(regs.rdi);
return_rsp = s.schedule(return_rsp); return_rsp = s.schedule(return_rsp);
} }
break; break;