Compare commits

...
Author SHA1 Message Date
403166c476 drivers: add flanterm
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-07-17 01:52:46 -04:00
baf8c4c7e5 proc: page: add pgd_clone
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-07-17 00:59:05 -04:00
d7477ae860 amd64: add syscall entry mechanism
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-07-15 20:08:22 -04:00
a73fc47664 proc: add context switching
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-07-15 17:19:46 -04:00
c01b1d7eee syscall: add basic syscall entry
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-07-15 16:36:38 -04:00
1b8fe6b24d kernel: add basic proc outline
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-07-15 14:50:46 -04:00
9de7664bb9 ahci: remove unused functions
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-07-15 14:36:32 -04:00
37 changed files with 4928 additions and 100 deletions

1
.gitignore vendored
View file

@ -12,3 +12,4 @@ edk2-ovmf/
*.hdd
*.cpio
initramfs/
Makefile.local

View file

@ -38,6 +38,7 @@ CFLAGS += \
-mno-red-zone \
-mcmodel=kernel \
-Iinclude \
-Idrivers \
-MMD
LDFLAGS := \
@ -90,7 +91,6 @@ $(KERNEL): $(KOBJ) linker.ld
.PHONY: clean
clean:
$(Q)$(MAKE) -f scripts/Makefile.build _clean obj=.
$(Q)rm -rf $(INITRAMFS)
$(Q)rm -rf $(IMG)
$(Q)rm -rf iso_root
$(Q)rm -rf os_root
@ -105,3 +105,4 @@ config:
$(Q)scripts/config.sh
-include scripts/Makefile.limine
-include Makefile.local

View file

@ -4,7 +4,7 @@ obj-y += drivers/
obj-y += kernel/
obj-y += amd64_init.o
obj-y += exception.o
obj-y += gdt.o
obj-y += idt.o
obj-y += trap.o
obj-y += exception.o
obj-y += syscall.o

View file

