kernel: implement tty driver system

This commit is contained in:
2023-06-10 21:41:07 +01:00
parent e10f11af88
commit d09ad5838e
9 changed files with 198 additions and 54 deletions

View File

@@ -29,8 +29,8 @@ include arch/$(SOCKS_ARCH)/config.mk
#################################### ####################################
KERNEL_SRC_DIRS := init kernel vm ds util obj sched dev test kxld 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_C_FILES := $(foreach dir,$(KERNEL_SRC_DIRS),$(shell find $(dir) -type f -name *.c))
KERNEL_CXX_FILES := $(foreach dir,$(KERNEL_SRC_DIRS),$(wildcard $(dir)/*.cpp)) 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)) 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_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)) LIBC_OBJ := $(addprefix $(BUILD_DIR)/,$(LIBC_C_FILES:.c=.o))
BUILD_ID := $(shell tools/socks.buildid --arch $(SOCKS_ARCH)) BUILD_ID := $(shell tools/socks.buildid --arch $(SOCKS_ARCH))

View File

@@ -48,14 +48,25 @@ kern_status_t framebuffer_device_register(struct device *dev)
return object_namespace_create_link(global_namespace(), path, &dev->dev_base); 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); struct framebuffer_device *fbdev = FRAMEBUFFER_DEVICE(dev);
if (!fbdev) { if (!fbdev) {
return KERN_INVALID_ARGUMENT; 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; return KERN_OK;
} }
@@ -70,7 +81,12 @@ kern_status_t framebuffer_set_varinfo(struct device *dev, const struct framebuff
return KERN_UNSUPPORTED; 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 = { struct device_type_ops framebuffer_type_ops = {

View File

@@ -11,6 +11,7 @@
struct device; struct device;
struct input_event; struct input_event;
struct tty_device;
#define DEV_NAME_MAX OBJECT_NAME_MAX #define DEV_NAME_MAX OBJECT_NAME_MAX
#define DEV_MAJOR_MAX 1024 #define DEV_MAJOR_MAX 1024
@@ -79,6 +80,8 @@ struct block_device {
struct char_device { struct char_device {
struct char_device_ops *c_ops; struct char_device_ops *c_ops;
/* only valid for TTY devices */
struct tty_device *c_tty;
}; };
struct net_device { 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 kern_status_t driver_remove_device(struct driver *driver, struct device *dev);
extern struct driver *system_driver(void); 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); extern kern_status_t framebuffer_set_varinfo(struct device *dev, const struct framebuffer_varinfo *varinfo);
static inline void driver_lock(struct driver *driver) static inline void driver_lock(struct driver *driver)

View File

@@ -2,10 +2,13 @@
#define SOCKS_TTY_H_ #define SOCKS_TTY_H_
#include <socks/status.h> #include <socks/status.h>
#include <socks/device.h>
#include <socks/queue.h> #include <socks/queue.h>
#include <socks/object.h> #include <socks/object.h>
#include <stdint.h> #include <stdint.h>
struct kext;
/* The TTY system. /* The TTY system.
TTYs are an enhanced version of the console object. Rather than a simple output TTYs are an enhanced version of the console object. Rather than a simple output
@@ -25,9 +28,6 @@
extern "C" { extern "C" {
#endif #endif
/* opaque context pointer for use by the tty driver */
typedef void *tty_driver_ctx_t;
enum tty_driver_type { enum tty_driver_type {
/* For TTYs operating on simple IO devices like serial ports. /* For TTYs operating on simple IO devices like serial ports.
Allows writing characters, receiving characters, and not much else. */ Allows writing characters, receiving characters, and not much else. */
@@ -58,38 +58,45 @@ enum tty_scroll_dir {
support, depending on what the driver supports. */ support, depending on what the driver supports. */
typedef uint64_t tty_attrib_t; 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 tty_driver {
struct object tty_base; struct driver tty_base;
char tty_name[16]; char tty_name[16];
enum tty_driver_type tty_type; enum tty_driver_type tty_type;
struct queue_entry tty_list; struct queue_entry tty_head;
void (*tty_init)(tty_driver_ctx_t *ctx); struct tty_driver_ops *tty_ops;
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 { struct tty_device {
int tty_xcur, tty_ycur; unsigned int tty_xcells, tty_ycells;
unsigned int tty_xcur, tty_ycur;
unsigned int tty_iflag, tty_oflag, tty_lflag; 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_register(struct tty_driver *drv);
extern kern_status_t tty_driver_unregister(struct tty_driver *drv); extern kern_status_t tty_driver_unregister(struct tty_driver *drv);
static inline struct driver *tty_driver_base(struct tty_driver *drv)
extern struct tty *tty_create(void); {
extern void tty_destroy(struct tty *tty); return &drv->tty_base;
}
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);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@@ -12,6 +12,10 @@
#include <socks/machine/init.h> #include <socks/machine/init.h>
#include <socks/cpu.h> #include <socks/cpu.h>
#ifdef KEXT_NET_DOORSTUCK_SOCKS_FBCON
#include <socks/fbcon.h>
#endif
extern unsigned long get_rflags(void); extern unsigned long get_rflags(void);
extern char __pstart[], __pend[]; extern char __pstart[], __pend[];
@@ -186,24 +190,17 @@ void kernel_init(uintptr_t arg)
create_kernel_thread(background_thread); create_kernel_thread(background_thread);
struct object *fb;
struct object *kbd; struct object *kbd;
run_all_tests(); run_all_tests();
#ifdef KEXT_NET_DOORSTUCK_SOCKS_FBCON
struct object *fb;
status = object_get("/dev/video/fb0", &fb); status = object_get("/dev/video/fb0", &fb);
if (status == KERN_OK) { if (status == KERN_OK) {
struct device *fb_dev = cast_to_device(fb); start_console_on_framebuffer(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");
} }
#endif
status = object_get("/dev/input/input0", &kbd); status = object_get("/dev/input/input0", &kbd);
if (status != KERN_OK) { if (status != KERN_OK) {

View File

@@ -6,6 +6,20 @@
static struct queue consoles; static struct queue consoles;
static spin_lock_t consoles_lock = SPIN_LOCK_INIT; 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) kern_status_t console_register(struct console *con)
{ {
unsigned long flags; unsigned long flags;
@@ -19,7 +33,13 @@ kern_status_t console_register(struct console *con)
} }
queue_push_back(&consoles, &con->c_list); queue_push_back(&consoles, &con->c_list);
if (!(con->c_flags & CON_BOOT)) {
unregister_boot_consoles();
}
spin_unlock_irqrestore(&consoles_lock, flags); spin_unlock_irqrestore(&consoles_lock, flags);
return KERN_OK; return KERN_OK;
} }

View File

@@ -1,13 +0,0 @@
#include <socks/tty.h>
#include <socks/locks.h>
#include <socks/queue.h>
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;
}

42
kernel/tty/device.c Normal file
View File

@@ -0,0 +1,42 @@
#include <socks/tty.h>
#include <socks/device.h>
#include <socks/libc/stdio.h>
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);
}

71
kernel/tty/driver.c Normal file
View File

@@ -0,0 +1,71 @@
#include <socks/tty.h>
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);
}