From e7d88df7a5a7e11677b68303a0d05455bf9a60d6 Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Wed, 11 Dec 2019 21:22:15 +0100 Subject: Some user-side graphics --- src/kernel/graphics/font.c | 3 ++- src/kernel/graphics/vesa.c | 6 ++--- src/kernel/syscall/actions/sys_get_pointers.c | 18 ++++++++++++++ src/kernel/syscall/syscall.c | 5 ++-- src/kernel/syscall/syscall.h | 2 ++ src/userspace/graphics/framebuffer.c | 34 +++++++++++++++++++++++++++ src/userspace/graphics/graphics.h | 18 ++++++++++++++ src/userspace/main.c | 29 ++--------------------- src/userspace/syscall.c | 6 +++-- src/userspace/syscall.h | 2 ++ 10 files changed, 88 insertions(+), 35 deletions(-) create mode 100644 src/kernel/syscall/actions/sys_get_pointers.c create mode 100644 src/userspace/graphics/framebuffer.c create mode 100644 src/userspace/graphics/graphics.h (limited to 'src') diff --git a/src/kernel/graphics/font.c b/src/kernel/graphics/font.c index 147eaff..3f55d01 100644 --- a/src/kernel/graphics/font.c +++ b/src/kernel/graphics/font.c @@ -10,7 +10,8 @@ void font_install() { - font = (struct font *) kmalloc(100000);; // High quality shit + font = (struct font *) paging_alloc_pages(25);; // High quality shit + paging_set_user((uint32_t) font, 25); uint8_t boot_drive_id = (uint8_t) (*((uint8_t *) 0x9000)); if (boot_drive_id != 0xE0) { diff --git a/src/kernel/graphics/vesa.c b/src/kernel/graphics/vesa.c index d673bde..c74b34b 100644 --- a/src/kernel/graphics/vesa.c +++ b/src/kernel/graphics/vesa.c @@ -190,11 +190,11 @@ void set_optimal_resolution() uint32_t fb_size = vbe_width * vbe_height * vbe_bpl; cursor_buffer = kmalloc(fb_size); + paging_set_user((uint32_t) fb, fb_size / 4096); for (uint32_t z = 0; z < fb_size; z += 4096) { - paging_map((uint32_t) fb + z, (uint32_t) fb + z, PT_PRESENT | PT_RW | PT_USED); + paging_map((uint32_t) fb + z, (uint32_t) fb + z, PT_PRESENT | PT_RW | PT_USED | PT_ALL_PRIV); paging_map((uint32_t) cursor_buffer + z, (uint32_t) cursor_buffer + z, PT_PRESENT | PT_RW | PT_USED); } - paging_set_user((uint32_t) fb, fb_size); serial_write_hex((int) &fb); serial_write("\n"); @@ -399,4 +399,4 @@ void vesa_set_color(uint32_t color) { vesa_convert_color(terminal_color, color); vesa_convert_color(terminal_background, default_background_color); -} +} \ No newline at end of file diff --git a/src/kernel/syscall/actions/sys_get_pointers.c b/src/kernel/syscall/actions/sys_get_pointers.c new file mode 100644 index 0000000..adbdd36 --- /dev/null +++ b/src/kernel/syscall/actions/sys_get_pointers.c @@ -0,0 +1,18 @@ +#include +#include +#include +#include + +struct userspace_pointers { + unsigned char *fb; + struct font *font; +}; + +uint32_t sys_get_pointers() +{ + struct userspace_pointers *pointers = (struct userspace_pointers *) paging_alloc_pages(1); + pointers->fb = fb; + pointers->font = font; + paging_set_user((uint32_t) pointers, 1); + return (uint32_t) pointers; +} \ No newline at end of file diff --git a/src/kernel/syscall/syscall.c b/src/kernel/syscall/syscall.c index 3a1b011..24f2445 100644 --- a/src/kernel/syscall/syscall.c +++ b/src/kernel/syscall/syscall.c @@ -13,8 +13,9 @@ uint32_t (*syscalls[])() = { [2] = sys_read, [3] = sys_writec, [4] = sys_readc, - [5] = sys_paging_alloc, - [6] = sys_paging_free + [5] = sys_get_pointers, + [6] = sys_paging_alloc, + [7] = sys_paging_free }; void syscall_handler(struct regs *r) diff --git a/src/kernel/syscall/syscall.h b/src/kernel/syscall/syscall.h index a037d7f..378b5e3 100644 --- a/src/kernel/syscall/syscall.h +++ b/src/kernel/syscall/syscall.h @@ -13,6 +13,8 @@ uint32_t sys_read(char *buf); uint32_t sys_readc(char *ch); +uint32_t sys_get_pointers(); + uint32_t sys_paging_alloc(uint32_t count); uint32_t sys_paging_free(uint32_t virt, uint32_t count); diff --git a/src/userspace/graphics/framebuffer.c b/src/userspace/graphics/framebuffer.c new file mode 100644 index 0000000..f7b793b --- /dev/null +++ b/src/userspace/graphics/framebuffer.c @@ -0,0 +1,34 @@ +#include +#include + +unsigned char *fb; +int vbe_bpl = 3; +int vbe_pitch = 3000; +int vbe_height = 1080; +int vbe_width = 2560; + +void vesa_draw_rectangle(int x1, int y1, int x2, int y2, const uint32_t color[3]) +{ + int pos1 = x1 * vbe_bpl + y1 * vbe_pitch; + char *draw = (char *) &fb[pos1]; + for (int i = 0; i <= y2 - y1; i++) { + for (int j = 0; j <= x2 - x1; j++) { + draw[vbe_bpl * j] = color[2]; + draw[vbe_bpl * j + 1] = color[1]; + draw[vbe_bpl * j + 2] = color[0]; + } + draw += vbe_pitch; + } +} + +void vesa_clear() +{ + vesa_draw_rectangle(0, 0, vbe_width - 1, vbe_height - 1, 0); +} + +void init_framebuffer() +{ + struct userspace_pointers *pointers = (struct userspace_pointers *) syscall_get_pointers(); + fb = pointers->fb; + vesa_clear(); +} \ No newline at end of file diff --git a/src/userspace/graphics/graphics.h b/src/userspace/graphics/graphics.h new file mode 100644 index 0000000..6197020 --- /dev/null +++ b/src/userspace/graphics/graphics.h @@ -0,0 +1,18 @@ +#ifndef MELVIX_GRAPHICS_H +#define MELVIX_GRAPHICS_H + +struct font { + uint16_t font_32[758][32]; + uint16_t font_24[758][24]; + uint8_t font_16[758][16]; + uint16_t cursor[19]; +}; + +struct userspace_pointers { + unsigned char *fb; + struct font *font; +}; + +void init_framebuffer(); + +#endif \ No newline at end of file diff --git a/src/userspace/main.c b/src/userspace/main.c index 4e036c5..9341bd0 100644 --- a/src/userspace/main.c +++ b/src/userspace/main.c @@ -1,37 +1,12 @@ #include - -char *fb; -int vbe_bpl = 3; -int vbe_pitch = 3000; -int vbe_height = 1080; -int vbe_width = 2560; - -void vesa_draw_rectangle(int x1, int y1, int x2, int y2, const uint32_t color[3]) -{ - int pos1 = x1 * vbe_bpl + y1 * vbe_pitch; - char *draw = (char *) &fb[pos1]; - for (int i = 0; i <= y2 - y1; i++) { - for (int j = 0; j <= x2 - x1; j++) { - draw[vbe_bpl * j] = color[2]; - draw[vbe_bpl * j + 1] = color[1]; - draw[vbe_bpl * j + 2] = color[0]; - } - draw += vbe_pitch; - } -} - -void vesa_clear() -{ - vesa_draw_rectangle(0, 0, vbe_width - 1, vbe_height - 1, 0); -} +#include void user_main() { char hello[] = "> Successfully switched to usermode!\n"; syscall_write(hello); - // fb = (char *) 0x110048; - // vesa_clear(); + init_framebuffer(); while (1) {}; diff --git a/src/userspace/syscall.c b/src/userspace/syscall.c index a6bdc80..b398728 100644 --- a/src/userspace/syscall.c +++ b/src/userspace/syscall.c @@ -13,6 +13,8 @@ DEFN_SYSCALL1(writec, 3, char *); DEFN_SYSCALL1(readc, 4, char *); -DEFN_SYSCALL1(paging_alloc, 5, uint32_t); +DEFN_SYSCALL0(get_pointers, 5); -DEFN_SYSCALL2(paging_free, 6, uint32_t, uint32_t); \ No newline at end of file +DEFN_SYSCALL1(paging_alloc, 6, uint32_t); + +DEFN_SYSCALL2(paging_free, 7, uint32_t, uint32_t); \ No newline at end of file diff --git a/src/userspace/syscall.h b/src/userspace/syscall.h index 5de717e..c46bb54 100644 --- a/src/userspace/syscall.h +++ b/src/userspace/syscall.h @@ -74,6 +74,8 @@ DECL_SYSCALL1(writec, char *); DECL_SYSCALL1(readc, char *); +DECL_SYSCALL0(get_pointers); + DECL_SYSCALL1(paging_alloc, uint32_t); DECL_SYSCALL2(paging_free, uint32_t, uint32_t); -- cgit v1.2.3