Proper userspace program

This commit is contained in:
apio 2022-09-29 20:06:18 +02:00
parent 522d74b65d
commit ef5994e389
5 changed files with 71 additions and 15 deletions

View File

@ -6,6 +6,8 @@
#define SYS_yield 1 #define SYS_yield 1
#define SYS_sleep 2 #define SYS_sleep 2
#define SYS_write 3 #define SYS_write 3
#define SYS_paint 4
#define SYS_rand 5
namespace Syscall namespace Syscall
{ {
@ -15,4 +17,6 @@ namespace Syscall
void sys_exit(Context* context); void sys_exit(Context* context);
void sys_yield(Context* context); void sys_yield(Context* context);
void sys_sleep(Context* context, uint64_t ms); void sys_sleep(Context* context, uint64_t ms);
void sys_write(Context* context, const char* addr, size_t size); void sys_write(Context* context, const char* addr, size_t size);
void sys_paint(Context* context, uint64_t x, uint64_t y, uint64_t w, uint64_t h, uint64_t col);
void sys_rand(Context* context);

View File

@ -9,20 +9,36 @@ global _userspace
_userspace: _userspace:
mov rdi, 4000 ; 4000 ms / 4 seconds mov rdi, 4000 ; 4000 ms / 4 seconds
mov rax, 2 ; sys_sleep mov rax, 2 ; sys_sleep
int 42h ; syscall int 42h
mov rcx, 10 cmp rax, 0
.loop: jne .fail ; syscall failed
dec rcx
mov rdi, .message mov rdi, .message
mov rsi, 22 mov rsi, 33
mov rax, 3 ; sys_write mov rax, 3 ; sys_write
int 42h ; syscall int 42h
mov rdi, 200 ; 200 ms / 0.2 seconds cmp rax, 33
mov rax, 2 jne .fail ; syscall did not write enough bytes
int 42h ; syscall .draw:
cmp rcx, 0 mov rax, 5 ; sys_rand
jne .loop int 42h
mov rax, 0 mov r9, rax ; color
int 42h ; Exit current task mov rdi, 20 ; x
mov rsi, 20 ; y
mov r10, 40 ; width
mov r8, 30 ; height
mov rax, 4 ; sys_paint
int 42h
cmp rax, 0
jne .fail
mov rdi, 100
mov rax, 2 ; sys_sleep
int 42h
cmp rax, 0
jne .fail
jmp .draw
.fail:
mov rax, 0 ; sys_exit
int 42h
jmp $ ; sys_exit failed, nothing more we can do
.message: .message:
db "hello from userspace!", 0xA, 0 db "userspace preparing for painting", 0xA

View File

@ -19,6 +19,8 @@ void Syscall::entry(Context* context)
case SYS_write: // sys_write case SYS_write: // sys_write
sys_write(context, (const char*)context->rdi, context->rsi); sys_write(context, (const char*)context->rdi, context->rsi);
break; break;
case SYS_paint: sys_paint(context, context->rdi, context->rsi, context->r10, context->r8, context->r9); break;
case SYS_rand: sys_rand(context); break;
default: context->rax = -1; break; default: context->rax = -1; break;
} }
} }

27
kernel/src/sys/gfx.cpp Normal file
View File

@ -0,0 +1,27 @@
#include "bootboot.h"
#include "interrupts/Context.h"
#include "render/Framebuffer.h"
#include <stdint.h>
extern BOOTBOOT bootboot;
void sys_paint(Context* context, uint64_t x, uint64_t y, uint64_t w, uint64_t h, uint64_t c)
{
if ((x + w) > bootboot.fb_width)
{
context->rax = -1;
return;
}
if ((y + h) > bootboot.fb_height)
{
context->rax = -1;
return;
}
uint32_t color = (uint32_t)c;
uint32_t* colptr = &color;
framebuffer0.paint_rect(x, y, w, h, *(Color*)colptr);
context->rax = 0;
}

7
kernel/src/sys/rand.cpp Normal file
View File

@ -0,0 +1,7 @@
#include "interrupts/Context.h"
#include "rand/Mersenne.h"
void sys_rand(Context* context)
{
context->rax = Mersenne::get();
}