Compare commits

..

11 commits

14 changed files with 272 additions and 51 deletions

View file

@ -6,25 +6,93 @@
.set CHECKSUM, -(MAGIC + FLAGS) # checksum to prove we're multiboot .set CHECKSUM, -(MAGIC + FLAGS) # checksum to prove we're multiboot
# Declare header for multiboot # Declare header for multiboot
.section .multiboot .section .multiboot.data, "aw"
.align 4 .align 4
.long MAGIC .long MAGIC
.long FLAGS .long FLAGS
.long CHECKSUM .long CHECKSUM
# Reserve stack for initial thread # Reserve stack for initial thread
.section .bss .section .bootstrap_stack, "aw", @nobits
.align 16
stack_bottom: stack_bottom:
.skip 16384 # 16K .skip 16384 # 16K
stack_top: stack_top:
# Preallocate pages for paging
.section .bss, "aw", @nobits
.align 4096
boot_page_directory:
.skip 4096
boot_page_table1:
.skip 4096
# May need additional page for kernel > 3 MiB
# Kernel entry # Kernel entry
.section .text .section .multiboot.text, "a"
.global _start .global _start
.type _start, @function .type _start, @function
_start: _start:
movl $stack_top, %esp # Physical address of boot_page_table 1
movl $(boot_page_table1 - 0xC0000000), %edi
# Map address 0
movl $0, %esi
# Map first 1023 pages
movl $1023, %ecx
1:
# Only map the kernel
cmpl $_kernel_start, %esi
jl 2f
cmpl $(_kernel_end - 0xC0000000), %esi
jge 3f
# Map physical address as "present, writable"
# TODO: map .text and .rodata as non-writable
movl %esi, %edx
orl $0x003, %edx
movl %edx, (%edi)
2:
# Size of page is 4KiB
addl $4096, %esi
# Size of entries in boot_page_table1 is 4 bytes
addl $4, %edi
# Loop to next if not done
loop 1b
3:
# Map VGA memory to 0xC03FF000 as "present, writable"
movl $(0x000B8000 | 0x003), boot_page_table1 - 0xC0000000 + 1023 * 4
# Map page table to 0x0 and 0xC0000000
movl $(boot_page_table1 - 0xC0000000 + 0x003), boot_page_directory - 0xC0000000 + 0
movl $(boot_page_table1 - 0xC0000000 + 0x003), boot_page_directory - 0xC0000000 + 768 * 4
# Set cr3 to address of boot page directory
movl $(boot_page_directory - 0xC0000000), %ecx
movl %ecx, %cr3
# Enable paging and write-protect
movl %cr0, %ecx
orl $0x80010000, %ecx
movl %ecx, %cr0
# Jump to higher half
lea 4f, %ecx
jmp *%ecx
.section .text
4:
# Unmap identity mapping
movl $0, boot_page_directory + 0
# Reload cr3 to force TLB flush
movl %cr3, %ecx
movl %ecx, %cr3
# Set up stack
mov $stack_top, %esp
# Call global constructors # Call global constructors
call _init call _init
@ -36,4 +104,3 @@ _start:
cli cli
1: hlt 1: hlt
jmp 1b jmp 1b
.size _start, . - _start

View file

