aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--apps/init.c6
-rw-r--r--apps/window.c4
-rw-r--r--apps/wm.c71
-rw-r--r--kernel/drivers/interrupts.c4
-rw-r--r--kernel/drivers/keyboard.c6
-rw-r--r--kernel/drivers/mouse.c6
-rw-r--r--kernel/features/fs.c4
-rw-r--r--kernel/features/mm.c4
-rw-r--r--kernel/features/proc.c20
-rw-r--r--kernel/inc/mm.h3
-rw-r--r--libc/alloc.c2
-rw-r--r--libc/inc/def.h3
-rw-r--r--libc/inc/sys.h4
-rw-r--r--libgui/gfx.c9
-rw-r--r--libgui/gui.c18
-rw-r--r--libgui/inc/gfx.h7
-rw-r--r--libgui/inc/gui.h3
-rw-r--r--libgui/inc/msg.h24
-rw-r--r--libgui/msg.c22
-rwxr-xr-xrun9
20 files changed, 135 insertions, 94 deletions
diff --git a/apps/init.c b/apps/init.c
index f854a81..c59e3bf 100644
--- a/apps/init.c
+++ b/apps/init.c
@@ -6,13 +6,15 @@
#include <str.h>
#include <sys.h>
+#include <cpu.h>
+
int main(int argc, char **argv)
{
UNUSED(argc);
UNUSED(argv);
int wm = exec("/bin/wm", "wm", NULL);
- /* int test = exec("/bin/window", "test", NULL); */
+ int test = exec("/bin/window", "test", NULL);
- return wm; //+ test;
+ return wm + test;
}
diff --git a/apps/window.c b/apps/window.c
index ac29ba1..90a414c 100644
--- a/apps/window.c
+++ b/apps/window.c
@@ -8,6 +8,9 @@ int main(void)
{
struct gui_window win = { 0 };
assert(gui_new_window(&win) > 0);
+ while (1)
+ ;
+#if 0
gfx_fill(win.ctx, COLOR_GREEN);
// Professional testing
for (int i = 0; i < 12; i++) {
@@ -16,5 +19,6 @@ int main(void)
}
assert(gui_redraw_window(win.id) > 0);
log("%d\n", win.ctx->size.x);
+#endif
return 0;
}
diff --git a/apps/wm.c b/apps/wm.c
index 8eb6f70..ecc7cb2 100644
--- a/apps/wm.c
+++ b/apps/wm.c
@@ -8,6 +8,7 @@
#include <ioctl.h>
#include <keymap.h>
#include <list.h>
+#include <msg.h>
#include <random.h>
#include <vesa.h>
@@ -308,46 +309,34 @@ static void handle_event_mouse(struct event_mouse *event)
window_redraw(cursor);
}
-static void handle_message_new_window(struct message *msg)
+static void handle_message_new_window(struct message_new_window *msg)
{
- if (!msg->data) {
- msg_send(msg->src, GUI_NEW_WINDOW | MSG_FAILURE, NULL);
- return;
- }
- struct gui_window *buf = msg->data;
- struct window *win = window_new((struct client){ .pid = msg->src }, "idk", vec2(500, 600),
- vec2(600, 400), 0);
- buf->id = win->id;
- buf->ctx = &win->ctx;
- buf->pos = &win->pos;
- msg_send(msg->src, GUI_NEW_WINDOW | MSG_SUCCESS, NULL);
+ struct window *win = window_new((struct client){ .pid = msg->header.src }, "idk",
+ vec2(500, 600), vec2(600, 400), 0);
+ msg->ctx = win->ctx;
+ msg->id = win->id;
+ msg_send(msg->header.src, GUI_NEW_WINDOW | MSG_SUCCESS, msg, sizeof(*msg));
/* window_redraw(win); */
}
-static void handle_message_redraw_window(struct message *msg)
+static void handle_message_redraw_window(struct message_redraw_window *msg)
{
- if (!msg->data) {
- msg_send(msg->src, GUI_REDRAW_WINDOW | MSG_FAILURE, NULL);
- return;
- }
- u32 id = *(u32 *)msg->data;
+ u32 id = msg->id;
struct window *win = window_find(id);
if (!win) {
- msg_send(msg->src, GUI_REDRAW_WINDOW | MSG_FAILURE, NULL);
+ msg_send(msg->header.src, GUI_REDRAW_WINDOW | MSG_FAILURE, NULL,
+ sizeof(msg->header));
return;
}
- msg_send(msg->src, GUI_REDRAW_WINDOW | MSG_SUCCESS, NULL);
+ msg_send(msg->header.src, GUI_REDRAW_WINDOW | MSG_SUCCESS, NULL, sizeof(msg->header));
window_redraw(win);
}
-static void handle_message(struct message *msg)
+static void handle_message(void *msg)
{
- if (msg->magic != MSG_MAGIC) {
- log("Message magic doesn't match!\n");
- return;
- }
+ struct message_header *header = msg;
- switch (msg->type) {
+ switch (header->type) {
case GUI_NEW_WINDOW:
handle_message_new_window(msg);
break;
@@ -355,8 +344,8 @@ static void handle_message(struct message *msg)
handle_message_redraw_window(msg);
break;
default:
- log("Message type %d not implemented!\n", msg->type);
- msg_send(msg->src, MSG_FAILURE, NULL);
+ log("Message type %d not implemented!\n", header->type);
+ msg_send(header->src, MSG_FAILURE, NULL, sizeof(*header));
}
}
@@ -389,32 +378,34 @@ int main(int argc, char **argv)
gfx_load_wallpaper(&cursor->ctx, "/res/cursor.png");
window_redraw(wallpaper);
- struct message msg = { 0 };
+ u8 msg[1024] = { 0 };
struct event_keyboard event_keyboard = { 0 };
struct event_mouse event_mouse = { 0 };
- const char *listeners[] = { "/dev/kbd", "/dev/mouse", "/proc/self/msg" };
+ const char *listeners[] = { "/dev/kbd", "/dev/mouse", "/proc/self/msg", NULL };
while (1) {
int poll_ret = 0;
if ((poll_ret = poll(listeners)) >= 0) {
if (poll_ret == 0) {
if (read(listeners[poll_ret], &event_keyboard, 0,
- sizeof(event_keyboard)) > 0)
+ sizeof(event_keyboard)) > 0) {
handle_event_keyboard(&event_keyboard);
- continue;
+ continue;
+ }
} else if (poll_ret == 1) {
if (read(listeners[poll_ret], &event_mouse, 0,
- sizeof(event_mouse)) > 0)
+ sizeof(event_mouse)) > 0) {
handle_event_mouse(&event_mouse);
- continue;
+ continue;
+ }
} else if (poll_ret == 2) {
- if (read(listeners[poll_ret], &msg, 0, sizeof(msg)) > 0)
- handle_message(&msg);
- continue;
+ if (msg_receive(msg, 1024) > 0) {
+ handle_message(msg);
+ continue;
+ }
}
- } else {
- err(1, "POLL ERROR!\n");
}
- };
+ panic("Poll/read error!\n");
+ }
// TODO: Execute?
free(keymap);
diff --git a/kernel/drivers/interrupts.c b/kernel/drivers/interrupts.c
index fe2321a..2e1444f 100644
--- a/kernel/drivers/interrupts.c
+++ b/kernel/drivers/interrupts.c
@@ -5,6 +5,7 @@
#include <def.h>
#include <interrupts.h>
#include <mem.h>
+#include <mm.h>
#include <print.h>
#include <proc.h>
#include <serial.h>
@@ -236,6 +237,9 @@ static void isr_install(void)
// Set default routines
for (u32 i = 0; i < 256; i++)
isr_routines[i] = isr_panic;
+
+ // Set page fault handler
+ isr_install_handler(14, page_fault_handler);
}
/**
diff --git a/kernel/drivers/keyboard.c b/kernel/drivers/keyboard.c
index 3ae3c0e..dbee8e1 100644
--- a/kernel/drivers/keyboard.c
+++ b/kernel/drivers/keyboard.c
@@ -66,10 +66,10 @@ static s32 keyboard_read(void *buf, u32 offset, u32 count, struct device *dev)
if (stack_empty(queue))
return -1;
- struct event *e = stack_pop(queue);
- memcpy(buf, (u8 *)e + offset, count);
+ struct event_keyboard *e = stack_pop(queue);
+ memcpy(buf, (u8 *)e + offset, MIN(count, sizeof(*e)));
free(e);
- return count;
+ return MIN(count, sizeof(*e));
}
static u8 keyboard_ready(void)
diff --git a/kernel/drivers/mouse.c b/kernel/drivers/mouse.c
index 40094d1..5c481da 100644
--- a/kernel/drivers/mouse.c
+++ b/kernel/drivers/mouse.c
@@ -94,10 +94,10 @@ static s32 mouse_read(void *buf, u32 offset, u32 count, struct device *dev)
if (stack_empty(queue))
return -1;
- struct event *e = stack_pop(queue);
- memcpy(buf, (u8 *)e + offset, count);
+ struct event_mouse *e = stack_pop(queue);
+ memcpy(buf, (u8 *)e + offset, MIN(count, sizeof(*e)));
free(e);
- return count;
+ return MIN(count, sizeof(*e));
}
void mouse_install(void)
diff --git a/kernel/features/fs.c b/kernel/features/fs.c
index 20d00e5..c8ad317 100644
--- a/kernel/features/fs.c
+++ b/kernel/features/fs.c
@@ -122,7 +122,7 @@ s32 vfs_mount(struct device *dev, const char *path)
s32 vfs_read(const char *path, void *buf, u32 offset, u32 count)
{
- /* printf("%s READ: %s\n", proc_current()->name, path); */
+ /* printf("%s READ: %s\n", proc_current() ? proc_current()->name : "Unknown", path); */
if (!count)
return 0;
@@ -148,7 +148,7 @@ s32 vfs_read(const char *path, void *buf, u32 offset, u32 count)
s32 vfs_write(const char *path, void *buf, u32 offset, u32 count)
{
- /* printf("%s WRITE: %s\n", proc_current()->name, path); */
+ /* printf("%s WRITE: %s\n", proc_current() ? proc_current()->name : "Unknown", path); */
if (!count)
return 0;
diff --git a/kernel/features/mm.c b/kernel/features/mm.c
index bd32683..9eca438 100644
--- a/kernel/features/mm.c
+++ b/kernel/features/mm.c
@@ -32,7 +32,7 @@ static void paging_switch_dir(u32 dir)
extern void paging_invalidate_tlb(void);
-static void page_fault(struct regs *r)
+void page_fault_handler(struct regs *r)
{
// Check error code
const char *type = (r->err_code & 1) ? "present" : "non-present";
@@ -495,6 +495,4 @@ void memory_install(struct mem_info *mem_info, struct vid_info *vid_info)
memory_switch_dir(&kernel_dir);
paging_enable();
-
- isr_install_handler(14, page_fault);
}
diff --git a/kernel/features/proc.c b/kernel/features/proc.c
index 6bbe894..b93f7c8 100644
--- a/kernel/features/proc.c
+++ b/kernel/features/proc.c
@@ -305,6 +305,11 @@ static enum stream_defaults procfs_stream(const char *path)
}
}
+struct procfs_message {
+ u8 *data;
+ u32 size;
+};
+
static s32 procfs_write(const char *path, void *buf, u32 offset, u32 count, struct device *dev)
{
u32 pid = 0;
@@ -318,7 +323,10 @@ static s32 procfs_write(const char *path, void *buf, u32 offset, u32 count, stru
if (!memcmp(path, "msg", 4)) {
void *msg_data = malloc(count);
memcpy(msg_data, buf, count);
- stack_push_bot(p->messages, msg_data); // TODO: Use offset
+ struct procfs_message *msg = malloc(sizeof(*msg));
+ msg->data = msg_data;
+ msg->size = count;
+ stack_push_bot(p->messages, msg); // TODO: Use offset
proc_enable_waiting(pid, PROC_WAIT_MSG);
return count;
} else if (!memcmp(path, "io/", 3)) {
@@ -371,12 +379,13 @@ static s32 procfs_read(const char *path, void *buf, u32 offset, u32 count, struc
if (stack_empty(p->messages)) {
return -1; // This shouldn't happen
} else {
- u8 *msg = stack_pop(p->messages);
+ struct procfs_message *msg = stack_pop(p->messages);
if (!msg)
return -1;
- memcpy(buf, msg + offset, count);
+ memcpy(buf, msg->data + offset, MIN(count, msg->size));
+ free(msg->data);
free(msg);
- return count;
+ return MIN(count, msg->size);
}
} else if (!memcmp(path, "io/", 3)) {
path += 3;
@@ -500,7 +509,10 @@ void proc_init(void)
printf("Jumping to userspace!\n");
memory_switch_dir(((struct proc *)new->data)->page_dir);
+
+ // You're waiting for a train. A train that will take you far away...
proc_jump_userspace();
+
while (1) {
};
}
diff --git a/kernel/inc/mm.h b/kernel/inc/mm.h
index 590875c..d3e37f6 100644
--- a/kernel/inc/mm.h
+++ b/kernel/inc/mm.h
@@ -5,6 +5,7 @@
#include <boot.h>
#include <def.h>
+#include <interrupts.h>
struct memory_range {
u32 base;
@@ -102,4 +103,6 @@ void memory_backup_dir(struct page_dir **backup);
void memory_install(struct mem_info *mem_info, struct vid_info *vid_info);
+void page_fault_handler(struct regs *r);
+
#endif
diff --git a/libc/alloc.c b/libc/alloc.c
index 4ccf35f..be986dc 100644
--- a/libc/alloc.c
+++ b/libc/alloc.c
@@ -86,8 +86,6 @@ struct liballoc_minor {
#define MAJOR_SIZE (ALIGN_UP(sizeof(struct liballoc_major), 16))
#define MINOR_SIZE (ALIGN_UP(sizeof(struct liballoc_minor), 16))
-#define MIN(__x, __y) ((__x) < (__y) ? (__x) : (__y))
-#define MAX(__x, __y) ((__x) > (__y) ? (__x) : (__y))
static struct liballoc_major *l_mem_root = NULL;
static struct liballoc_major *l_best_bet = NULL;
diff --git a/libc/inc/def.h b/libc/inc/def.h
index db1c95e..c334fcb 100644
--- a/libc/inc/def.h
+++ b/libc/inc/def.h
@@ -25,6 +25,9 @@ typedef unsigned long long u64;
#define UNUSED(a) ((void)(a))
+#define MIN(a, b) (((a) < (b)) ? (a) : (b))
+#define MAX(a, b) (((a) > (b)) ? (a) : (b))
+
#define NORETURN __attribute__((noreturn))
#define NO_SANITIZE __attribute__((no_sanitize("undefined")))
#define PACKED __attribute__((packed))
diff --git a/libc/inc/sys.h b/libc/inc/sys.h
index 3125cb0..8add0de 100644
--- a/libc/inc/sys.h
+++ b/libc/inc/sys.h
@@ -93,7 +93,9 @@ int sysv(enum sys num, ...);
static inline u32 getpid(void)
{
- u32 buf = 0;
+ static u32 buf = 0;
+ if (buf)
+ return buf;
read("/proc/self/pid", &buf, 0, sizeof(buf));
return buf;
}
diff --git a/libgui/gfx.c b/libgui/gfx.c
index d9457c7..dad8b88 100644
--- a/libgui/gfx.c
+++ b/libgui/gfx.c
@@ -98,10 +98,11 @@ static void draw_rectangle(struct context *ctx, vec2 pos1, vec2 pos2, u32 c)
struct context *gfx_new_ctx(struct context *ctx)
{
- struct message msg = { 0 };
- assert(msg_send(pidof(WM_PATH), GFX_NEW_CONTEXT, ctx) > 0);
- assert(msg_receive(&msg) > 0);
- memcpy(ctx, msg.data, sizeof(*ctx));
+ /* struct message msg = { 0 }; */
+ assert(0);
+ /* assert(msg_send(pidof(WM_PATH), GFX_NEW_CONTEXT, ctx) > 0); */
+ /* assert(msg_receive(&msg) > 0); */
+ /* memcpy(ctx, msg.data, sizeof(*ctx)); */
return ctx;
}
diff --git a/libgui/gui.c b/libgui/gui.c
index 2083f23..bc8adb1 100644
--- a/libgui/gui.c
+++ b/libgui/gui.c
@@ -2,24 +2,30 @@
#include <def.h>
#include <gui.h>
+#include <msg.h>
#include <print.h>
#define WM_PATH "/bin/wm"
s32 gui_new_window(struct gui_window *win)
{
- struct message msg = { 0 };
- if (msg_send(pidof(WM_PATH), GUI_NEW_WINDOW, win) > 0 && msg_receive(&msg) > 0 &&
- msg.type == (GUI_NEW_WINDOW | MSG_SUCCESS))
+ struct message_new_window msg = { 0 };
+ if (msg_send(pidof(WM_PATH), GUI_NEW_WINDOW, &msg, sizeof(msg)) > 0 &&
+ msg_receive(&msg, sizeof(msg)) > 0 &&
+ msg.header.type == (GUI_NEW_WINDOW | MSG_SUCCESS)) {
+ win->id = msg.id;
+ win->ctx = msg.ctx;
return win->id;
+ }
return -1;
}
s32 gui_redraw_window(u32 id)
{
- struct message msg = { 0 };
- if (msg_send(pidof(WM_PATH), GUI_REDRAW_WINDOW, &id) > 0 && msg_receive(&msg) > 0 &&
- msg.type == (GUI_REDRAW_WINDOW | MSG_SUCCESS))
+ struct message_redraw_window msg = { .id = id };
+ if (msg_send(pidof(WM_PATH), GUI_REDRAW_WINDOW, &msg, sizeof(msg)) > 0 &&
+ msg_receive(&msg, sizeof(msg)) > 0 &&
+ msg.header.type == (GUI_REDRAW_WINDOW | MSG_SUCCESS))
return id;
return -1;
}
diff --git a/libgui/inc/gfx.h b/libgui/inc/gfx.h
index 4a358ca..f3555a4 100644
--- a/libgui/inc/gfx.h
+++ b/libgui/inc/gfx.h
@@ -5,7 +5,6 @@
#define GFX_H
#include <def.h>
-#include <msg.h>
#include <sys.h>
#include <vec.h>
#include <vesa.h>
@@ -82,8 +81,8 @@ int gfx_font_width(enum font_type);
* Wrappers
*/
-#define gfx_redraw() \
- (msg_send(pidof(WM_PATH), GFX_REDRAW, NULL)) // TODO: Partial redraw (optimization)
-#define gfx_redraw_focused() (msg_send(pidof(WM_PATH), GFX_REDRAW_FOCUSED, NULL))
+/* #define gfx_redraw() \ */
+/* (msg_send(pidof(WM_PATH), GFX_REDRAW, NULL)) // TODO: Partial redraw (optimization) */
+/* #define gfx_redraw_focused() (msg_send(pidof(WM_PATH), GFX_REDRAW_FOCUSED, NULL)) */
#endif
diff --git a/libgui/inc/gui.h b/libgui/inc/gui.h
index 460bf88..d160333 100644
--- a/libgui/inc/gui.h
+++ b/libgui/inc/gui.h
@@ -8,8 +8,7 @@
struct gui_window {
u32 id;
- struct context *ctx;
- vec2 *pos;
+ struct context ctx;
};
s32 gui_new_window(struct gui_window *win);
diff --git a/libgui/inc/msg.h b/libgui/inc/msg.h
index db00460..7cbfa2c 100644
--- a/libgui/inc/msg.h
+++ b/libgui/inc/msg.h
@@ -4,16 +4,28 @@
#define MSG_H
#include <def.h>
+#include <gfx.h>
#define MSG_MAGIC 0x42042069
#define MSG_SUCCESS (1 << 29)
#define MSG_FAILURE (1 << 30)
-struct message {
+struct message_header {
u32 magic;
- int src;
- int type;
- void *data;
+ u32 src;
+ u32 type;
+ u32 size;
+};
+
+struct message_new_window {
+ struct message_header header;
+ u32 id;
+ struct context ctx;
+};
+
+struct message_redraw_window {
+ struct message_header header;
+ u32 id;
};
enum message_type {
@@ -32,7 +44,7 @@ enum message_type {
GUI_MAX
};
-int msg_send(u32 pid, enum message_type, void *data);
-int msg_receive(struct message *msg);
+int msg_send(u32 pid, enum message_type type, void *data, u32 size);
+int msg_receive(void *buf, u32 size);
#endif
diff --git a/libgui/msg.c b/libgui/msg.c
index 8448e73..3f58267 100644
--- a/libgui/msg.c
+++ b/libgui/msg.c
@@ -5,23 +5,23 @@
#include <print.h>
#include <sys.h>
-int msg_send(u32 pid, enum message_type type, void *data)
+int msg_send(u32 pid, enum message_type type, void *data, u32 size)
{
- struct message msg = { 0 };
- assert((signed)pid != -1);
+ assert((signed)pid != -1 && size >= sizeof(struct message_header));
char path[32] = { 0 };
sprintf(path, "/proc/%d/msg", pid);
- msg.magic = MSG_MAGIC;
- msg.src = getpid();
- msg.type = type;
- msg.data = data;
- return write(path, &msg, 0, sizeof(msg));
+ struct message_header *header = data;
+ header->magic = MSG_MAGIC;
+ header->src = getpid();
+ header->type = type;
+ return write(path, data, 0, size);
}
-int msg_receive(struct message *msg)
+int msg_receive(void *buf, u32 size)
{
- int ret = read("/proc/self/msg", msg, 0, sizeof(*msg));
- if (msg->magic == MSG_MAGIC && ret == sizeof(*msg))
+ int ret = read("/proc/self/msg", buf, 0, size);
+ struct message_header *header = buf;
+ if (header->magic == MSG_MAGIC)
return ret;
else
return -1;
diff --git a/run b/run
index 1a8202a..cd03ba3 100755
--- a/run
+++ b/run
@@ -26,7 +26,7 @@ no_ask="${2}"
# TODO: Support -enable-kvm: GPF?!
qemu_with_flags() {
network="rtl8139"
- qemu-system-i386 -cpu max -no-reboot -vga std -rtc base=localtime -m 256M -netdev user,id=net0,hostfwd=tcp:127.0.0.1:8000-10.0.2.15:8000 -device $network,netdev=net0 -object filter-dump,id=dump,netdev=net0,file=dump.pcap "$@"
+ qemu-system-i386 -d guest_errors -cpu max -no-reboot -vga std -rtc base=localtime -m 256M -netdev user,id=net0,hostfwd=tcp:127.0.0.1:8000-10.0.2.15:8000 -device $network,netdev=net0 -object filter-dump,id=dump,netdev=net0,file=dump.pcap "$@"
}
make_cross() {
@@ -189,6 +189,10 @@ make_addr() {
addr2line -e build/"$1".elf -f -p "$2"
}
+make_cloc() {
+ cloc . --exclude-dir=build,iso,disk,res,cross
+}
+
make_append_commands() {
s=""
while read -r data; do
@@ -239,6 +243,8 @@ elif [ "${mode}" = "disasm" ]; then
make_disasm "$2" "$3"
elif [ "${mode}" = "addr" ]; then
make_addr "$2" "$3"
+elif [ "${mode}" = "cloc" ]; then
+ make_cloc
elif [ "${mode}" = "sync" ]; then
make_sync
elif [ "${mode}" = "disk" ]; then
@@ -267,6 +273,7 @@ else
printf "again\t\tOpens QEMU again using the previous build\n"
printf "disasm\t\tDisassembles a given part of Melvix\n"
printf "addr\t\tResolves an address to a line of code\n"
+ printf "cloc\t\tCount the total lines of code\n"
printf "sync\t\tSyncs the 'tags' and 'compile_commands.json' file\n"
printf "disk\t\tPrepares the userspace disk (e.g. fonts)\n"
printf "*\t\tAnything else prints this help\n"