@ -1,14 +1,20 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include <drivers/acpi.h>
#include <kernel/tty.h>
#include <kernel/atomic.h>
#include <arch/proc.h>
#include <drivers/acpi.h>
#include <drivers/serial.h>
#include <lib/stdio.h>
#include "arch/msr.h"
#include "arch/reg.h"
#include "descriptor.h"
static struct tty tty_early = {0};
static struct tty_serial_ops tty_early_ops = {.serial_getchar = arch_serial_read, .serial_putchar = arch_serial_write};
static struct cpu_local cpu_local_arr[128];
static u64 cpu_ticker = 0;
void yield()
{
for (;;) {
@ -36,6 +42,21 @@ void arch_early_init()
void arch_init()
{
void _syscall_enter();
gdt_init();
idt_init();
wrmsr(IA32_STAR, ((u64)GDT_SEGMENT_CODE_RING0 << 48) | ((u64)GDT_SEGMENT_CODE_RING3 << 32));
wrmsr(IA32_LSTAR, (u64)_syscall_enter);
wrmsr(IA32_FMASK, RFLAGS_IF);
}
void arch_ap_startup()
{
u64 id = atomic_xadd(&cpu_ticker, 1);
struct cpu_local *this_local = &cpu_local_arr[id];
wrmsr(IA32_GS_BASE, (u64)this_local);
yield();
}

View file

@ -4,3 +4,4 @@ obj-y += page.o
obj-y += reg.o
obj-y += stack.o
obj-y += cache.o
obj-y += entry.o

151
amd64/arch/entry.S Normal file
View file

@ -0,0 +1,151 @@
/* SPDX-License-Identifier: GPL-2.0 */
.intel_syntax noprefix
.macro push_all
push r15
push r14
push r13
push r12
push r11
push r10
push r9
push r8
push rbp
push rsi
push rdi
push rdx
push rcx
push rbx
push rax
.endm
.macro pop_all
pop rax
pop rbx
pop rcx
pop rdx
pop rdi
pop rsi
pop rbp
pop r8
pop r9
pop r10
pop r11
pop r12
pop r13
pop r14
pop r15
.endm
.macro exception nr
.global _exception_\nr
_exception_\nr:
push_all
mov rdi, \nr
call exception_handler
pop_all
iretq
.endm
.macro exception_err nr
.global _exception_\nr
_exception_\nr:
pop rsi
push_all
mov rdi, \nr
call exception_handler
pop_all
iretq
.endm
.macro irq nr
.global _irq_\nr
_irq_\nr:
push_all
mov rdi, \nr
call irq_handler
pop_all
iretq
.endm
.global _syscall_enter
_syscall_enter:
swapgs
/* save user stack */
mov gs:[16], rsp
/* load kernel stack */
mov rsp, gs:[8]
/* save registers into struct pt_regs */
push gs:[16] /* save user RSP */
push_all
mov rdi, rsp
call syscall
/* restore registers */
pop_all
add rsp, 8
mov rsp, [rsp] /* load user RSP */
swapgs
sysretq
.global switch_to
switch_to:
push_all
mov [rdi + 8], rsp /* 8: struct thread stack_addr offset */
mov rsp, [rsi + 8]
pop_all
ret
/* exceptions */
exception 0x00
exception 0x01
exception 0x02
exception 0x03
exception 0x04
exception 0x05
exception 0x06
exception 0x07
exception_err 0x08
exception 0x09
exception_err 0x0A
exception_err 0x0B
exception_err 0x0C
exception_err 0x0D
exception_err 0x0E
exception 0x0F
exception 0x10
exception_err 0x11
exception 0x12
exception 0x13
exception 0x14
exception_err 0x15
exception 0x16
exception 0x17
exception 0x18
exception 0x19
exception 0x1A
exception 0x1B
exception 0x1C
exception_err 0x1D
exception_err 0x1E
exception 0x1F
irq 0x00
irq 0x01
irq 0x02
irq 0x03
irq 0x04
irq 0x05
irq 0x06
irq 0x07
irq 0x08
irq 0x09
irq 0x0A
irq 0x0B
irq 0x0C
irq 0x0D
irq 0x0E
irq 0x0F

View file

@ -50,7 +50,7 @@ lidt:
.global ltr
ltr:
ltr rdi
ltr di
ret
.global tlb_flush

View file

@ -39,8 +39,10 @@ static const char *exception_names[] = {
void exception_handler(u64 no, u64 err)
{
if (no == 0x0e)
kerr("(Page fault): 0x%16x\n", cr2_read());
if (no == 0x0e) {
if (kernel_init_ready)
panic("(Page fault) addr: 0x%16x err: %x\n", cr2_read(), err);
}
if (!kernel_init_ready)
panic("(Exception) %s(%02xh)\n", exception_names[no], no);
}

View file

@ -4,6 +4,11 @@
#include <kernel/types.h>
#define IA32_STAR 0xC0000081
#define IA32_LSTAR 0xC0000082
#define IA32_FMASK 0xC0000084
#define IA32_GS_BASE 0xC0000101
u64 rdmsr(u32 msr);
void wrmsr(u32 msr, u64 val);

View file

@ -26,5 +26,6 @@
u64 *_pt_read(u64 *dir, size_t index, u64 *flags);
void _pt_write(u64 *dir, size_t index, u64 *val, u64 flags);
void pgd_load(pgd_t *pgd);
pgd_t *pgd_get();
#endif /* _AMD64_PAGE_H_ */

36
amd64/include/arch/proc.h Normal file
View file

@ -0,0 +1,36 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#ifndef _ARCH_PROC_H_
#define _ARCH_PROC_H_
#include <kernel/common.h>
#include <kernel/types.h>
struct thread;
struct cpu_local {
struct thread *cur_thread;
vaddr_t kernel_rsp;
vaddr_t user_rsp;
u32 cpu_id;
} PACKED;
struct pt_regs {
u64 rax;
u64 rbx;
u64 rcx;
u64 rdx;
u64 rdi;
u64 rsi;
u64 rbp;
u64 r8;
u64 r9;
u64 r10;
u64 r11;
u64 r12;
u64 r13;
u64 r14;
u64 r15;
u64 rsp;
u64 rip;
} PACKED;
#endif /* _ARCH_PROC_H_ */

17
amd64/syscall.c Normal file
View file

@ -0,0 +1,17 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include <kernel/syscall.h>
#include <arch/proc.h>
static inline i64 _do_syscall(struct pt_regs *regs)
{
syscall_t c = get_syscall(regs->rax);
if (!c)
return -ENOSYS;
return c(regs->rdi, regs->rsi, regs->rdx, regs->r10, regs->r8, regs->r9);
}
void syscall(struct pt_regs *regs)
{
regs->rax = _do_syscall(regs);
}

View file

@ -1,77 +0,0 @@
/* SPDX-License-Identifier: GPL-2.0-only */
.intel_syntax noprefix
.macro exception nr
.global _exception_\nr
_exception_\nr:
mov rdi, \nr
call exception_handler
iretq
.endm
.macro exception_err nr
.global _exception_\nr
_exception_\nr:
mov rdi, \nr
popq rsi
call exception_handler
iretq
.endm
.macro irq nr
.global _irq_\nr
_irq_\nr:
mov rdi, \nr
call irq_handler
iretq
.endm
exception 0x00
exception 0x01
exception 0x02
exception 0x03
exception 0x04
exception 0x05
exception 0x06
exception 0x07
exception 0x08
exception 0x09
exception 0x0A
exception 0x0B
exception 0x0C
exception 0x0D
exception 0x0E
exception 0x0F
exception 0x10
exception 0x11
exception 0x12
exception 0x13
exception 0x14
exception 0x15
exception 0x16
exception 0x17
exception 0x18
exception 0x19
exception 0x1A
exception 0x1B
exception 0x1C
exception 0x1D
exception 0x1E
exception 0x1F
irq 0x00
irq 0x01
irq 0x02
irq 0x03
irq 0x04
irq 0x05
irq 0x06
irq 0x07
irq 0x08
irq 0x09
irq 0x0A
irq 0x0B
irq 0x0C
irq 0x0D
irq 0x0E
irq 0x0F

View file

@ -2,3 +2,4 @@
obj-y += acpi/
obj-y += pci/
obj-y += ahci.o
obj-y += term/

View file

@ -23,9 +23,6 @@ static uint8_t sata_device_count = 0;
static uint8_t cd_device_count = 0;
static struct sata_device sata_devices[512] = {0};
static paddr_t ahci_pbase = 0;
static vaddr_t ahci_vbase = 0;
static int ahci_port_type(hbaport_t *port)
{
uint32_t ssts = port->ssts;
@ -205,13 +202,6 @@ static int ahci_rw_sg(struct sata_device *dev, paddr_t lba, uint16_t count, stru
return ahci_issue_wait(port, slot);
}
static int ahci_rw(struct sata_device *dev, paddr_t lba, uint16_t count, paddr_t buf, bool write)
{
SG_STATIC(sg, 1);
sg_append(&sg, buf, count * dev->sector_size);
return ahci_rw_sg(dev, lba, count, &sg, write);
}
static int ahci_identify(hbaport_t *port, struct ahci_identify *ident, bool packet)
{
void *buf = kalloc(4096, ALLOC_KERN);

4
drivers/term/Makefile Normal file
View file

@ -0,0 +1,4 @@
# SPDX-License-Identifier: GPL-2.0-only
obj-y += flanterm.o
obj-y += flanterm_backends/
obj-y += fbcon.o

60
drivers/term/fbcon.c Normal file
View file

@ -0,0 +1,60 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include <kernel/common.h>
#include <kernel/requests.h>
#include <kernel/mem.h>
#include <kernel/page.h>
#include <kernel/tty.h>
#include <lib/stdio.h>
#include <term/flanterm.h>
#include <term/flanterm_backends/fb.h>
static struct flanterm_context *ft_ctx;
static void fbcon_putchar(struct tty *tty, char c)
{
flanterm_write(ft_ctx, &c, 1);
if (c == '\n') {
c = '\r';
flanterm_write(ft_ctx, &c, 1);
}
}
static char fbcon_getchar(struct tty *tty)
{
return 0;
}
static struct tty_serial_ops fbcon_serial_ops = {
.serial_putchar = fbcon_putchar,
.serial_getchar = fbcon_getchar,
};
void fbcon_init(void)
{
if (!fb_res || !fb_res->framebuffer_count)
return;
struct limine_framebuffer *fb = fb_res->framebuffers[0];
size_t fb_size = fb->pitch * fb->height;
void *fb_virt = map_mmio_dev((paddr_t)TO_PHYS(fb->address), fb_size);
if (!fb_virt)
return;
ft_ctx = flanterm_fb_init(
NULL, NULL, fb_virt, fb->width, fb->height, fb->pitch, fb->red_mask_size, fb->red_mask_shift, fb->green_mask_size,
fb->green_mask_shift, fb->blue_mask_size, fb->blue_mask_shift, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 0,
0, 0, 0, 0, FLANTERM_FB_ROTATE_0);
if (!ft_ctx)
return;
struct tty *fbtty = tty_create("tty1", NULL);
fbtty->serial_ops = fbcon_serial_ops;
tty_printf = fbtty;
kinfo("fbcon: framebuffer console initialized (%lux%lu)\n", fb->width, fb->height);
}

2275
drivers/term/flanterm.c Normal file

File diff suppressed because it is too large Load diff

141
drivers/term/flanterm.h Normal file
View file

@ -0,0 +1,141 @@
/* SPDX-License-Identifier: BSD-2-Clause */
/* Copyright (C) 2022-2026 Mintsuki and contributors.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef FLANTERM_H
#define FLANTERM_H 1
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
#define FLANTERM_CB_DEC 10
#define FLANTERM_CB_BELL 20
#define FLANTERM_CB_PRIVATE_ID 30
#define FLANTERM_CB_STATUS_REPORT 40
#define FLANTERM_CB_POS_REPORT 50
#define FLANTERM_CB_KBD_LEDS 60
#define FLANTERM_CB_MODE 70
#define FLANTERM_CB_LINUX 80
#define FLANTERM_CB_OSC 90
#ifdef FLANTERM_IN_FLANTERM
#include "flanterm_private.h"
#else
struct flanterm_context;
#endif
void flanterm_write(struct flanterm_context *ctx, const char *buf, size_t count);
void flanterm_flush(struct flanterm_context *ctx);
void flanterm_full_refresh(struct flanterm_context *ctx);
void flanterm_deinit(struct flanterm_context *ctx, void (*_free)(void *ptr, size_t size));
void flanterm_get_dimensions(struct flanterm_context *ctx, size_t *cols, size_t *rows);
void flanterm_set_autoflush(struct flanterm_context *ctx, bool state);
void flanterm_set_callback(struct flanterm_context *ctx, void (*callback)(struct flanterm_context *, uint64_t, uint64_t, uint64_t, uint64_t));
/**
* Get the current cursor position.
*
* The returned coordinates are zero-based.
*
* @param ctx Flanterm context to query.
* @param x Receives the cursor X position.
* @param y Receives the cursor Y position.
*/
void flanterm_get_cursor_pos(struct flanterm_context *ctx, size_t *x, size_t *y);
/**
* Set the current cursor position.
*
* Coordinates are zero-based.
*
* @param ctx Flanterm context to update.
* @param x New cursor X position.
* @param y New cursor Y position.
*/
void flanterm_set_cursor_pos(struct flanterm_context *ctx, size_t x, size_t y);
/**
* Set the current text foreground colour.
*
* The colour index uses the terminal's base 0-7 colour range. Brightness is
* controlled separately by the bright flag.
*
* @param ctx Flanterm context to update.
* @param colour Base foreground colour index.
* @param bright True for bright/intense output, false for normal intensity.
*/
void flanterm_set_text_fg(struct flanterm_context *ctx, size_t colour, bool bright);
/**
* Set the current text background colour.
*
* The colour index uses the terminal's base 0-7 colour range. Brightness is
* controlled separately by the bright flag.
*
* @param ctx Flanterm context to update.
* @param colour Base background colour index.
* @param bright True for bright/intense output, false for normal intensity.
*/
void flanterm_set_text_bg(struct flanterm_context *ctx, size_t colour, bool bright);
/**
* Reset the foreground colour to the current default.
*
* @param ctx Flanterm context to update.
*/
void flanterm_reset_text_fg(struct flanterm_context *ctx);
/**
* Reset the background colour to the current default.
*
* @param ctx Flanterm context to update.
*/
void flanterm_reset_text_bg(struct flanterm_context *ctx);
/**
* Clear the terminal contents.
*
* @param ctx Flanterm context to clear.
* @param move True to reposition the cursor as part of the clear.
*/
void flanterm_clear(struct flanterm_context *ctx, bool move);
#ifdef __cplusplus
}
#endif
#endif

View file

@ -0,0 +1,2 @@
# SPDX-License-Identifier: GPL-2.0-only
obj-y += fb.o

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,79 @@
/* SPDX-License-Identifier: BSD-2-Clause */
/* Copyright (C) 2022-2026 Mintsuki and contributors.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef FLANTERM_FB_H
#define FLANTERM_FB_H 1
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
#include "../flanterm.h"
#ifdef FLANTERM_IN_FLANTERM
#include "fb_private.h"
#endif
#define FLANTERM_FB_ROTATE_0 0
#define FLANTERM_FB_ROTATE_90 1
#define FLANTERM_FB_ROTATE_180 2
#define FLANTERM_FB_ROTATE_270 3
struct flanterm_context *flanterm_fb_init(
/* If _malloc and _free are nulled, use the bump allocated instance (1 use only). */
void *(*_malloc)(size_t size),
void (*_free)(void *ptr, size_t size),
uint32_t *framebuffer, size_t width, size_t height, size_t pitch,
uint8_t red_mask_size, uint8_t red_mask_shift,
uint8_t green_mask_size, uint8_t green_mask_shift,
uint8_t blue_mask_size, uint8_t blue_mask_shift,
uint32_t *canvas, /* If nulled, no canvas. */
uint32_t *ansi_colours, uint32_t *ansi_bright_colours, /* If nulled, default. */
uint32_t *default_bg, uint32_t *default_fg, /* If nulled, default. */
uint32_t *default_bg_bright, uint32_t *default_fg_bright, /* If nulled, default. */
/* If font is null, use default font and font_width and font_height ignored. */
void *font, size_t font_width, size_t font_height, size_t font_spacing,
/* If scale_x and scale_y are 0, automatically scale font based on resolution. */
size_t font_scale_x, size_t font_scale_y,
size_t margin,
/* One of FLANTERM_FB_ROTATE_* values. */
int rotation
);
void flanterm_fb_set_flush_callback(struct flanterm_context *ctx, void (*flush_callback)(volatile void *address, size_t length));
#ifdef __cplusplus
}
#endif
#endif

View file

@ -0,0 +1,134 @@
/* SPDX-License-Identifier: BSD-2-Clause */
/* Copyright (C) 2022-2026 Mintsuki and contributors.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef FLANTERM_FB_PRIVATE_H
#define FLANTERM_FB_PRIVATE_H 1
#ifndef FLANTERM_IN_FLANTERM
#error "Do not use fb_private.h. Use interfaces defined in fb.h only."
#endif
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
#define FLANTERM_FB_FONT_GLYPHS 256
struct flanterm_fb_char {
uint8_t c;
bool fg_default;
bool bg_default;
/* 1 byte implicit tail-of-first-word padding */
uint32_t fg;
uint32_t bg;
};
struct flanterm_fb_queue_item {
size_t x, y;
struct flanterm_fb_char c;
};
struct flanterm_fb_context {
struct flanterm_context term;
void (*plot_char)(struct flanterm_context *ctx, struct flanterm_fb_char *c, size_t x, size_t y);
void (*flush_callback)(volatile void *address, size_t length);
size_t font_width;
size_t font_height;
size_t glyph_width;
size_t glyph_height;
size_t font_scale_x;
size_t font_scale_y;
size_t offset_x, offset_y;
volatile uint32_t *framebuffer;
size_t pitch;
size_t width;
size_t height;
size_t phys_height;
size_t bpp;
uint8_t red_mask_size, red_mask_shift;
uint8_t green_mask_size, green_mask_shift;
uint8_t blue_mask_size, blue_mask_shift;
int rotation;
size_t font_bits_size;
uint8_t *font_bits;
size_t font_bool_size;
bool *font_bool;
uint32_t ansi_colours[8];
uint32_t ansi_bright_colours[8];
uint32_t default_fg, default_bg;
uint32_t default_fg_bright, default_bg_bright;
size_t canvas_size;
uint32_t *canvas;
size_t grid_size;
size_t queue_size;
size_t map_size;
struct flanterm_fb_char *grid;
struct flanterm_fb_queue_item *queue;
size_t queue_i;
struct flanterm_fb_queue_item **map;
uint32_t text_fg;
uint32_t text_bg;
bool text_fg_default;
bool text_bg_default;
size_t cursor_x;
size_t cursor_y;
uint32_t saved_state_text_fg;
uint32_t saved_state_text_bg;
bool saved_state_text_fg_default;
bool saved_state_text_bg_default;
size_t saved_state_cursor_x;
size_t saved_state_cursor_y;
size_t old_cursor_x;
size_t old_cursor_y;
};
#ifdef __cplusplus
}
#endif
#endif

