add trap
don't want handle trap now
This commit is contained in:
parent
8301f87815
commit
cb56f42e08
@ -5,6 +5,7 @@ BOOTREL = 0x60000
|
|||||||
|
|
||||||
CC=cc
|
CC=cc
|
||||||
CFLAGS=-m32 -Wall -Werror -march=i386 -O0 -fno-stack-protector -ffreestanding -fno-builtin
|
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
|
CPPFLAGS=-nostdinc
|
||||||
LD=ld
|
LD=ld
|
||||||
AS=as
|
AS=as
|
||||||
|
|||||||
@ -7,11 +7,13 @@ OBJS = $(SSRCS:.S=.o) $(CSRCS:.c=.o)
|
|||||||
|
|
||||||
CFLAGS+=-c -fno-pie
|
CFLAGS+=-c -fno-pie
|
||||||
CPPFLAGS+=-nostdinc -DLOADADDR=$(LOADADDR) -DLINKADDR=$(LINKADDR) -DBOOTMAGIC=$(BOOTMAGIC)
|
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)
|
${PROG}: $(OBJS)
|
||||||
@rm -f $(PROG)
|
@rm -f $(PROG)
|
||||||
$(LD) $(LDFLAGS) -o $(BDIR)/$(PROG) $(OBJS)
|
$(LD) $(LDFLAGS) -o $(BDIR)/$(PROG) $(OBJS)
|
||||||
|
|
||||||
|
$(OBJS): conf.h pio.h
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f $(PROG) $(PROG).bin $(OBJS)
|
rm -f $(PROG) $(PROG).bin *.o
|
||||||
|
|||||||
41
boot/boot.c
41
boot/boot.c
@ -1,9 +1,11 @@
|
|||||||
#include "pio.h"
|
|
||||||
|
|
||||||
typedef unsigned short u16;
|
typedef unsigned short u16;
|
||||||
typedef unsigned char u8;
|
typedef unsigned char u8;
|
||||||
typedef int dev_t;
|
typedef int dev_t;
|
||||||
|
|
||||||
|
#include "pio.h"
|
||||||
|
#include "conf.h"
|
||||||
|
|
||||||
#define VGA_WIDTH 80
|
#define VGA_WIDTH 80
|
||||||
#define VGA_LENGTH 25
|
#define VGA_LENGTH 25
|
||||||
|
|
||||||
@ -40,42 +42,9 @@ itostr(int n, char *s)
|
|||||||
return 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
|
void
|
||||||
boot(dev_t bootdev)
|
boot(dev_t bootdev)
|
||||||
{
|
{
|
||||||
char *p;
|
machdep();
|
||||||
char s[VGA_WIDTH] = "booted on disk:0x";
|
for(;;);
|
||||||
|
|
||||||
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");
|
|
||||||
}
|
}
|
||||||
|
|||||||
67
boot/conf.h
Normal file
67
boot/conf.h
Normal file
@ -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]();
|
||||||
|
}
|
||||||
|
}
|
||||||
132
boot/gdt.S
132
boot/gdt.S
@ -1,5 +1,27 @@
|
|||||||
.file "gdt.S"
|
.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 */
|
/* memory segment types */
|
||||||
#define SDT_MEMRO 16 /* memory read only */
|
#define SDT_MEMRO 16 /* memory read only */
|
||||||
#define SDT_MEMROA 17 /* memory read only accessed */
|
#define SDT_MEMROA 17 /* memory read only accessed */
|
||||||
@ -18,19 +40,70 @@
|
|||||||
#define SDT_MEMERC 30 /* memory execute read conforming */
|
#define SDT_MEMERC 30 /* memory execute read conforming */
|
||||||
#define SDT_MEMERAC 31 /* memory execute read accessed conforming */
|
#define SDT_MEMERAC 31 /* memory execute read accessed conforming */
|
||||||
|
|
||||||
#ifndef _ALIGN_TEXT
|
#define S32TEXT 0x08 // segment selector of text section
|
||||||
# define _ALIGN_TEXT .align 2, 0x120
|
#define SDT_SYS386TGT 15 /* system 386 trap gate */
|
||||||
#endif
|
|
||||||
|
|
||||||
/* NB == No Binding: use .globl or .weak as necessary */
|
#define IPROC(n) X##n
|
||||||
#define _ENTRY_NB(x) \
|
#define idte(e) \
|
||||||
.text; _ALIGN_TEXT; .type x,@function; x:
|
movl $IPROC(e), %eax ; call entry_idt
|
||||||
#define _ENTRY(x) .globl x; _ENTRY_NB(x)
|
|
||||||
|
|
||||||
|
#define IENTRY_ERR(name,err) \
|
||||||
|
IPROC(name): \
|
||||||
|
pushl $err ; \
|
||||||
|
jmp 1f
|
||||||
|
|
||||||
.text
|
.text
|
||||||
.code32
|
.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
|
.align 8
|
||||||
gdt:
|
gdt:
|
||||||
/* 0x00 : null */
|
/* 0x00 : null */
|
||||||
@ -65,10 +138,53 @@ gdt:
|
|||||||
.byte (LINKADDR >> 20) & 0xff # hibase
|
.byte (LINKADDR >> 20) & 0xff # hibase
|
||||||
|
|
||||||
|
|
||||||
|
// Register GDT
|
||||||
.globl Gdtr
|
.globl Gdtr
|
||||||
Gdtr:
|
Gdtr:
|
||||||
.word . - gdt -1
|
.word . - gdt -1
|
||||||
.long gdt
|
.long gdt
|
||||||
.word 0
|
.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
|
||||||
@ -13,6 +13,7 @@ SECTIONS
|
|||||||
} :text
|
} :text
|
||||||
etext = .;
|
etext = .;
|
||||||
.data : { *(.data) } :text
|
.data : { *(.data) } :text
|
||||||
|
.rodata : { *(.rodata) } :text
|
||||||
edata = .;
|
edata = .;
|
||||||
.bss : { *(.bss) } :text
|
.bss : { *(.bss) } :text
|
||||||
ebss = .;
|
ebss = .;
|
||||||
|
|||||||
14
boot/pio.h
14
boot/pio.h
@ -1,8 +1,14 @@
|
|||||||
#define outb(port, data) \
|
|
||||||
(__outb(port, data))
|
|
||||||
|
|
||||||
static __inline void
|
static __inline void
|
||||||
__outb(int port, unsigned char data)
|
outb(int port, u8 data)
|
||||||
{
|
{
|
||||||
__asm volatile("outb %0,%w1" : : "a" (data), "d" (port));
|
__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;
|
||||||
|
};
|
||||||
|
|||||||
11
boot/srt0.S
11
boot/srt0.S
@ -7,6 +7,7 @@
|
|||||||
.globl edata
|
.globl edata
|
||||||
.globl Gdtr
|
.globl Gdtr
|
||||||
.globl boot
|
.globl boot
|
||||||
|
.globl pmm_init
|
||||||
|
|
||||||
.text
|
.text
|
||||||
.code16
|
.code16
|
||||||
@ -39,9 +40,13 @@ _start:
|
|||||||
movl $BOOTSTACK, %esp
|
movl $BOOTSTACK, %esp
|
||||||
pushl %edx
|
pushl %edx
|
||||||
|
|
||||||
|
|
||||||
// fill 0 .bss
|
// fill 0 .bss
|
||||||
xorl %eax, %eax
|
xorl %eax, %eax
|
||||||
|
movl $end, %ecx
|
||||||
|
subl $edata, %ecx
|
||||||
|
movl $edata, %edi
|
||||||
|
cld
|
||||||
|
rep; stosb
|
||||||
|
|
||||||
call boot;
|
call pmm_init
|
||||||
|
call boot
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user