From cb56f42e086a1b3fee9e96449c0341152137b298 Mon Sep 17 00:00:00 2001 From: Hojun-Cho Date: Mon, 2 Dec 2024 10:19:57 +0900 Subject: [PATCH] add trap don't want handle trap now --- Makefile.inc | 1 + boot/Makefile | 6 ++- boot/boot.c | 41 ++------------- boot/conf.h | 67 +++++++++++++++++++++++++ boot/gdt.S | 132 ++++++++++++++++++++++++++++++++++++++++++++++--- boot/ld.script | 1 + boot/pio.h | 14 ++++-- boot/srt0.S | 11 +++-- 8 files changed, 220 insertions(+), 53 deletions(-) create mode 100644 boot/conf.h diff --git a/Makefile.inc b/Makefile.inc index 25ce203..b617a63 100644 --- a/Makefile.inc +++ b/Makefile.inc @@ -5,6 +5,7 @@ BOOTREL = 0x60000 CC=cc CFLAGS=-m32 -Wall -Werror -march=i386 -O0 -fno-stack-protector -ffreestanding -fno-builtin +#CFLAGS=-m32 -march=i386 -O0 -fno-stack-protector -ffreestanding -fno-builtin CPPFLAGS=-nostdinc LD=ld AS=as diff --git a/boot/Makefile b/boot/Makefile index 975cc98..fb7361b 100644 --- a/boot/Makefile +++ b/boot/Makefile @@ -7,11 +7,13 @@ OBJS = $(SSRCS:.S=.o) $(CSRCS:.c=.o) CFLAGS+=-c -fno-pie CPPFLAGS+=-nostdinc -DLOADADDR=$(LOADADDR) -DLINKADDR=$(LINKADDR) -DBOOTMAGIC=$(BOOTMAGIC) -LDFLAGS+= -s -T ld.script -Ttext=$(LINKADDR) --no-omagic +LDFLAGS+= -s -T ld.script -Ttext=$(LINKADDR) --no-omagic -M ${PROG}: $(OBJS) @rm -f $(PROG) $(LD) $(LDFLAGS) -o $(BDIR)/$(PROG) $(OBJS) +$(OBJS): conf.h pio.h + clean: - rm -f $(PROG) $(PROG).bin $(OBJS) + rm -f $(PROG) $(PROG).bin *.o diff --git a/boot/boot.c b/boot/boot.c index 9aa970e..a7d62de 100644 --- a/boot/boot.c +++ b/boot/boot.c @@ -1,9 +1,11 @@ -#include "pio.h" typedef unsigned short u16; typedef unsigned char u8; typedef int dev_t; +#include "pio.h" +#include "conf.h" + #define VGA_WIDTH 80 #define VGA_LENGTH 25 @@ -40,42 +42,9 @@ itostr(int n, char *s) return s; } -void -setcursor(int x, int y) -{ - u16 pos = y * VGA_WIDTH + x; - - outb(0x3D4, 0x0F); - outb(0x3D5, (u8) (pos & 0xFF)); - outb(0x3D4, 0x0E); - outb(0x3D5, (u8) ((pos >> 8) & 0xFF)); -} - -void -wr8(u8 c, u8 fg, u8 bg, int x, int y) -{ - u16 attrib = (bg << 4) | (fg & 0x0F); - volatile u16 * where; - where = (volatile u16 *)0xB8000 + (y * 80 + x); - *where = c | (attrib << 8); -} - void boot(dev_t bootdev) { - char *p; - char s[VGA_WIDTH] = "booted on disk:0x"; - - p = s+strlen(s); - p = itostr((int)bootdev, p); - setcursor(0,0); - for (int i = 0; i < sizeof(s); ++i) - wr8(s[i], 0xe8, 0x00, i, 0); - for(int i = 1; i < VGA_LENGTH; ++i) - for(int j = 0; j < VGA_WIDTH; ++j) - wr8(0x00, 0x91, 0x00, j, i); - // disable cursor - outb(0x3D4, 0x0a); - outb(0x3D5, 0x20); - asm("hlt"); + machdep(); + for(;;); } diff --git a/boot/conf.h b/boot/conf.h new file mode 100644 index 0000000..58557dc --- /dev/null +++ b/boot/conf.h @@ -0,0 +1,67 @@ +#define elem(x) (sizeof(x)/sizeof((x)[0])) + +// IO Port Access Type Purpose +// 0x60 Read/Write Data Port +// 0x64 Read Status Register +// 0x64 Write Command Register + +typedef struct{ + char *name; + void (**probes)(void); + int cnt; +}BootProbe; + +// for high memory +static void +a20up(void) +{ + struct{ + int dport, sport, cport; + u8 a20; + u8 dib, ib, wout; + } i8042 = { + .dport = 0x60, // data port + .sport = 0x64, // status register port + .cport = 0x64, // command register port + .a20 = 0xdf, // enable a20 line value + .dib = 0x01, // kbd data in buffer + .ib = 0x02, // kbd input buffer + .wout = 0xd1, // write output port value + }; + + while(inb(i8042.sport) & i8042.ib) + ; + while(inb(i8042.sport) & i8042.dib) + inb(i8042.dport); + + outb(i8042.cport, i8042.wout); + while(inb(i8042.sport) & i8042.ib) + ; + + outb(i8042.dport, i8042.a20); + while(inb(i8042.sport) & i8042.ib) + ; + while(inb(i8042.sport) & i8042.dib) + inb(i8042.dport); +} + +static void (*probe1[])(void) = { + a20up, +}; +static void (*probe2[])(void) = { +}; + +static BootProbe probes[] = { + {"probing", probe1, elem(probe1) }, + {"disk", probe2, elem(probe2) }, +}; + +static void +machdep(void) +{ + for(int i = 0; i < elem(probes); ++i){ + BootProbe bp = probes[i]; + for(int j = 0; j < bp.cnt; ++j) + bp.probes[j](); + } +} diff --git a/boot/gdt.S b/boot/gdt.S index cec9788..b636223 100644 --- a/boot/gdt.S +++ b/boot/gdt.S @@ -1,5 +1,27 @@ .file "gdt.S" +// Trap +#define T_PRIVINFLT 0 /* privileged instruction */ +#define T_BPTFLT 1 /* breakpoint trap */ +#define T_ARITHTRAP 2 /* arithmetic trap */ +#define T_RESERVED 3 /* reserved fault base */ +#define T_PROTFLT 4 /* protection fault */ +#define T_TRCTRAP 5 /* trace trap */ +#define T_PAGEFLT 6 /* page fault */ +#define T_ALIGNFLT 7 /* alignment fault */ +#define T_DIVIDE 8 /* integer divide fault */ +#define T_NMI 9 /* non-maskable interrupt */ +#define T_OFLOW 10 /* overflow trap */ +#define T_BOUND 11 /* bounds check fault */ +#define T_DNA 12 /* device not available fault */ +#define T_DOUBLEFLT 13 /* double fault */ +#define T_FPOPFLT 14 /* fp coprocessor operand fetch fault (![P]Pro)*/ +#define T_TSSFLT 15 /* invalid tss fault */ +#define T_SEGNPFLT 16 /* segment not present fault */ +#define T_STKFLT 17 /* stack fault */ +#define T_MACHK 18 /* machine check ([P]Pro) */ +#define T_XFTRAP 19 /* SIMD FP exception */ + /* memory segment types */ #define SDT_MEMRO 16 /* memory read only */ #define SDT_MEMROA 17 /* memory read only accessed */ @@ -18,19 +40,70 @@ #define SDT_MEMERC 30 /* memory execute read conforming */ #define SDT_MEMERAC 31 /* memory execute read accessed conforming */ -#ifndef _ALIGN_TEXT -# define _ALIGN_TEXT .align 2, 0x120 -#endif +#define S32TEXT 0x08 // segment selector of text section +#define SDT_SYS386TGT 15 /* system 386 trap gate */ -/* NB == No Binding: use .globl or .weak as necessary */ -#define _ENTRY_NB(x) \ - .text; _ALIGN_TEXT; .type x,@function; x: -#define _ENTRY(x) .globl x; _ENTRY_NB(x) +#define IPROC(n) X##n +#define idte(e) \ + movl $IPROC(e), %eax ; call entry_idt +#define IENTRY_ERR(name,err) \ +IPROC(name): \ + pushl $err ; \ + jmp 1f .text .code32 +.globl pmm_init +entry_idt: +// Table +// IDTR offset + 0 : entry 0 +// IDTR offset + 8 : entry 1 +// +// Gate Descriptor +// 63 ~ 48 : offset low +// 47 ~ 32 : info +// 31 ~ 16 : segment selector +// 15 ~ 0 : offset high + movw %ax, (%ebx) + movw $S32TEXT, 2(%ebx) + movw $((0x80|SDT_SYS386TGT) << 8), 4(%ebx) + shr $16, %eax + movw %ax, 6(%ebx) + addl $8, %ebx + ret + + .align 8, 0x90 +pmm_init: + movl $idt, %ebx + movl $Idtr, %eax + movw $(640 - 1), (%eax) + movl %ebx, 2(%eax) + /* interal interrupts 0~31 */ + idte(de); idte(db); idte(nmi); idte(bp); idte(of); idte(br) + idte(ud); idte(nm); idte(df); idte(fo); idte(ts); idte(np) + idte(ss); idte(gp); idte(pf); idte(xx); idte(mf); idte(ac) + idte(mc) + idte(xx); idte(xx); idte(xx); idte(xx); idte(xx); idte(xx) + idte(xx); idte(xx); idte(xx); idte(xx); idte(xx); idte(xx) + idte(xx) + + lidt Idtr + ret + + .bss + .align 8, 0x90 +idt: + .space 640 + + .globl Idtr +Idtr: + .word 0 + .long 0 + .word 0 + + .text .align 8 gdt: /* 0x00 : null */ @@ -65,10 +138,53 @@ gdt: .byte (LINKADDR >> 20) & 0xff # hibase +// Register GDT .globl Gdtr Gdtr: .word . - gdt -1 .long gdt .word 0 -.end +// ENTRY Reserved +IPROC(xx): + pushl $1 + pushl $T_RESERVED + jmp 1f + +// trap entry points +IENTRY_ERR(de,T_DIVIDE) /* #DE divide by zero */ +IENTRY_ERR(db,T_TRCTRAP) /* #DB debug */ +IENTRY_ERR(nmi,T_NMI) /* NMI */ +IENTRY_ERR(bp,T_BPTFLT) /* #BP breakpoint */ +IENTRY_ERR(of,T_OFLOW) /* #OF overflow */ +IENTRY_ERR(br,T_BOUND) /* #BR BOUND range exceeded */ +IENTRY_ERR(ud,T_PRIVINFLT) /* #UD invalid opcode */ +IENTRY_ERR(nm,T_DNA) /* #NM device not available */ +IENTRY_ERR(df,T_DOUBLEFLT) /* #DF double fault */ +IENTRY_ERR(fo,T_FPOPFLT) /* #FO coprocessor segment overrun */ +IENTRY_ERR(ts,T_TSSFLT) /* #TS invalid TSS */ +IENTRY_ERR(np,T_SEGNPFLT) /* #NP segment not present */ +IENTRY_ERR(ss,T_STKFLT) /* #SS stack fault */ +IENTRY_ERR(gp,T_PROTFLT) /* #GP general protection */ +IENTRY_ERR(pf,T_PAGEFLT) /* #PF page fault */ +IENTRY_ERR(mf,T_ARITHTRAP) /* #MF floating point error */ +IENTRY_ERR(ac,T_ALIGNFLT) /* #AC alignment check */ +IENTRY_ERR(mc,T_MACHK) /* #MC machine check */ + +1: + jmp alltraps + +// in boot mode don't want handle trap +alltraps: + hlt + iret + +// bios interrupt entry point +// switch to real to protected mode and emulate 16bit mode + + .globl EMUh + .align 8, 0x90 +EMUh: + pushl %eax + +.end \ No newline at end of file diff --git a/boot/ld.script b/boot/ld.script index 48ca7ae..95a3ec3 100644 --- a/boot/ld.script +++ b/boot/ld.script @@ -13,6 +13,7 @@ SECTIONS } :text etext = .; .data : { *(.data) } :text + .rodata : { *(.rodata) } :text edata = .; .bss : { *(.bss) } :text ebss = .; diff --git a/boot/pio.h b/boot/pio.h index 4c98414..80daec0 100644 --- a/boot/pio.h +++ b/boot/pio.h @@ -1,8 +1,14 @@ -#define outb(port, data) \ - (__outb(port, data)) - static __inline void -__outb(int port, unsigned char data) +outb(int port, u8 data) { __asm volatile("outb %0,%w1" : : "a" (data), "d" (port)); } + +static __inline u8 +inb(int port) +{ + u8 data; + + __asm volatile("inb %w1,%0" : "=a" (data) : "d" (port)); + return data; +}; diff --git a/boot/srt0.S b/boot/srt0.S index 23ef2d0..a99fe11 100644 --- a/boot/srt0.S +++ b/boot/srt0.S @@ -7,6 +7,7 @@ .globl edata .globl Gdtr .globl boot + .globl pmm_init .text .code16 @@ -39,9 +40,13 @@ _start: movl $BOOTSTACK, %esp pushl %edx - // fill 0 .bss xorl %eax, %eax + movl $end, %ecx + subl $edata, %ecx + movl $edata, %edi + cld + rep; stosb - call boot; - + call pmm_init + call boot