diff options
author | Danny Holman <dholman@gymli.org> | 2024-05-27 14:15:18 -0500 |
---|---|---|
committer | Danny Holman <dholman@gymli.org> | 2024-05-27 14:21:10 -0500 |
commit | d5e63951bdf8c154bafee11c84816539fde77b81 (patch) | |
tree | b18deeadd60f7719404193648a624941a6c8d9a8 | |
parent | fc70ab53e4868c84cb56a5353c3ec2cc5cf827eb (diff) |
arch: i386: add a spinlock implementation
Add two functions to control thread-local spinlocks.
Signed-off-by: Danny Holman <dholman@gymli.org>
-rw-r--r-- | arch/i386/include/kernel/asm.h | 51 | ||||
-rw-r--r-- | arch/i386/kernel/spinlock.s | 30 | ||||
-rw-r--r-- | arch/i386/make.config | 4 | ||||
-rw-r--r-- | include/kernel/video/framebuffer.h | 23 | ||||
-rw-r--r-- | include/kernel/video/vga.h | 33 |
5 files changed, 131 insertions, 10 deletions
diff --git a/arch/i386/include/kernel/asm.h b/arch/i386/include/kernel/asm.h index cf394e1..3c7bc00 100644 --- a/arch/i386/include/kernel/asm.h +++ b/arch/i386/include/kernel/asm.h @@ -1,6 +1,7 @@ #ifndef I386_ASM_H #define I386_ASM_H +#include <kernel/gdt.h> #include <stdint.h> extern uintptr_t _kernel_start; @@ -8,19 +9,30 @@ extern uintptr_t _kernel_end; #define KSTART ((uintptr_t)&_kernel_start) #define KEND ((uintptr_t)&_kernel_end - 0xC0000000) -#define HIMEM_START 0x00100000 #define PAGE_SIZE 4096 -struct thread_block { +#define PCI_CONFIG_ADDR 0xCF8 +#define PCI_CONFIG_DATA 0xCFC + +#define COM_PORT 0x3F8 + +struct regs { + uint32_t eax; + uint32_t ebx; + uint32_t ecx; + uint32_t edx; + uint32_t esi; + uint32_t edi; + uint32_t ebp; uint32_t esp; + + uint32_t cr0; + uint32_t cr2; uint32_t cr3; - unsigned int tid; - unsigned int tgid; - unsigned int state; - struct thread_block *next; + uint32_t cr4; }; -struct regs { +struct isr_frame { uint32_t cr4; uint32_t cr3; uint32_t cr2; @@ -92,16 +104,41 @@ void irq_stub_15(void); void syscall_stub(void); +void aquire_lock(int *lock); +void release_lock(int *lock); + +void enable_paging(uint32_t new_cr3); + static inline void outb(uint16_t port, uint8_t value) { __asm__ volatile("outb %0, %1" : : "a"(value), "Nd"(port)); } +static inline void outw(uint16_t port, uint16_t value) { + __asm__ volatile("outw %0, %1" : : "a"(value), "Nd"(port)); +} + +static inline void outl(uint16_t port, uint32_t value) { + __asm__ volatile("outl %0, %1" : : "a"(value), "Nd"(port)); +} + static inline uint8_t inb(uint16_t port) { uint8_t ret; __asm__ volatile("inb %1, %0" : "=a"(ret) : "Nd"(port)); return ret; } +static inline uint16_t inw(uint16_t port) { + uint16_t ret; + __asm__ volatile("inw %1, %0" : "=a"(ret) : "Nd"(port)); + return ret; +} + +static inline uint32_t inl(uint16_t port) { + uint32_t ret; + __asm__ volatile("inl %1, %0" : "=a"(ret) : "Nd"(port)); + return ret; +} + static inline void enable_ints(void) { __asm__ volatile("sti"); } diff --git a/arch/i386/kernel/spinlock.s b/arch/i386/kernel/spinlock.s new file mode 100644 index 0000000..c8235ca --- /dev/null +++ b/arch/i386/kernel/spinlock.s @@ -0,0 +1,30 @@ +.section .text + +.global aquire_lock +.type aquire_lock, @function +aquire_lock: + pushl %ebp + movl %esp, %ebp +locked: + movl 8(%esp), %eax + lock incl (%eax) + jc spin_wait + + popl %ebp + ret +spin_wait: + test %eax, 1 + jnz spin_wait + jmp locked + +.global release_lock +.type release_lock, @function +release_lock: + pushl %ebp + movl %esp, %ebp + + movl 8(%ebp), %eax + lock decl (%eax) + + popl %ebp + ret diff --git a/arch/i386/make.config b/arch/i386/make.config index d4d98a7..82d0733 100644 --- a/arch/i386/make.config +++ b/arch/i386/make.config @@ -7,15 +7,13 @@ KERNEL_ARCH_OBJS=$(ARCHDIR)/boot/boot.o \ $(ARCHDIR)/kernel/idt.o \ $(ARCHDIR)/kernel/gdt.o \ $(ARCHDIR)/kernel/isr.o \ - $(ARCHDIR)/kernel/framebuffer.o \ $(ARCHDIR)/kernel/multiboot.o \ - $(ARCHDIR)/kernel/keyboard.o \ $(ARCHDIR)/kernel/serial.o \ $(ARCHDIR)/kernel/pic.o \ $(ARCHDIR)/kernel/timer.o \ + $(ARCHDIR)/kernel/spinlock.o \ $(ARCHDIR)/kernel/syscall.o \ $(ARCHDIR)/kernel/paging.o \ $(ARCHDIR)/kernel/pmem.o \ $(ARCHDIR)/kernel/kmalloc.o \ - $(ARCHDIR)/kernel/jump_userspace.o \ $(ARCHDIR)/kernel/switch_thread.o \ diff --git a/include/kernel/video/framebuffer.h b/include/kernel/video/framebuffer.h new file mode 100644 index 0000000..500e71b --- /dev/null +++ b/include/kernel/video/framebuffer.h @@ -0,0 +1,23 @@ +#ifndef KERNEL_FRAMEBUFFER_H +#define KERNEL_FRAMEBUFFER_H + +#include <stddef.h> +#include <stdint.h> + +#define VGA_WIDTH 80 +#define VGA_HEIGHT 25 + +static uint16_t *const VGA_MEMORY = (uint16_t*)0xC03FF000; + +void fb_init(void); +void fb_setcolor(uint8_t color); +void fb_putchar(char c); +void fb_setpos(int x, int y); +void fb_offsetpos(int dx, int dy); + +static inline void fb_write(const char *data, size_t size) { + for (size_t i = 0; i < size; i++) + fb_putchar(data[i]); +} + +#endif diff --git a/include/kernel/video/vga.h b/include/kernel/video/vga.h new file mode 100644 index 0000000..b639a3c --- /dev/null +++ b/include/kernel/video/vga.h @@ -0,0 +1,33 @@ +#ifndef KERNEL_VGA_H +#define KERNEL_VGA_H + +#include <stdint.h> + +enum vga_color { + VGA_COLOR_BLACK = 0, + VGA_COLOR_BLUE = 1, + VGA_COLOR_GREEN = 2, + VGA_COLOR_CYAN = 3, + VGA_COLOR_RED = 4, + VGA_COLOR_MAGENTA = 5, + VGA_COLOR_BROWN = 6, + VGA_COLOR_LIGHT_GREY = 7, + VGA_COLOR_DARK_GREY = 8, + VGA_COLOR_LIGHT_BLUE = 9, + VGA_COLOR_LIGHT_GREEN = 10, + VGA_COLOR_LIGHT_CYAN = 11, + VGA_COLOR_LIGHT_RED = 12, + VGA_COLOR_LIGHT_MAGENTA = 13, + VGA_COLOR_LIGHT_BROWN = 14, + VGA_COLOR_LIGHT_WHITE = 15, +}; + +static inline uint8_t vga_entry_color(enum vga_color fg, enum vga_color bg) { + return fg | bg << 4; +} + +static inline uint16_t vga_entry(unsigned char uc, uint8_t color) { + return (uint16_t)uc | (uint16_t)color << 8; +} + +#endif |