From d09ad5838ea7fbcd05d5f7362710fa808f2ca548 Mon Sep 17 00:00:00 2001 From: Max Wash Date: Sat, 10 Jun 2023 21:41:07 +0100 Subject: [PATCH] kernel: implement tty driver system --- Makefile | 6 ++-- dev/fb.c | 22 +++++++++++-- include/socks/device.h | 6 +++- include/socks/tty.h | 53 +++++++++++++++++-------------- init/main.c | 19 +++++------ kernel/console.c | 20 ++++++++++++ kernel/tty.c | 13 -------- kernel/tty/device.c | 42 +++++++++++++++++++++++++ kernel/tty/driver.c | 71 ++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 198 insertions(+), 54 deletions(-) delete mode 100644 kernel/tty.c create mode 100644 kernel/tty/device.c create mode 100644 kernel/tty/driver.c diff --git a/Makefile b/Makefile index 5300de4..c72ec1f 100644 --- a/Makefile +++ b/Makefile @@ -29,8 +29,8 @@ include arch/$(SOCKS_ARCH)/config.mk #################################### KERNEL_SRC_DIRS := init kernel vm ds util obj sched dev test kxld -KERNEL_C_FILES := $(foreach dir,$(KERNEL_SRC_DIRS),$(wildcard $(dir)/*.c)) -KERNEL_CXX_FILES := $(foreach dir,$(KERNEL_SRC_DIRS),$(wildcard $(dir)/*.cpp)) +KERNEL_C_FILES := $(foreach dir,$(KERNEL_SRC_DIRS),$(shell find $(dir) -type f -name *.c)) +KERNEL_CXX_FILES := $(foreach dir,$(KERNEL_SRC_DIRS),$(shell find $(dir) -type f -name *.cpp)) KERNEL_OBJ := $(addprefix $(BUILD_DIR)/,$(KERNEL_C_FILES:.c=.o) $(KERNEL_CXX_FILES:.cpp=.o)) #################################### @@ -38,7 +38,7 @@ KERNEL_OBJ := $(addprefix $(BUILD_DIR)/,$(KERNEL_C_FILES:.c=.o) $(KERNEL_CXX_FIL #################################### LIBC_SRC_DIRS := stdio string ctype -LIBC_C_FILES := $(foreach dir,$(LIBC_SRC_DIRS),$(wildcard libc/$(dir)/*.c)) +LIBC_C_FILES := $(foreach dir,$(LIBC_SRC_DIRS),$(shell find libc/$(dir) -type f -name *.c)) LIBC_OBJ := $(addprefix $(BUILD_DIR)/,$(LIBC_C_FILES:.c=.o)) BUILD_ID := $(shell tools/socks.buildid --arch $(SOCKS_ARCH)) diff --git a/dev/fb.c b/dev/fb.c index 89595d0..64f6953 100644 --- a/dev/fb.c +++ b/dev/fb.c @@ -48,14 +48,25 @@ kern_status_t framebuffer_device_register(struct device *dev) return object_namespace_create_link(global_namespace(), path, &dev->dev_base); } -kern_status_t framebuffer_get_varinfo(struct device *dev, struct framebuffer_varinfo *varinfo) +kern_status_t framebuffer_get_fixedinfo(struct device *dev, struct framebuffer_fixedinfo *out) { struct framebuffer_device *fbdev = FRAMEBUFFER_DEVICE(dev); if (!fbdev) { return KERN_INVALID_ARGUMENT; } - memcpy(varinfo, &fbdev->fb_varinfo, sizeof *varinfo); + memcpy(out, &fbdev->fb_fixedinfo, sizeof *out); + return KERN_OK; +} + +kern_status_t framebuffer_get_varinfo(struct device *dev, struct framebuffer_varinfo *out) +{ + struct framebuffer_device *fbdev = FRAMEBUFFER_DEVICE(dev); + if (!fbdev) { + return KERN_INVALID_ARGUMENT; + } + + memcpy(out, &fbdev->fb_varinfo, sizeof *out); return KERN_OK; } @@ -70,7 +81,12 @@ kern_status_t framebuffer_set_varinfo(struct device *dev, const struct framebuff return KERN_UNSUPPORTED; } - return fbdev->fb_ops->set_varinfo(dev, varinfo); + kern_status_t status = fbdev->fb_ops->set_varinfo(dev, varinfo); + if (status == KERN_OK) { + memcpy(&fbdev->fb_varinfo, varinfo, sizeof *varinfo); + } + + return status; } struct device_type_ops framebuffer_type_ops = { diff --git a/include/socks/device.h b/include/socks/device.h index 8f5fc2a..a4c14d6 100644 --- a/include/socks/device.h +++ b/include/socks/device.h @@ -11,6 +11,7 @@ struct device; struct input_event; +struct tty_device; #define DEV_NAME_MAX OBJECT_NAME_MAX #define DEV_MAJOR_MAX 1024 @@ -79,6 +80,8 @@ struct block_device { struct char_device { struct char_device_ops *c_ops; + /* only valid for TTY devices */ + struct tty_device *c_tty; }; struct net_device { @@ -245,7 +248,8 @@ extern kern_status_t driver_add_device(struct driver *driver, struct device *dev extern kern_status_t driver_remove_device(struct driver *driver, struct device *dev); extern struct driver *system_driver(void); -extern kern_status_t framebuffer_get_varinfo(struct device *dev, struct framebuffer_varinfo *varinfo); +extern kern_status_t framebuffer_get_fixedinfo(struct device *dev, struct framebuffer_fixedinfo *out); +extern kern_status_t framebuffer_get_varinfo(struct device *dev, struct framebuffer_varinfo *out); extern kern_status_t framebuffer_set_varinfo(struct device *dev, const struct framebuffer_varinfo *varinfo); static inline void driver_lock(struct driver *driver) diff --git a/include/socks/tty.h b/include/socks/tty.h index a88a0e1..98d96d1 100644 --- a/include/socks/tty.h +++ b/include/socks/tty.h @@ -2,10 +2,13 @@ #define SOCKS_TTY_H_ #include +#include #include #include #include +struct kext; + /* The TTY system. TTYs are an enhanced version of the console object. Rather than a simple output @@ -25,9 +28,6 @@ extern "C" { #endif -/* opaque context pointer for use by the tty driver */ -typedef void *tty_driver_ctx_t; - enum tty_driver_type { /* For TTYs operating on simple IO devices like serial ports. Allows writing characters, receiving characters, and not much else. */ @@ -58,38 +58,45 @@ enum tty_scroll_dir { support, depending on what the driver supports. */ typedef uint64_t tty_attrib_t; +struct tty_driver_ops { + void (*tty_init)(struct device *dev); + void (*tty_deinit)(struct device *dev); + void (*tty_clear)(struct device *dev, int x, int y, int width, int height); + void (*tty_putc)(struct device *dev, int c, int xpos, int ypos, tty_attrib_t attrib); + void (*tty_set_cursor)(struct device *dev, enum tty_cursor cur); + void (*tty_move_cursor)(struct device *dev, int x, int y); + void (*tty_scroll)(struct device *dev, enum tty_scroll_dir dir, int lines); +}; + struct tty_driver { - struct object tty_base; + struct driver tty_base; char tty_name[16]; enum tty_driver_type tty_type; - struct queue_entry tty_list; + struct queue_entry tty_head; - void (*tty_init)(tty_driver_ctx_t *ctx); - void (*tty_deinit)(tty_driver_ctx_t ctx); - void (*tty_clear)(tty_driver_ctx_t ctx, int x, int y, int width, int height); - void (*tty_putc)(tty_driver_ctx_t ctx, int c, int xpos, int ypos, tty_attrib_t attrib); - void (*tty_set_cursor)(tty_driver_ctx_t ctx, enum tty_cursor cur); - void (*tty_move_cursor)(tty_driver_ctx_t ctx, int x, int y); - void (*tty_scroll)(tty_driver_ctx_t ctx, enum tty_scroll_dir dir, int lines); + struct tty_driver_ops *tty_ops; }; -struct tty { - int tty_xcur, tty_ycur; +struct tty_device { + unsigned int tty_xcells, tty_ycells; + unsigned int tty_xcur, tty_ycur; unsigned int tty_iflag, tty_oflag, tty_lflag; - - tty_driver_ctx_t tty_dctx; - const struct tty_driver *tty_driver; }; +extern kern_status_t tty_init(void); + +extern struct device *tty_device_create(void); +extern kern_status_t tty_device_register(struct device *dev, struct tty_driver *owner, struct device *parent); + +extern struct tty_driver *tty_driver_create(struct kext *self, const char *name); +extern kern_status_t tty_driver_destroy(struct tty_driver *drv); extern kern_status_t tty_driver_register(struct tty_driver *drv); extern kern_status_t tty_driver_unregister(struct tty_driver *drv); - -extern struct tty *tty_create(void); -extern void tty_destroy(struct tty *tty); - -extern int tty_read(struct tty *tty, char *s, unsigned long len); -extern int tty_write(struct tty *tty, const char *s, unsigned long len); +static inline struct driver *tty_driver_base(struct tty_driver *drv) +{ + return &drv->tty_base; +} #ifdef __cplusplus } diff --git a/init/main.c b/init/main.c index 5bde89a..ac036ce 100644 --- a/init/main.c +++ b/init/main.c @@ -12,6 +12,10 @@ #include #include +#ifdef KEXT_NET_DOORSTUCK_SOCKS_FBCON +#include +#endif + extern unsigned long get_rflags(void); extern char __pstart[], __pend[]; @@ -186,24 +190,17 @@ void kernel_init(uintptr_t arg) create_kernel_thread(background_thread); - struct object *fb; struct object *kbd; run_all_tests(); +#ifdef KEXT_NET_DOORSTUCK_SOCKS_FBCON + struct object *fb; status = object_get("/dev/video/fb0", &fb); if (status == KERN_OK) { - struct device *fb_dev = cast_to_device(fb); - struct framebuffer_device *fb_info = FRAMEBUFFER_DEVICE(fb_dev); - - printk("fb: mode=%ux%ux%u type=%u cells=%ux%u", - fb_info->fb_varinfo.fb_xres, fb_info->fb_varinfo.fb_yres, fb_info->fb_varinfo.fb_bpp, - fb_info->fb_varinfo.fb_flags, - fb_info->fb_varinfo.fb_xcells, fb_info->fb_varinfo.fb_ycells); - } else { - printk("fb: unavailable"); + start_console_on_framebuffer(cast_to_device(fb)); } - +#endif status = object_get("/dev/input/input0", &kbd); if (status != KERN_OK) { diff --git a/kernel/console.c b/kernel/console.c index 29abaad..e492846 100644 --- a/kernel/console.c +++ b/kernel/console.c @@ -6,6 +6,20 @@ static struct queue consoles; static spin_lock_t consoles_lock = SPIN_LOCK_INIT; +static void unregister_boot_consoles(void) +{ + struct queue_entry *cur = queue_first(&consoles); + while (cur) { + struct queue_entry *next = cur->qe_next; + struct console *con = QUEUE_CONTAINER(struct console, c_list, cur); + if (con->c_flags & CON_BOOT) { + queue_delete(&consoles, cur); + } + + cur = next; + } +} + kern_status_t console_register(struct console *con) { unsigned long flags; @@ -19,7 +33,13 @@ kern_status_t console_register(struct console *con) } queue_push_back(&consoles, &con->c_list); + + if (!(con->c_flags & CON_BOOT)) { + unregister_boot_consoles(); + } + spin_unlock_irqrestore(&consoles_lock, flags); + return KERN_OK; } diff --git a/kernel/tty.c b/kernel/tty.c deleted file mode 100644 index 669f81b..0000000 --- a/kernel/tty.c +++ /dev/null @@ -1,13 +0,0 @@ -#include -#include -#include - -int tty_read(struct tty *tty, char *s, unsigned long len) -{ - return 0; -} - -int tty_write(struct tty *tty, const char *s, unsigned long len) -{ - return 0; -} diff --git a/kernel/tty/device.c b/kernel/tty/device.c new file mode 100644 index 0000000..e5a094a --- /dev/null +++ b/kernel/tty/device.c @@ -0,0 +1,42 @@ +#include +#include +#include + +struct device *tty_device_create(void) +{ + struct char_device *cdev = char_device_create(); + if (!cdev) { + return NULL; + } + + struct tty_device *tty_dev = kmalloc(sizeof *tty_dev, VM_NORMAL); + if (!tty_dev) { + /* TODO destroy cdev */ + return NULL; + } + + cdev->c_tty = tty_dev; + return char_device_base(cdev); +} + +static kern_status_t generate_name(struct tty_driver *owner, struct device *dev, char out[DEV_NAME_MAX]) +{ + /* minor numbers start at 1. subtract 1 to start at 0 instead */ + snprintf(out, DEV_NAME_MAX, "%s%u", owner->tty_name, dev->dev_minor - 1); + return KERN_OK; +} + +kern_status_t tty_device_register(struct device *dev, struct tty_driver *owner, struct device *parent) +{ + kern_status_t status = device_register(dev, tty_driver_base(owner), parent); + if (status != KERN_OK) { + return status; + } + + char link_name[DEV_NAME_MAX]; + generate_name(owner, dev, link_name); + char link_path[OBJECT_PATH_MAX]; + snprintf(link_path, sizeof link_path, "/dev/tty/%s", link_name); + + return object_namespace_create_link(global_namespace(), link_path, &dev->dev_base); +} diff --git a/kernel/tty/driver.c b/kernel/tty/driver.c new file mode 100644 index 0000000..0c382ae --- /dev/null +++ b/kernel/tty/driver.c @@ -0,0 +1,71 @@ +#include + +static struct vm_cache tty_driver_cache = { + .c_name = "tty_driver", + .c_obj_size = sizeof(struct tty_driver), +}; + +static struct queue tty_drivers; +static spin_lock_t tty_drivers_lock; + +kern_status_t tty_init(void) +{ + vm_cache_init(&tty_driver_cache); + tty_drivers = QUEUE_INIT; + tty_drivers_lock = SPIN_LOCK_INIT; + + return KERN_OK; +} + +struct tty_driver *tty_driver_create(struct kext *self, const char *name) +{ + struct tty_driver *driver = vm_cache_alloc(&tty_driver_cache, VM_NORMAL); + if (!driver) { + return NULL; + } + + kern_status_t status = driver_init(&driver->tty_base, self, name); + if (status != KERN_OK) { + vm_cache_free(&tty_driver_cache, driver); + return NULL; + } + + strncpy(driver->tty_name, name, sizeof driver->tty_name - 1); + + return driver; +} + +kern_status_t tty_driver_destroy(struct tty_driver *driver) +{ + /* TODO */ + return KERN_UNIMPLEMENTED; +} + +kern_status_t tty_driver_register(struct tty_driver *driver) +{ + kern_status_t status = driver_register(&driver->tty_base); + if (status != KERN_OK) { + return status; + } + + unsigned long flags; + spin_lock_irqsave(&tty_drivers_lock, &flags); + queue_push_back(&tty_drivers, &driver->tty_head); + spin_unlock_irqrestore(&tty_drivers_lock, flags); + + return KERN_OK; +} + +kern_status_t tty_driver_unregister(struct tty_driver *driver) +{ + if (driver->tty_base.drv_major == DEV_MAJOR_INVALID) { + return KERN_INVALID_ARGUMENT; + } + + unsigned long flags; + spin_lock_irqsave(&tty_drivers_lock, &flags); + queue_delete(&tty_drivers, &driver->tty_head); + spin_unlock_irqrestore(&tty_drivers_lock, flags); + + return driver_unregister(&driver->tty_base); +}