Skip to content

Commit

Permalink
kernel: statically allocate unique objects
Browse files Browse the repository at this point in the history
  • Loading branch information
mosmeh committed Jun 16, 2024
1 parent cf85a9c commit d360519
Show file tree
Hide file tree
Showing 14 changed files with 96 additions and 148 deletions.
16 changes: 6 additions & 10 deletions kernel/ac97.c
Original file line number Diff line number Diff line change
Expand Up @@ -273,17 +273,13 @@ static short ac97_device_poll(file_description* desc, short events) {
return revents;
}

struct inode* ac97_device_create(void) {
struct inode* inode = kmalloc(sizeof(struct inode));
if (!inode)
return ERR_PTR(-ENOMEM);

struct inode* ac97_device_get(void) {
static file_ops fops = {.write = ac97_device_write,
.ioctl = ac97_device_ioctl,
.poll = ac97_device_poll};
*inode = (struct inode){.fops = &fops,
.mode = S_IFCHR,
.device_id = makedev(14, 3),
.ref_count = 1};
return inode;
static struct inode inode = {.fops = &fops,
.mode = S_IFCHR,
.device_id = makedev(14, 3),
.ref_count = 1};
return &inode;
}
4 changes: 2 additions & 2 deletions kernel/console/console.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
#include <stddef.h>

void fb_console_init(void);
struct inode* fb_console_device_create(void);
struct inode* fb_console_device_get(void);

void serial_console_init(void);
void serial_console_on_char(uint16_t port, char);
struct inode* serial_console_device_create(uint16_t port);

void system_console_init(void);
struct inode* system_console_device_create(void);
struct inode* system_console_device_get(void);

void tty_maybe_send_signal(pid_t pgid, char ch);
16 changes: 6 additions & 10 deletions kernel/console/fb_console.c
Original file line number Diff line number Diff line change
Expand Up @@ -610,18 +610,14 @@ static short fb_console_device_poll(file_description* desc, short events) {
return revents;
}

struct inode* fb_console_device_create(void) {
struct inode* inode = kmalloc(sizeof(struct inode));
if (!inode)
return ERR_PTR(-ENOMEM);

struct inode* fb_console_device_get(void) {
static file_ops fops = {.read = fb_console_device_read,
.write = fb_console_device_write,
.ioctl = fb_console_device_ioctl,
.poll = fb_console_device_poll};
*inode = (struct inode){.fops = &fops,
.mode = S_IFCHR,
.device_id = makedev(5, 0),
.ref_count = 1};
return inode;
static struct inode inode = {.fops = &fops,
.mode = S_IFCHR,
.device_id = makedev(5, 0),
.ref_count = 1};
return &inode;
}
16 changes: 6 additions & 10 deletions kernel/console/system_console.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,15 @@ static int system_console_device_ioctl(file_description* desc, int request,
return file_description_ioctl(active_console, request, user_argp);
}

struct inode* system_console_device_create(void) {
struct inode* inode = kmalloc(sizeof(struct inode));
if (!inode)
return ERR_PTR(-ENOMEM);

struct inode* system_console_device_get(void) {
static file_ops fops = {
.read = system_console_device_read,
.write = system_console_device_write,
.ioctl = system_console_device_ioctl,
};
*inode = (struct inode){.fops = &fops,
.mode = S_IFCHR,
.device_id = makedev(5, 1),
.ref_count = 1};
return inode;
static struct inode inode = {.fops = &fops,
.mode = S_IFCHR,
.device_id = makedev(5, 1),
.ref_count = 1};
return &inode;
}
12 changes: 4 additions & 8 deletions kernel/graphics/bochs.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,8 @@ struct fb* bochs_fb_init(void) {
kprintf("Found framebuffer at P0x%x\n", paddr);
configure(640, 480, 32);

struct fb* fb = kmalloc(sizeof(struct fb));
if (!fb)
return ERR_PTR(-ENOMEM);
*fb = (struct fb){.get_info = bochs_fb_get_info,
.set_info = bochs_fb_set_info,
.mmap = bochs_fb_mmap};

return fb;
static struct fb fb = {.get_info = bochs_fb_get_info,
.set_info = bochs_fb_set_info,
.mmap = bochs_fb_mmap};
return &fb;
}
16 changes: 6 additions & 10 deletions kernel/graphics/fb.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,11 @@ static int fb_device_ioctl(file_description* desc, int request,
return 0;
}

struct inode* fb_device_create(void) {
struct inode* inode = kmalloc(sizeof(struct inode));
if (!inode)
return ERR_PTR(-ENOMEM);

struct inode* fb_device_get(void) {
static file_ops fops = {.mmap = fb_device_mmap, .ioctl = fb_device_ioctl};
*inode = (struct inode){.fops = &fops,
.mode = S_IFBLK,
.device_id = makedev(29, 0),
.ref_count = 1};
return inode;
static struct inode inode = {.fops = &fops,
.mode = S_IFBLK,
.device_id = makedev(29, 0),
.ref_count = 1};
return &inode;
}
2 changes: 1 addition & 1 deletion kernel/graphics/graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ NODISCARD int fb_set_info(struct fb_info*);
NODISCARD int fb_mmap(uintptr_t addr, size_t length, off_t offset,
uint16_t page_flags);

struct inode* fb_device_create(void);
struct inode* fb_device_get(void);
12 changes: 4 additions & 8 deletions kernel/graphics/multiboot.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,8 @@ struct fb* multiboot_fb_init(const multiboot_info_t* mb_info) {
info.bpp = mb_info->framebuffer_bpp;
kprintf("Found framebuffer at P0x%x\n", paddr);

struct fb* fb = kmalloc(sizeof(struct fb));
if (!fb)
return ERR_PTR(-ENOMEM);
*fb = (struct fb){.get_info = multiboot_fb_get_info,
.set_info = multiboot_fb_set_info,
.mmap = multiboot_fb_mmap};

return fb;
static struct fb fb = {.get_info = multiboot_fb_get_info,
.set_info = multiboot_fb_set_info,
.mmap = multiboot_fb_mmap};
return &fb;
}
4 changes: 2 additions & 2 deletions kernel/hid/hid.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ typedef void (*ps2_key_event_handler_fn)(const key_event*);

NODISCARD bool ps2_init(void);
void ps2_set_key_event_handler(ps2_key_event_handler_fn);
struct inode* ps2_keyboard_device_create(void);
struct inode* ps2_mouse_device_create(void);
struct inode* ps2_keyboard_device_get(void);
struct inode* ps2_mouse_device_get(void);
16 changes: 6 additions & 10 deletions kernel/hid/keyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -377,16 +377,12 @@ static short ps2_keyboard_device_poll(file_description* desc, short events) {
return revents;
}

struct inode* ps2_keyboard_device_create(void) {
struct inode* inode = kmalloc(sizeof(struct inode));
if (!inode)
return ERR_PTR(-ENOMEM);

struct inode* ps2_keyboard_device_get(void) {
static file_ops fops = {.read = ps2_keyboard_device_read,
.poll = ps2_keyboard_device_poll};
*inode = (struct inode){.fops = &fops,
.mode = S_IFCHR,
.device_id = makedev(11, 0),
.ref_count = 1};
return inode;
static struct inode inode = {.fops = &fops,
.mode = S_IFCHR,
.device_id = makedev(11, 0),
.ref_count = 1};
return &inode;
}
16 changes: 6 additions & 10 deletions kernel/hid/mouse.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,12 @@ static short ps2_mouse_device_poll(file_description* desc, short events) {
return revents;
}

struct inode* ps2_mouse_device_create(void) {
struct inode* inode = kmalloc(sizeof(struct inode));
if (!inode)
return ERR_PTR(-ENOMEM);

struct inode* ps2_mouse_device_get(void) {
static file_ops fops = {.read = ps2_mouse_device_read,
.poll = ps2_mouse_device_poll};
*inode = (struct inode){.fops = &fops,
.mode = S_IFCHR,
.device_id = makedev(10, 1),
.ref_count = 1};
return inode;
static struct inode inode = {.fops = &fops,
.mode = S_IFCHR,
.device_id = makedev(10, 1),
.ref_count = 1};
return &inode;
}
22 changes: 11 additions & 11 deletions kernel/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,25 +74,25 @@ void start(uint32_t mb_magic, uintptr_t mb_info_paddr) {
initrd_populate_root_fs(initrd_paddr, initrd_size);

ASSERT_OK(vfs_mount("/dev", tmpfs_create_root()));
ASSERT_OK(vfs_register_device(null_device_create()));
ASSERT_OK(vfs_register_device(zero_device_create()));
ASSERT_OK(vfs_register_device(full_device_create()));
ASSERT_OK(vfs_register_device(random_device_create()));
ASSERT_OK(vfs_register_device(urandom_device_create()));
ASSERT_OK(vfs_register_device(null_device_get()));
ASSERT_OK(vfs_register_device(zero_device_get()));
ASSERT_OK(vfs_register_device(full_device_get()));
ASSERT_OK(vfs_register_device(random_device_get()));
ASSERT_OK(vfs_register_device(urandom_device_get()));

if (ps2_init()) {
create_char_device("/dev/kbd", ps2_keyboard_device_create());
create_char_device("/dev/psaux", ps2_mouse_device_create());
create_char_device("/dev/kbd", ps2_keyboard_device_get());
create_char_device("/dev/psaux", ps2_mouse_device_get());
}

if (fb_init(mb_info)) {
create_char_device("/dev/fb0", fb_device_create());
create_char_device("/dev/fb0", fb_device_get());
fb_console_init();
create_char_device("/dev/tty", fb_console_device_create());
create_char_device("/dev/tty", fb_console_device_get());
}

if (ac97_init())
create_char_device("/dev/dsp", ac97_device_create());
create_char_device("/dev/dsp", ac97_device_get());

serial_console_init();
if (serial_enable_port(SERIAL_COM1))
Expand All @@ -109,7 +109,7 @@ void start(uint32_t mb_magic, uintptr_t mb_info_paddr) {
serial_console_device_create(SERIAL_COM4));

system_console_init();
ASSERT_OK(vfs_register_device(system_console_device_create()));
ASSERT_OK(vfs_register_device(system_console_device_get()));

syscall_init();
scheduler_init();
Expand Down
80 changes: 30 additions & 50 deletions kernel/pseudo_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,67 +40,47 @@ static ssize_t write_to_full_disk(file_description* desc, const void* buffer,
return 0;
}

struct inode* null_device_create(void) {
struct inode* inode = kmalloc(sizeof(struct inode));
if (!inode)
return ERR_PTR(-ENOMEM);

struct inode* null_device_get(void) {
static file_ops fops = {.read = read_nothing, .write = write_to_bit_bucket};
*inode = (struct inode){.fops = &fops,
.mode = S_IFCHR,
.device_id = makedev(1, 3),
.ref_count = 1};
return inode;
static struct inode inode = {.fops = &fops,
.mode = S_IFCHR,
.device_id = makedev(1, 3),
.ref_count = 1};
return &inode;
}

struct inode* zero_device_create(void) {
struct inode* inode = kmalloc(sizeof(struct inode));
if (!inode)
return ERR_PTR(-ENOMEM);

struct inode* zero_device_get(void) {
static file_ops fops = {.read = read_zeros, .write = write_to_bit_bucket};
*inode = (struct inode){.fops = &fops,
.mode = S_IFCHR,
.device_id = makedev(1, 5),
.ref_count = 1};
return inode;
static struct inode inode = {.fops = &fops,
.mode = S_IFCHR,
.device_id = makedev(1, 5),
.ref_count = 1};
return &inode;
}

struct inode* full_device_create(void) {
struct inode* inode = kmalloc(sizeof(struct inode));
if (!inode)
return ERR_PTR(-ENOMEM);

struct inode* full_device_get(void) {
static file_ops fops = {.read = read_zeros, .write = write_to_full_disk};
*inode = (struct inode){.fops = &fops,
.mode = S_IFCHR,
.device_id = makedev(1, 7),
.ref_count = 1};
return inode;
static struct inode inode = {.fops = &fops,
.mode = S_IFCHR,
.device_id = makedev(1, 7),
.ref_count = 1};
return &inode;
}

struct inode* random_device_create(void) {
struct inode* inode = kmalloc(sizeof(struct inode));
if (!inode)
return ERR_PTR(-ENOMEM);

struct inode* random_device_get(void) {
static file_ops fops = {.read = read_random, .write = write_to_bit_bucket};
*inode = (struct inode){.fops = &fops,
.mode = S_IFCHR,
.device_id = makedev(1, 8),
.ref_count = 1};
return inode;
static struct inode inode = {.fops = &fops,
.mode = S_IFCHR,
.device_id = makedev(1, 8),
.ref_count = 1};
return &inode;
}

struct inode* urandom_device_create(void) {
struct inode* inode = kmalloc(sizeof(struct inode));
if (!inode)
return ERR_PTR(-ENOMEM);

struct inode* urandom_device_get(void) {
static file_ops fops = {.read = read_random, .write = write_to_bit_bucket};
*inode = (struct inode){.fops = &fops,
.mode = S_IFCHR,
.device_id = makedev(1, 9),
.ref_count = 1};
return inode;
static struct inode inode = {.fops = &fops,
.mode = S_IFCHR,
.device_id = makedev(1, 9),
.ref_count = 1};
return &inode;
}
12 changes: 6 additions & 6 deletions kernel/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ noreturn void reboot(void);
noreturn void halt(void);
noreturn void poweroff(void);

struct inode* null_device_create(void);
struct inode* zero_device_create(void);
struct inode* full_device_create(void);
struct inode* random_device_create(void);
struct inode* urandom_device_create(void);
struct inode* null_device_get(void);
struct inode* zero_device_get(void);
struct inode* full_device_get(void);
struct inode* random_device_get(void);
struct inode* urandom_device_get(void);

NODISCARD bool ac97_init(void);
struct inode* ac97_device_create(void);
struct inode* ac97_device_get(void);

0 comments on commit d360519

Please sign in to comment.