View file

@ -0,0 +1,132 @@
/* SPDX-License-Identifier: BSD-2-Clause */
/* Copyright (C) 2022-2026 Mintsuki and contributors.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef FLANTERM_PRIVATE_H
#define FLANTERM_PRIVATE_H 1
#ifndef FLANTERM_IN_FLANTERM
#error "Do not use flanterm_private.h. Use interfaces defined in flanterm.h only."
#endif
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
#define FLANTERM_MAX_ESC_VALUES 16
struct flanterm_context {
/* internal use */
size_t tab_size;
bool autoflush;
bool cursor_enabled;
bool scroll_enabled;
bool wrap_enabled;
bool origin_mode;
bool control_sequence;
bool escape;
bool osc;
bool osc_escape;
size_t osc_buf_i;
uint8_t osc_buf[256];
bool rrr;
bool discard_next;
bool bold;
bool bg_bold;
bool reverse_video;
bool dec_private;
bool insert_mode;
bool csi_unhandled;
uint64_t code_point;
size_t unicode_remaining;
uint8_t g_select;
uint8_t charsets[2];
size_t current_charset;
size_t escape_offset;
size_t esc_values_i;
size_t saved_cursor_x;
size_t saved_cursor_y;
size_t current_primary;
size_t current_bg;
size_t scroll_top_margin;
size_t scroll_bottom_margin;
uint32_t esc_values[FLANTERM_MAX_ESC_VALUES];
uint8_t last_printed_char;
bool last_was_graphic;
bool saved_state_bold;
bool saved_state_bg_bold;
bool saved_state_reverse_video;
bool saved_state_origin_mode;
size_t saved_state_current_charset;
uint8_t saved_state_charsets[2];
size_t saved_state_current_primary;
size_t saved_state_current_bg;
/* to be set by backend */
size_t rows, cols;
void (*raw_putchar)(struct flanterm_context *, uint8_t c);
void (*clear)(struct flanterm_context *, bool move);
void (*set_cursor_pos)(struct flanterm_context *, size_t x, size_t y);
void (*get_cursor_pos)(struct flanterm_context *, size_t *x, size_t *y);
void (*set_text_fg)(struct flanterm_context *, size_t fg);
void (*set_text_bg)(struct flanterm_context *, size_t bg);
void (*set_text_fg_bright)(struct flanterm_context *, size_t fg);
void (*set_text_bg_bright)(struct flanterm_context *, size_t bg);
void (*set_text_fg_rgb)(struct flanterm_context *, uint32_t fg);
void (*set_text_bg_rgb)(struct flanterm_context *, uint32_t bg);
void (*set_text_fg_default)(struct flanterm_context *);
void (*set_text_bg_default)(struct flanterm_context *);
void (*set_text_fg_default_bright)(struct flanterm_context *);
void (*set_text_bg_default_bright)(struct flanterm_context *);
void (*move_character)(struct flanterm_context *, size_t new_x, size_t new_y, size_t old_x, size_t old_y);
void (*scroll)(struct flanterm_context *);
void (*revscroll)(struct flanterm_context *);
void (*swap_palette)(struct flanterm_context *);
void (*save_state)(struct flanterm_context *);
void (*restore_state)(struct flanterm_context *);
void (*double_buffer_flush)(struct flanterm_context *);
void (*full_refresh)(struct flanterm_context *);
void (*deinit)(struct flanterm_context *, void (*)(void *, size_t));
/* to be set by client */
void (*callback)(struct flanterm_context *, uint64_t, uint64_t, uint64_t, uint64_t);
};
void flanterm_context_reinit(struct flanterm_context *ctx);
#ifdef __cplusplus
}
#endif
#endif

