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

@@ -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
mypid: resq 1
mychild: resq 1
mymessage:
resq 1024
extern main
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
_start:
xor rbp, rbp ; Sentinel rbp
push rbp
push rbp
mov rbp, rsp
mov rax, 5 ; GETPID syscall
syscall ; int 0xee
mov [mypid], rax
mov rdi, 0
mov rsi, 0
call main
mov rax, 8 ; FORK syscall
syscall ; int 0xee
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
mov rdi, rax
call __localexit

View File

@@ -46,6 +46,7 @@ apic::apic(uint32_t *base) :
lapic::lapic(uint32_t *base, isr spurious) :
apic(base)
{
// TODO: This causes a "reserved" page fault under KVM
apic_write(m_base, 0xf0, static_cast<uint32_t>(spurious));
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->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();
p->wait_on_time(regs.rbx);
p->wait_on_time(regs.rdi);
return_rsp = s.schedule(return_rsp);
}
break;