Skip to content

Commit

Permalink
dev: Add devenum and devmsg syscalls
Browse files Browse the repository at this point in the history
  • Loading branch information
Qwinci committed Aug 26, 2023
1 parent 6fd20a7 commit 1cb1cea
Show file tree
Hide file tree
Showing 16 changed files with 232 additions and 148 deletions.
18 changes: 18 additions & 0 deletions include/crescent/dev.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef CRESCENT_DEV_H
#define CRESCENT_DEV_H

typedef enum {
DEVICE_TYPE_SOUND,
DEVICE_TYPE_FB,
DEVICE_TYPE_STORAGE,
DEVICE_TYPE_MAX
} DeviceType;

#define DEVMSG_INFO 0

#define DEVMSG_SND_MAP 1

#define DEVMSG_FB_INFO 1
#define DEVMSG_FB_MAP 2

#endif
6 changes: 2 additions & 4 deletions include/crescent/fb.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ typedef enum {
SYS_FB_FORMAT_BGRA32
} SysFramebufferFormat;

typedef struct SysFramebuffer {
void* base;
typedef struct SysFramebufferInfo {
size_t width;
size_t height;
size_t pitch;
size_t bpp;
SysFramebufferFormat fmt;
bool primary;
} SysFramebuffer;
} SysFramebufferInfo;