View file

@ -31,9 +31,10 @@ vaddr_t page_lowest_free(vaddr_t after, size_t len);
void *map_mmio_dev(paddr_t base, size_t len);
void page_map(pgd_t *pgd_table, paddr_t paddr, vaddr_t vaddr, size_t len, u64 flags, rbtree_t *map_tree);
pgd_t *pgd_get();
pgd_t *pgd_create_user();
pgd_t *pgd_clone(pgd_t *src);
void tlb_flush();
void page_init();
extern pgd_t *kpgd;
#endif /* _PAGE_H_ */

56
include/kernel/proc.h Normal file
View file

@ -0,0 +1,56 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#ifndef _PROC_H_
#define _PROC_H_
#include <kernel/types.h>
#include <kernel/spinlock.h>
#include <kernel/mem.h>
#include <kernel/page.h>
#include <lib/rbtree.h>
#define PROC_STATE_RUNNING 0
#define PROC_STATE_WAITING 1
#define PROC_STATE_DNS 2 /* do not schedule */
typedef u64 pid_t;
typedef u64 tid_t;
struct vm_area {
vaddr_t start;
vaddr_t end;
u64 flags;
};
struct proc;
struct thread {
tid_t tid;
vaddr_t stack_addr;
u64 state;
bool in_kernel;
u64 tls_base;
struct proc *proc;
struct thread *next;
};
struct proc {
pid_t pid;
struct thread main_thread;
char *name;
rbtree_t vm_tree;
spinlock_t vm_lock;
pgd_t *pgd;
spinlock_t sched_lock;
};
void proc_init();
void switch_to(struct thread *from, struct thread *to);
#endif /* _PROC_H_ */