@ -3,6 +3,7 @@
#include <string.h> #include <string.h>
#include <sys/io.h> #include <sys/io.h>
#include "gdt.h" #include "gdt.h"
#include "ps2_kbd.h"
int tss[16][2]; int tss[16][2];
@ -42,11 +43,84 @@ void setup_gdt(void) {
extern void enter_pmode(void); extern void enter_pmode(void);
int initialize_ps2_kbd() {
/* Should really make sure PS/2 controller exists here: */
/* Disable devices */
outb(0xAD, 0x64);
outb(0xA7, 0xAD);
/* Flush output buffer */
get_ps2_inbyte();
/* Set config byte */
outb(0x20, 0x64);
unsigned char config_byte = get_ps2_inbyte();
int is_dual_ch = (config_byte & 020) != 0 ? 1 : 0;
config_byte &= 0274;
outb(0x60, 0x64);
outb(config_byte, 0x60);
/* Quick self-test */
outb(0xAA, 0x64);
unsigned char test_response = get_ps2_inbyte();
if(test_response != 0x55) {
printf("error: PS/2 self-test failed\n");
return 1;
}
outb(0x60, 0x64);
outb(config_byte, 0x60);
/* Double check dual channel stuff */
if(is_dual_ch) {
outb(0xA8, 0x64);
outb(0x20, 0x64);
unsigned char new_config_byte = get_ps2_inbyte();
is_dual_ch = (new_config_byte & 020) == 0 ? 1 : 0;
if(is_dual_ch) {
outb(0xA7, 0x64);
}
}
/* Interface tests */
outb(0xAB, 0x64);
test_response = get_ps2_inbyte();
int working_ports = test_response == 0x00 ? 01 : 00;
if(test_response != 0x00 && !is_dual_ch) {
printf("error: could not initialize PS/2 device\n");
return 2;
}
if(is_dual_ch) {
outb(0xA9, 0x64);
test_response = get_ps2_inbyte();
working_ports |= test_response == 0x00 ? 02 : 00;
if(test_response != 0x00 && working_ports != 01) {
printf("error: could not initialize PS/2 device\n");
return 2;
}
}
/* Enable devices */
if(working_ports & 01) {
outb(0xAE, 0x64);
config_byte |= 01;
}
if(working_ports & 02) {
outb(0xA8, 0x64);
config_byte |= 02;
}
outb(0x60, 0x64);
outb(config_byte, 0x60);
return 0;
}
int init(void) { int init(void) {
printf("Setting up GDT...\n"); printf("Setting up GDT...\n");
setup_gdt(); setup_gdt();
printf("Enabling interrupts...\n"); printf("Enabling interrupts...\n");
idt_init(); idt_init();
printf("Fenix Dev Pre-release v0.0.3\n"); printf("Initializing PS/2 controller...\n");
initialize_ps2_kbd();
return 0; return 0;
} }

View file

@ -69,99 +69,100 @@
.extern irq15_handler .extern irq15_handler
irq0: irq0:
pusha pushal
call irq0_handler call irq0_handler
popa popal
iret iret
irq1: irq1:
pusha pushal
cld
call irq1_handler call irq1_handler
popa popal
iret iret
irq2: irq2:
pusha pushal
call irq2_handler call irq2_handler
popa popal
iret iret
irq3: irq3:
pusha pushal
call irq3_handler call irq3_handler
popa popal
iret iret
irq4: irq4:
pusha pushal
call irq4_handler call irq4_handler
popa popal
iret iret
irq5: irq5:
pusha pushal
call irq5_handler call irq5_handler
popa popal
iret iret
irq6: irq6:
pusha pushal
call irq6_handler call irq6_handler
popa popal
iret iret
irq7: irq7:
pusha pushal
call irq7_handler call irq7_handler
popa popal
iret iret
irq8: irq8:
pusha pushal
call irq8_handler call irq8_handler
popa popal
iret iret
irq9: irq9:
pusha pushal
call irq9_handler call irq9_handler
popa popal
iret iret
irq10: irq10:
pusha pushal
call irq10_handler call irq10_handler
popa popal
iret iret
irq11: irq11:
pusha pushal
call irq11_handler call irq11_handler
popa popal
iret iret
irq12: irq12:
pusha pushal
call irq12_handler call irq12_handler
popa popal
iret iret
irq13: irq13:
pusha pushal
call irq13_handler call irq13_handler
popa popal
iret iret
irq14: irq14:
pusha pushal
call irq14_handler call irq14_handler
popa popal
iret iret
irq15: irq15:
pusha pushal
call irq15_handler call irq15_handler
popa popal
iret iret
load_idt: load_idt:

View file

@ -1,5 +1,7 @@
#include <kernel/interrupt.h> #include <kernel/interrupt.h>
#include <sys/io.h> #include <sys/io.h>
#include <stdio.h>
#include "ps2_kbd.h"
void idt_init(void) { void idt_init(void) {
extern int load_idt(); extern int load_idt();
@ -192,6 +194,7 @@ void irq0_handler(void) {
/* Keyboard Interrupt */ /* Keyboard Interrupt */
void irq1_handler(void) { void irq1_handler(void) {
printf("owo\n");
outb(0x20, 0x20); outb(0x20, 0x20);
} }

View file

@ -4,21 +4,33 @@ SECTIONS
{ {
. = 1M; . = 1M;
.text BLOCK (4K) : ALIGN (4K) { _kernel_start = .;
*(.multiboot) .multiboot.data : {
*(.multiboot.data)
}
.multiboot.text : {
*(.multiboot.text)
}
. += 0xC0000000;
.text ALIGN (4K) : AT (ADDR (.text) - 0xC0000000) {
*(.text) *(.text)
} }
.rodata BLOCK (4K) : ALIGN (4K) { .rodata ALIGN (4K) : AT (ADDR (.rodata) - 0xC0000000) {
*(.rodata) *(.rodata)
} }
.data BLOCK (4K) : ALIGN (4K) { .data ALIGN (4K) : AT (ADDR(.data) - 0xC0000000) {
*(.data) *(.data)
} }
.bss BLOCK (4K) : ALIGN (4K) { .bss ALIGN (4K) : AT (ADDR (.bss) - 0xC0000000) {
*(COMMON) *(COMMON)
*(.bss) *(.bss)
*(.bootstrap_stack)
} }
_kernel_end = .;
} }

View file

@ -10,4 +10,6 @@ $(ARCHDIR)/cmos.o \
$(ARCHDIR)/gdt.o \ $(ARCHDIR)/gdt.o \
$(ARCHDIR)/gdt_load.o \ $(ARCHDIR)/gdt_load.o \
$(ARCHDIR)/interrupt.o \ $(ARCHDIR)/interrupt.o \
$(ARCHDIR)/inter.o $(ARCHDIR)/inter.o \
$(ARCHDIR)/ps2_kbd.o \
$(ARCHDIR)/syscall_uname.o

15
arch/i386/ps2_kbd.c Normal file
View file

@ -0,0 +1,15 @@
#include "ps2_kbd.h"
#include <sys/io.h>
unsigned char get_ps2_inbyte() {
return inb(0x60);
}
unsigned char* identify_device() {
unsigned char ret_val[2]; int j = 0;
outb(0xF2, 0x64);
for(unsigned char i; i != 0xFA; i = inb(0x60)) {
ret_val[j++] = i;
}
return ret_val;
}

10
arch/i386/ps2_kbd.h Normal file
View file

@ -0,0 +1,10 @@
#ifndef _ARCH_I386_PS2_KBD_H
#define _ARCH_I386_PS2_KBD_H
int scan_set;
unsigned char get_ps2_inbyte();
unsigned char* identify_device();
#endif

13
arch/i386/syscall_uname.c Normal file
View file

@ -0,0 +1,13 @@
#include <sys/utsname.h>
#include <string.h>
#define PTR ((struct utsname *) v)
int __syscall_uname(void * v) {
strcpy(PTR->sysname, "FENIX");
strcpy(PTR->release, "PRE-ALPHA");
strcpy(PTR->version, "20220726_1700");
strcpy(PTR->machine, "i386");
return 0;
}

View file

@ -9,7 +9,7 @@
static const size_t VGA_WIDTH = 80; static const size_t VGA_WIDTH = 80;
static const size_t VGA_HEIGHT = 24; static const size_t VGA_HEIGHT = 24;
static uint16_t* const VGA_MEMORY = (uint16_t*) 0xB8000 ; static uint16_t* const VGA_MEMORY = (uint16_t*) 0xC03FF000 ;
static size_t term_row; static size_t term_row;
static size_t term_col; static size_t term_col;

View file

@ -2,7 +2,7 @@
#define _KERNEL_CMOS #define _KERNEL_CMOS
/* This is kinda important, and has to be changed each year. */ /* This is kinda important, and has to be changed each year. */
#define RELEASE_YEAR 2020 #define RELEASE_YEAR 2022
struct _rtc_val { struct _rtc_val {
unsigned char second; unsigned char second;

View file

@ -1,6 +1,15 @@
#ifndef _KERNEL_INTERRUPT #ifndef _KERNEL_INTERRUPT
#define _KERNEL_INTERRUPT #define _KERNEL_INTERRUPT
#ifdef __GNUC__
struct IDT_entry {
unsigned short int offset_lowerbits;
unsigned short int selector;
unsigned char zero;
unsigned char type_attr;
unsigned short int offset_higherbits;
} __attribute__((packed));
#else
struct IDT_entry { struct IDT_entry {
unsigned short int offset_lowerbits; unsigned short int offset_lowerbits;
unsigned short int selector; unsigned short int selector;
@ -8,7 +17,11 @@ struct IDT_entry {
unsigned char type_attr; unsigned char type_attr;
unsigned short int offset_higherbits; unsigned short int offset_higherbits;
}; };
#endif
#ifdef __GNUC__
__attribute((aligned(0x10)))
#endif
struct IDT_entry IDT[256]; struct IDT_entry IDT[256];
void idt_init(void); void idt_init(void);

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

@ -0,0 +1,6 @@
#ifndef _KERNEL_SYSCALL
#define _KERNEL_SYSCALL
int __syscall_uname(void *);
#endif

View file

@ -1,7 +1,12 @@
#include <kernel/tty.h> #include <kernel/tty.h>
#include <kernel/init.h> #include <kernel/init.h>
#include <sys/utsname.h>
#include <stdio.h>
void kern_main(void) { void kern_main(void) {
term_init(); term_init();
init(); init();
struct utsname u;
uname(&u);
printf("%s %s %s\n", u.sysname, u.release, u.machine);
} }