#endif
3 changes: 2 additions & 1 deletion include/crescent/sys.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ typedef enum {
#define SYS_MMAP 10
#define SYS_MUNMAP 11
#define SYS_CLOSE 12
#define SYS_ENUMERATE_FRAMEBUFFERS 13
#define SYS_DEVMSG 13
#define SYS_DEVENUM 14

#define ERR_INVALID_ARG (-1)
#define ERR_NO_PERMISSIONS (-2)
Expand Down
48 changes: 29 additions & 19 deletions src/arch/x86/con.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,28 @@
#include "arch/mod.h"
#include "mem/allocator.h"
#include "assert.h"
#include "string.h"

static volatile struct limine_framebuffer_request FB_REQUEST = {
.id = LIMINE_FRAMEBUFFER_REQUEST
};

static SysFramebuffer x86_boot_fb;
static FbDev x86_boot_fb;

void x86_init_con() {
if (FB_REQUEST.response && FB_REQUEST.response->framebuffer_count) {
struct limine_framebuffer* limine_fb = FB_REQUEST.response->framebuffers[0];
memcpy(x86_boot_fb.generic.name, "fb#0", sizeof("fb#0"));
x86_boot_fb.base = limine_fb->address;
x86_boot_fb.width = limine_fb->width;
x86_boot_fb.height = limine_fb->height;
x86_boot_fb.pitch = limine_fb->pitch;
x86_boot_fb.bpp = limine_fb->bpp;
x86_boot_fb.fmt = SYS_FB_FORMAT_BGRA32;
x86_boot_fb.primary = true;
primary_fb = &x86_boot_fb;
x86_boot_fb.info.width = limine_fb->width;
x86_boot_fb.info.height = limine_fb->height;
x86_boot_fb.info.pitch = limine_fb->pitch;
x86_boot_fb.info.bpp = limine_fb->bpp;
x86_boot_fb.info.fmt = SYS_FB_FORMAT_BGRA32;

Module font_module = arch_get_module("Tamsyn8x16r.psf");
if (font_module.base) {
fbcon_init(primary_fb, font_module.base);
fbcon_init(&x86_boot_fb, font_module.base);
}
}
}
Expand All @@ -34,18 +34,28 @@ void x86_init_fbs() {
if (FB_REQUEST.response) {
for (usize i = 0; i < FB_REQUEST.response->framebuffer_count; ++i) {
struct limine_framebuffer* limine_fb = FB_REQUEST.response->framebuffers[i];
LinkedSysFramebuffer* fb = kmalloc(sizeof(LinkedSysFramebuffer));
FbDev* fb = kcalloc(sizeof(FbDev));
assert(fb);
fb->next = NULL;

fb->fb.base = limine_fb->address;
fb->fb.width = limine_fb->width;
fb->fb.height = limine_fb->height;
fb->fb.pitch = limine_fb->pitch;
fb->fb.bpp = limine_fb->bpp;
fb->fb.fmt = SYS_FB_FORMAT_BGRA32;
fb->fb.primary = i == 0;
sys_fb_add(fb);
memcpy(fb->generic.name, "fb#", 3);
// todo maybe snprintf
usize num = i;
char buf[21];
buf[20] = 0;
char* ptr = buf + 20;
do {
*--ptr = (char) ('0' + num % 10);
num /= 10;
} while (num);
memcpy(fb->generic.name + 3, ptr, buf + 21 - ptr);

fb->base = limine_fb->address;
fb->info.width = limine_fb->width;
fb->info.height = limine_fb->height;
fb->info.pitch = limine_fb->pitch;
fb->info.bpp = limine_fb->bpp;
fb->info.fmt = SYS_FB_FORMAT_BGRA32;
dev_add(&fb->generic, DEVICE_TYPE_FB);
}
}
}
54 changes: 11 additions & 43 deletions src/dev/fb.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,64 +3,32 @@
#include "sched/mutex.h"
#include "string.h"

void fb_set_pixel(SysFramebuffer* self, usize x, usize y, u32 color) {
void fb_set_pixel(FbDev* self, usize x, usize y, u32 color) {
assert(self);
assert(self->base);
assert(x < self->width);
assert(y < self->height);
if (self->fmt == SYS_FB_FORMAT_BGRA32) {
*offset(self->base, u32*, y * self->pitch + x * (self->bpp / 8)) = color;
assert(x < self->info.width);
assert(y < self->info.height);
if (self->info.fmt == SYS_FB_FORMAT_BGRA32) {
*offset(self->base, u32*, y * self->info.pitch + x * (self->info.bpp / 8)) = color;
}
else {
return;
}
}

u32 fb_get_pixel(SysFramebuffer* self, usize x, usize y) {
if (self->fmt == SYS_FB_FORMAT_BGRA32) {
return *offset(self->base, u32*, y * self->pitch + x * (self->bpp / 8));
u32 fb_get_pixel(FbDev* self, usize x, usize y) {
if (self->info.fmt == SYS_FB_FORMAT_BGRA32) {
return *offset(self->base, u32*, y * self->info.pitch + x * (self->info.bpp / 8));
}
else {
return 0;
}
}

void fb_clear(SysFramebuffer* self, u32 color) {
for (usize y = 0; y < self->height; ++y) {
for (usize x = 0; x < self->width; ++x) {
void fb_clear(FbDev* self, u32 color) {
for (usize y = 0; y < self->info.height; ++y) {
for (usize x = 0; x < self->info.width; ++x) {
fb_set_pixel(self, x, y, color);
}
}
}

static Mutex SYS_FB_LOCK = {};
static LinkedSysFramebuffer* SYS_FBS = NULL;
static usize SYS_FB_COUNT = 0;

void sys_fb_add(LinkedSysFramebuffer* fb) {
mutex_lock(&SYS_FB_LOCK);
fb->next = SYS_FBS;
SYS_FBS = fb;
SYS_FB_COUNT += 1;
mutex_unlock(&SYS_FB_LOCK);
}

void sys_fb_get(SysFramebuffer* res, usize* count) {
mutex_lock(&SYS_FB_LOCK);

if (count) {
*count = SYS_FB_COUNT;
}
if (res) {
usize i = 0;
for (LinkedSysFramebuffer* fb = SYS_FBS; fb; fb = fb->next) {
memcpy(res + i, &fb->fb, sizeof(SysFramebuffer));
i += 1;
}
}

mutex_unlock(&SYS_FB_LOCK);
}

SysFramebuffer* primary_fb = NULL;

21 changes: 9 additions & 12 deletions src/dev/fb.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
#pragma once
#include "types.h"
#include "crescent/fb.h"
#include "sys/dev.h"

void fb_set_pixel(SysFramebuffer* self, usize x, usize y, u32 color);
u32 fb_get_pixel(SysFramebuffer* self, usize x, usize y);
void fb_clear(SysFramebuffer* self, u32 color);
typedef struct FbDev {
GenericDevice generic;
void* base;
SysFramebufferInfo info;
} FbDev;

typedef struct LinkedSysFramebuffer {
struct LinkedSysFramebuffer* next;
SysFramebuffer fb;
} LinkedSysFramebuffer;

void sys_fb_add(LinkedSysFramebuffer* fb);
void sys_fb_get(SysFramebuffer* res, usize* count);

extern SysFramebuffer* primary_fb;
void fb_set_pixel(FbDev* self, usize x, usize y, u32 color);
u32 fb_get_pixel(FbDev* self, usize x, usize y);
void fb_clear(FbDev* self, u32 color);
34 changes: 17 additions & 17 deletions src/dev/fbcon.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ typedef struct {

typedef struct {
Con common;
SysFramebuffer* fb;
FbDev* fb;
const PsfFont* font;
} FbCon;

Expand All @@ -35,31 +35,31 @@ static FbCon fbcon = {
};

static int fbcon_write(Con* self, char c) {
FbCon* fb = container_of(self, FbCon, common);
if ((self->column + 1) * fb->font->width >= fb->fb->width) {
FbCon* con = container_of(self, FbCon, common);
if ((self->column + 1) * con->font->width >= con->fb->info.width) {
self->column = 0;
self->line += 1;
}
if (self->line * fb->font->height >= fb->fb->height) {
if (self->line * con->font->height >= con->fb->info.height) {
/*
fb_clear(fb->fb, 0);
fb_clear(con->con, 0);
self->line = 0;
self->column = 0;*/
return 0;
// todo
}

u32 bytes_per_line = (fb->font->width + 7) / 8;
u32 bytes_per_line = (con->font->width + 7) / 8;

usize offset = sizeof(PsfFont) + (usize) c * fb->font->bytes_per_glyph;
const u8* font_c = offset(fb->font, const u8*, offset);
usize init_x = self->column * fb->font->width;
usize init_y = self->line * fb->font->height;
for (usize y = 0; y < fb->font->height; ++y) {
for (usize x = 0; x < fb->font->width; ++x) {
u32 shift = fb->font->width - 1 - x;
usize offset = sizeof(PsfFont) + (usize) c * con->font->bytes_per_glyph;
const u8* font_c = offset(con->font, const u8*, offset);
usize init_x = self->column * con->font->width;
usize init_y = self->line * con->font->height;
for (usize y = 0; y < con->font->height; ++y) {
for (usize x = 0; x < con->font->width; ++x) {
u32 shift = con->font->width - 1 - x;
u32 color = font_c[shift / 8] & (1 << (shift % 8)) ? self->fg : self->bg;
fb_set_pixel(fb->fb, init_x + x, init_y + y, color);
fb_set_pixel(con->fb, init_x + x, init_y + y, color);
}
font_c += bytes_per_line;
}
Expand All @@ -70,11 +70,11 @@ static int fbcon_write(Con* self, char c) {

static int fbcon_write_at(Con* self, usize x, usize y, char c) {
FbCon* fb = container_of(self, FbCon, common);
if ((x + 1) * fb->font->width >= fb->fb->width) {
if ((x + 1) * fb->font->width >= fb->fb->info.width) {
x = 0;
y += 1;
}
if (y * fb->font->height >= fb->fb->height) {
if (y * fb->font->height >= fb->fb->info.height) {
return 0;
}

Expand All @@ -96,7 +96,7 @@ static int fbcon_write_at(Con* self, usize x, usize y, char c) {
return 1;
}

void fbcon_init(SysFramebuffer* fb, const void* font) {
void fbcon_init(FbDev* fb, const void* font) {
fbcon.fb = fb;
fbcon.font = (const PsfFont*) font;
kernel_con = &fbcon.common;
Expand Down
4 changes: 2 additions & 2 deletions src/dev/fbcon.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#pragma once

typedef struct SysFramebuffer SysFramebuffer;
typedef struct FbDev FbDev;

void fbcon_init(SysFramebuffer* fb, const void* font);
void fbcon_init(FbDev* fb, const void* font);
11 changes: 11 additions & 0 deletions src/mem/allocator.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "string.h"
#include "utils.h"
#include "vm.h"
#include "utils/math.h"

typedef struct Node {
struct Node* prev;
Expand Down Expand Up @@ -166,3 +167,13 @@ void* kcalloc(usize size) {
memset(mem, 0, size);
return mem;
}

void* krealloc(void* ptr, usize old_size, usize new_size) {
void* new_ptr = kmalloc(new_size);
if (!new_ptr) {
return NULL;
}
memcpy(new_ptr, ptr, MIN(old_size, new_size));
kfree(ptr, old_size);
return new_ptr;
}
3 changes: 2 additions & 1 deletion src/mem/allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ typedef struct Task Task;

void* kmalloc(usize size);
void* kcalloc(usize size);
void kfree(void* ptr, usize size);
void kfree(void* ptr, usize size);
void* krealloc(void* ptr, usize old_size, usize new_size);
3 changes: 2 additions & 1 deletion src/sys/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
target_sources(crescent PRIVATE
syscalls.c)
syscalls.c
dev.c)
Loading

0 comments on commit 1cb1cea

Please sign in to comment.