View file

@ -9,6 +9,7 @@ extern struct limine_memmap_response *kern_map_res;
extern struct limine_hhdm_response *kern_hhdm_res;
extern struct limine_rsdp_response *kern_rsdp_res;
extern struct limine_module_response *module_res;
extern struct limine_framebuffer_response *fb_res;
void requests_init();

11
include/kernel/syscall.h Normal file
View file

@ -0,0 +1,11 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#ifndef _SYSCALL_H_
#define _SYSCALL_H_
#include <kernel/types.h>
typedef ssize_t (*syscall_t)(u64 arg1, u64 arg2, u64 arg3, u64 arg4, u64 arg5, u64 arg6);
syscall_t get_syscall(u64 no);
#endif /* _SYSCALL_H_ */

View file

@ -37,6 +37,7 @@
typedef void (*initcall_t)(void);
extern initcall_t __initcall_start, __initcall_end;
extern void fbcon_init(void);
struct tty *tty_printf = NULL;
bool kernel_init_ready = false;
@ -78,10 +79,15 @@ static void kmain()
tty_printf = main_tty;
fbcon_init();
do_initcalls();
void arch_ap_startup();
arch_ap_startup();
/* do nothing, for now :) */
kernel_init_ready = true;
kernel_init_ready = true;
yield();
}

View file

@ -11,6 +11,7 @@ REQUEST struct limine_memmap_request memmap_req = {.id = LIMINE_MEMMAP_REQUEST_I
REQUEST struct limine_hhdm_request hhdm_req = {.id = LIMINE_HHDM_REQUEST_ID, .revision = 0};
REQUEST struct limine_rsdp_request rsdp_req = {.id = LIMINE_RSDP_REQUEST_ID, .revision = 0};
REQUEST struct limine_module_request module_req = {.id = LIMINE_MODULE_REQUEST_ID, .revision = 0};
REQUEST struct limine_framebuffer_request fb_req = {.id = LIMINE_FRAMEBUFFER_REQUEST_ID, .revision = 0};
REQUEST u64 limine_base_revision[] = LIMINE_BASE_REVISION(6);
REQUEST u64 _limine_requests_end[] = LIMINE_REQUESTS_END_MARKER;
@ -20,6 +21,7 @@ struct limine_memmap_response *kern_map_res = NULL;
struct limine_hhdm_response *kern_hhdm_res = NULL;
struct limine_rsdp_response *kern_rsdp_res = NULL;
struct limine_module_response *module_res = NULL;
struct limine_framebuffer_response *fb_res = NULL;
vaddr_t hhdm_start = 0;
@ -30,6 +32,7 @@ void requests_init()
kern_hhdm_res = hhdm_req.response;
kern_rsdp_res = rsdp_req.response;
module_res = module_req.response;
fb_res = fb_req.response;
hhdm_start = kern_hhdm_res->offset;
}

View file

@ -1,5 +1,7 @@
# SPDX-License-Identifier: GPL-2.0-only
obj-y += device.o
obj-y += sg.o
obj-y += syscall.o
obj-y += block/
obj-y += proc/
obj-y += tty/

2
kernel/proc/Makefile Normal file
View file

@ -0,0 +1,2 @@
# SPDX-License-Identifier: GPL-2.0-only
obj-y += proc.o

45
kernel/proc/proc.c Normal file
View file

@ -0,0 +1,45 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include <kernel/proc.h>
#include <kernel/slab.h>
mem_cache_t *proc_slab;
mem_cache_t *thread_slab;
int proc_kill(struct proc *proc, int signal)
{
return -ENOSYS;
}
struct proc *proc_create(struct proc *parent)
{
struct proc *ret = slab_alloc(proc_slab);
if (!ret)
return NULL;
struct thread *r_mt = slab_alloc(thread_slab);
if (!r_mt) {
slab_free(proc_slab, ret);
return NULL;
}
pgd_t *p;
if (parent)
p = parent->pgd;
else
p = kpgd;
return ret;
}
void proc_free(struct proc *proc)
{
slab_free(proc_slab, proc);
}
void proc_init()
{
proc_slab = slab_create("proc_slab", sizeof(struct proc), 1 * MB, 8);
thread_slab = slab_create("thread_slab", sizeof(struct thread), 1 * MB, 8);
}

14
kernel/syscall.c Normal file
View file

@ -0,0 +1,14 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include <kernel/common.h>
#include <kernel/types.h>
#include <kernel/syscall.h>
syscall_t syscalls[512] = {0};
syscall_t get_syscall(u64 no)
{
if (no >= ARRAY_SIZE(syscalls))
return NULL;
return syscalls[no];
}

View file

@ -1,8 +1,9 @@
serial: yes
timeout: 1
resolution: 1920x1080
# The entry name that will be displayed in the boot menu.
/vbos
protocol: limine
kernel_path: boot():/kernel.elf
module_path: boot():/initramfs.cpio
kaslr: yes

View file

@ -111,7 +111,7 @@ static void map_hhdm_zones(pgd_t *pgd_table)
}
hhdm_end = top_paddr;
device_base = ALIGN_UP(hhdm_end + hhdm_start, PAGE_SIZE);
device_base = ALIGN_UP(hhdm_end + PAGE_SIZE, PAGE_SIZE);
for (paddr_t p = 0; p < top_paddr; p += 0x200000) {
vaddr_t v = (vaddr_t)TO_VIRT(p);
@ -166,3 +166,72 @@ void page_init()
page_map(kpgd, kernel_paddr, kernel_start, kernel_size, PAGE_RW, &kmap_tree);
pgd_load(kpgd);
}
pgd_t *pgd_create_user()
{
pgd_t *pgd = pt_alloc();
for (int i = 256; i < 512; i++)
pgd[i] = kpgd[i];
return pgd;
}
pgd_t *pgd_clone(pgd_t *src)
{
pgd_t *pgd = pt_alloc();
/* don't clone a bunch of kernel stuff, just keep it */
for (int i = 256; i < 512; i++)
pgd[i] = kpgd[i];
for (int i = 0; i < 256; i++) {
pgd_t *src_pgd = &src[i];
if (!(src_pgd->pgd & PAGE_PRESENT))
continue;
pud_t *src_pud = (pud_t *)TO_VIRT(src_pgd->pgd & PAGE_MASK);
pud_t *new_pud = pt_alloc();
pgd[i].pgd = TO_PHYS((u64)new_pud) | (src_pgd->pgd & 0xFFF);
for (int j = 0; j < 512; j++) {
pud_t *src_pud_e = &src_pud[j];
if (!(src_pud_e->pud & PAGE_PRESENT))
continue;
pmd_t *src_pmd = (pmd_t *)TO_VIRT(src_pud_e->pud & PAGE_MASK);
pmd_t *new_pmd = pt_alloc();
new_pud[j].pud = TO_PHYS((u64)new_pmd) | (src_pud_e->pud & 0xFFF);
for (int k = 0; k < 512; k++) {
pmd_t *src_pmd_e = &src_pmd[k];
if (!(src_pmd_e->pmd & PAGE_PRESENT))
continue;
if (src_pmd_e->pmd & PAGE_PS) {
new_pmd[k] = *src_pmd_e;
continue;
}
pte_t *src_pte = (pte_t *)TO_VIRT(src_pmd_e->pmd & PAGE_MASK);
pte_t *new_pte = pt_alloc();
new_pmd[k].pmd = TO_PHYS((u64)new_pte) | (src_pmd_e->pmd & 0xFFF);
for (int l = 0; l < 512; l++) {
pte_t pte = src_pte[l];
if (!(pte.pte & PAGE_PRESENT))
continue;
paddr_t phys = pte.pte & PAGE_MASK;
void *copy = buddy_alloc(0);
memcpy(TO_VIRT(copy), TO_VIRT(phys), PAGE_SIZE);
new_pte[l].pte = (paddr_t)copy | (pte.pte & ~PAGE_MASK);
}
}
}
}
return pgd;
}

View file

@ -45,7 +45,6 @@ run: edk2-ovmf $(INITRAMFS) $(IMG)
-M q35 \
-drive if=pflash,unit=0,format=raw,file=edk2-ovmf/ovmf-code-$(ARCH).fd,readonly=on \
-accel kvm \
-nographic \
-hda $(IMG) \
-hdb test_drive.hdd \
$(QEMUFLAGS)
@ -56,7 +55,6 @@ debug: edk2-ovmf $(IMG)
-S -s \
-M q35 \
-drive if=pflash,unit=0,format=raw,file=edk2-ovmf/ovmf-code-$(ARCH).fd,readonly=on \
-nographic \
-hda $(IMG) \
-hdb test_drive.hdd \
$(QEMUFLAGS)