Skip to content
Snippets Groups Projects
Commit 49d6af7e authored by xliu152's avatar xliu152
Browse files

sha sha

parent 5c83d4f7
No related branches found
No related tags found
No related merge requests found
Pipeline #53873 failed
Showing
with 999 additions and 1 deletion
mp1_crashed @ 51dfe847
Subproject commit 51dfe8470e3a3dbb32a516ce5b8e6c41ce620766
mp2_m @ 66b3f83a
Subproject commit 66b3f83af68888bc5f4e84e2668bea22b03097da
#include "exception.h"
#include "lib.h"
void division_by_zero(){
printf("EXCEPTION : DIVISION BY ZERO!");
while(1);
}
void single_step(){
printf("EXCEPTION: SINGLE STEP!");
while(1);
}
void non_maskable(){
printf("EXCEPTION: NON MASKABLE(NMI)!");
while(1);
}
void breakpoint(){
printf("EXCEPTION: BREAKPOINT!");
while(1);
}
void overflow_trap(){
printf("EXCEPTION: OVERFLOW!");
while(1);
}
void bound_range_exceeded(){
printf("EXCEPTION: BOUNDS RANGE EXCEEDED!");
while(1);
}
void invalid_opcode(){
printf("EXCEPTION: INVALID OPCODE!");
while(1);
}
void coprocessor_not_available(){
printf("EXCEPTION: COPROCESSOR NOT AVAILABLE!");
while(1);
}
void double_fault_exception(){
printf("EXCEPTION: DOUBLE FAULT!");
while(1);
}
void coprocessor_segment_overrun(){
printf("EXCEPTION: COPROCESSOR SEGMENT OVERRUN!");
while(1);
}
void invalid_task_state_segment(){
printf("EXCEPTION: INVALID TASK STATE SEGMENT!");
while(1);
}
void segment_not_present(){
printf("EXCEPTION: SEGMENT NOT PRESENT!");
while(1);
}
void stack_exception(){
printf("EXCEPTION: STACK FAULT!");
while(1);
}
void general_protection_exception(){
printf("EXCEPTION: GENERAL PROTECTION FAULT!");
while(1);
}
void page_fault(){
printf("EXCEPTION: PAGE FAULT!");
int fault_addr;
asm volatile(
"movl %%cr2,%%eax;"
"movl %%eax,%0;"
:"=g"(fault_addr)
:
:"eax"
);
printf("%x",fault_addr);
while(1);
}
void reserved(){
printf("EXCEPTION: RESERVED!");
while(1);
}
void math_fault(){
printf("EXCEPTION: MATH FAULT!");
while(1);
}
void alignment_check(){
printf("EXCEPTION: ALIGNMENT CHECK!");
while(1);
}
void machine_check(){
printf("EXCEPTION: MACHINE CHECK!");
while(1);
}
void floating_point(){
printf("EXCEPTION: SIMD FLOATING POINT EXCEPTION!");
while(1);
}
void virtualization(){
printf("EXCEPTION: VIRTUALIZATION EXCEPTION!");
while(1);
}
void control_protection(){
printf("EXCEPTION: CONTROL PROTECTION EXCEPTION!");
while(1);
}
#ifndef EXCEPTION_H
#define EXCEPTION_H
#include "lib.h"
void division_by_zero();
void single_step();
void non_maskable();
void breakpoint();
void overflow_trap();
void bound_range_exceeded();
void invalid_opcode();
void coprocessor_not_available();
void double_fault_exception();
void coprocessor_segment_overrun();
void invalid_task_state_segment();
void segment_not_present();
void stack_exception();
void general_protection_exception();
void page_fault();
void reserved();
void math_fault();
void alignment_check();
void machine_check();
void floating_point();
void virtualization();
void control_protection();
#endif
/* i8259.c - Functions to interact with the 8259 interrupt controller
* vim:ts=4 noexpandtab
*/
#include "i8259.h"
#include "lib.h"
/* Interrupt masks to determine which interrupts are enabled and disabled */
uint8_t master_mask; /* IRQs 0-7 */
uint8_t slave_mask; /* IRQs 8-15 */
/* Initialize the 8259 PIC */
void i8259_init(void) {
//mask all interrupts
outb(0xFF, 0x21);
outb(0xFF, 0xA1);
//initialize PICs
outb(0x11, 0x20);//master PIC
outb(0x20, 0x21);
outb(0x04, 0x21);
outb(0x11, 0xA0);//slave PIC
outb(0x28, 0xA1);
outb(0x04, 0xA1);
outb(0x01, 0xA1);
}
/* Enable (unmask) the specified IRQ */
void enable_irq(uint32_t irq_num) {
unsigned int enable_irq;
enable_irq=0;
if(irq_num<0) //invalid irq_num
return;
else if(irq_num<=7){ //master's irq
enable_irq=~(1<<irq_num);
master_mask = (inb(0x21) & enable_irq); //get the IRQof master
outb(master_mask, 0x21); //enable the irq
}
else if(irq_num>7){ //slave's irq
irq_num=irq_num-8; //get the slave's IRQ
enable_irq=~(1<<irq_num);
slave_mask = (inb(0xA1) & enable_irq); //get the IRQ of slave
outb(master_mask, 0xA1); //enable the irq
}
}
/* Disable (mask) the specified IRQ */
void disable_irq(uint32_t irq_num) {
unsigned int mask_irq;
mask_irq=0;
if(irq_num<0) //invalid irq_num
return;
else if(irq_num<=7){ //master's irq
mask_irq=1<<irq_num;
master_mask = (inb(0x21) | mask_irq); //get the IRQof master
outb(master_mask, 0x21); //mask the irq
}
else if(irq_num>7){ //slave's irq
irq_num=irq_num-8; //get the slave's IRQ
mask_irq=1<<irq_num;
slave_mask = (inb(0xA1) | mask_irq); //get the IRQ of slave
outb(master_mask, 0xA1); //mask the irq
}
}
/* Send end-of-interrupt signal for the specified IRQ */
void send_eoi(uint32_t irq_num) {
if(irq_num<0) //invalid irq_num
return;
if (irq_num <= 7){ //master chip
outb((0x60+irq_num), 0x20); //tell the master
}
else{ //slave chip
outb((0x60 + (irq_num & 7)), 0xA0); //tell the slave chip
outb(0x62, 0x20); //tell the master chip
}
}
/* i8259.h - Defines used in interactions with the 8259 interrupt
* controller
* vim:ts=4 noexpandtab
*/
#ifndef _I8259_H
#define _I8259_H
#include "types.h"
/* Ports that each PIC sits on */
#define MASTER_8259_PORT 0x20
#define SLAVE_8259_PORT 0xA0
/* Initialization control words to init each PIC.
* See the Intel manuals for details on the meaning
* of each word */
#define ICW1 0x11
#define ICW2_MASTER 0x20
#define ICW2_SLAVE 0x28
#define ICW3_MASTER 0x04
#define ICW3_SLAVE 0x02
#define ICW4 0x01
/* End-of-interrupt byte. This gets OR'd with
* the interrupt number and sent out to the PIC
* to declare the interrupt finished */
#define EOI 0x60
/* Externally-visible functions */
/* Initialize both PICs */
void i8259_init(void);
/* Enable (unmask) the specified IRQ */
void enable_irq(uint32_t irq_num);
/* Disable (mask) the specified IRQ */
void disable_irq(uint32_t irq_num);
/* Send end-of-interrupt signal for the specified IRQ */
void send_eoi(uint32_t irq_num);
#endif /* _I8259_H */
#include "x86_desc.h"
#include "idt.h"
#include "exception.h"
#define SYSTEM_CALL 0x80
#define SYSTEM_CALL_DPL 3
#define HAEDWARE_INT_DPL 0
#define EXCEPTION_DPL 0
#define DIVISION 0x00
#define SINGLE 0x01
#define NMI 0x02
#define BP 0x03
#define OVERFLOW 0x04
#define BOUNDS 0x05
#define OPCODE 0x06
#define NA_COP 0x07
#define DOUBLE_FAULT 0x08
#define SEG_OVR_COP 0x09
#define INVLD_TSS 0x0A
#define SEG_NO_PRESENT 0x0B
#define STACK_FAULT 0x0C
#define GP 0x0D
#define PAGE_FAULT 0x0E
#define RESERVED 0x0F
#define MATH 0x10
#define ALIGN 0x11
#define MACHINE 0x12
#define FLOAT 0x13
#define VIRT 0x14
#define CTL_PROT 0x15
void idt_init(){
int index;
//intel defined exceptions
for(index = 0; index < 32; index++){
idt[index].seg_selector = KERNEL_CS;
idt[index].reserved0 = 0;
idt[index].reserved1 = 1;
idt[index].reserved2 = 1;
idt[index].reserved3 = 1;
idt[index].reserved4 = 0;
idt[index].size = 1;
idt[index].dpl = 0;
idt[index].present = 1;
}
for(index = 32; index < NUM_VEC;index++){
idt[index].seg_selector = KERNEL_CS;
idt[index].reserved0 = 0;
idt[index].reserved1 = 1;
idt[index].reserved2 = 1;
idt[index].reserved3 = 0;
idt[index].reserved4 = 0;
idt[index].size = 1;
idt[index].dpl = 0;
idt[index].present = 1;
if( index == SYSTEM_CALL){
idt[index].dpl = SYSTEM_CALL_DPL;
idt[index].reserved3 = 1;
}
}
SET_IDT_ENTRY(idt[DIVISION], division_by_zero);
SET_IDT_ENTRY(idt[SINGLE], single_step);
SET_IDT_ENTRY(idt[NMI], non_maskable);
SET_IDT_ENTRY(idt[BP], breakpoint);
SET_IDT_ENTRY(idt[OVERFLOW], overflow_trap);
SET_IDT_ENTRY(idt[BOUNDS], bound_range_exceeded);
SET_IDT_ENTRY(idt[OPCODE], invalid_opcode);
SET_IDT_ENTRY(idt[NA_COP], coprocessor_not_available);
SET_IDT_ENTRY(idt[DOUBLE_FAULT], double_fault_exception);
SET_IDT_ENTRY(idt[SEG_OVR_COP], coprocessor_segment_overrun);
SET_IDT_ENTRY(idt[INVLD_TSS], invalid_task_state_segment);
SET_IDT_ENTRY(idt[SEG_NO_PRESENT], segment_not_present);
SET_IDT_ENTRY(idt[STACK_FAULT], stack_exception);
SET_IDT_ENTRY(idt[GP], general_protection_exception);
SET_IDT_ENTRY(idt[PAGE_FAULT], page_fault);
SET_IDT_ENTRY(idt[RESERVED], reserved);
SET_IDT_ENTRY(idt[MATH], math_fault);
SET_IDT_ENTRY(idt[ALIGN], alignment_check);
SET_IDT_ENTRY(idt[MACHINE], machine_check);
SET_IDT_ENTRY(idt[FLOAT], floating_point);
SET_IDT_ENTRY(idt[VIRT], virtualization);
SET_IDT_ENTRY(idt[CTL_PROT], control_protection);
//keyboard int entry
//system call
//SET_IDT_ENTRY(idt[SYSTEM_CALL],system_call_wrapper);
}
#ifndef IDT_H
#define IDT_H
void idt_init();
#endif
/* kernel.c - the C part of the kernel
* vim:ts=4 noexpandtab
*/
#include "multiboot.h"
#include "x86_desc.h"
#include "lib.h"
#include "i8259.h"
#include "debug.h"
#include "tests.h"
#include "idt.h"
#include "paging.h"
#include "exception.h"
#define RUN_TESTS
/* Macros. */
/* Check if the bit BIT in FLAGS is set. */
#define CHECK_FLAG(flags, bit) ((flags) & (1 << (bit)))
/* Check if MAGIC is valid and print the Multiboot information structure
pointed by ADDR. */
void entry(unsigned long magic, unsigned long addr) {
multiboot_info_t *mbi;
/* Clear the screen. */
clear();
/* Am I booted by a Multiboot-compliant boot loader? */
if (magic != MULTIBOOT_BOOTLOADER_MAGIC) {
printf("Invalid magic number: 0x%#x\n", (unsigned)magic);
return;
}
/* Set MBI to the address of the Multiboot information structure. */
mbi = (multiboot_info_t *) addr;
/* Print out the flags. */
printf("flags = 0x%#x\n", (unsigned)mbi->flags);
/* Are mem_* valid? */
if (CHECK_FLAG(mbi->flags, 0))
printf("mem_lower = %uKB, mem_upper = %uKB\n", (unsigned)mbi->mem_lower, (unsigned)mbi->mem_upper);
/* Is boot_device valid? */
if (CHECK_FLAG(mbi->flags, 1))
printf("boot_device = 0x%#x\n", (unsigned)mbi->boot_device);
/* Is the command line passed? */
if (CHECK_FLAG(mbi->flags, 2))
printf("cmdline = %s\n", (char *)mbi->cmdline);
if (CHECK_FLAG(mbi->flags, 3)) {
int mod_count = 0;
int i;
module_t* mod = (module_t*)mbi->mods_addr;
while (mod_count < mbi->mods_count) {
printf("Module %d loaded at address: 0x%#x\n", mod_count, (unsigned int)mod->mod_start);
printf("Module %d ends at address: 0x%#x\n", mod_count, (unsigned int)mod->mod_end);
printf("First few bytes of module:\n");
for (i = 0; i < 16; i++) {
printf("0x%x ", *((char*)(mod->mod_start+i)));
}
printf("\n");
mod_count++;
mod++;
}
}
/* Bits 4 and 5 are mutually exclusive! */
if (CHECK_FLAG(mbi->flags, 4) && CHECK_FLAG(mbi->flags, 5)) {
printf("Both bits 4 and 5 are set.\n");
return;
}
/* Is the section header table of ELF valid? */
if (CHECK_FLAG(mbi->flags, 5)) {
elf_section_header_table_t *elf_sec = &(mbi->elf_sec);
printf("elf_sec: num = %u, size = 0x%#x, addr = 0x%#x, shndx = 0x%#x\n",
(unsigned)elf_sec->num, (unsigned)elf_sec->size,
(unsigned)elf_sec->addr, (unsigned)elf_sec->shndx);
}
/* Are mmap_* valid? */
if (CHECK_FLAG(mbi->flags, 6)) {
memory_map_t *mmap;
printf("mmap_addr = 0x%#x, mmap_length = 0x%x\n",
(unsigned)mbi->mmap_addr, (unsigned)mbi->mmap_length);
for (mmap = (memory_map_t *)mbi->mmap_addr;
(unsigned long)mmap < mbi->mmap_addr + mbi->mmap_length;
mmap = (memory_map_t *)((unsigned long)mmap + mmap->size + sizeof (mmap->size)))
printf(" size = 0x%x, base_addr = 0x%#x%#x\n type = 0x%x, length = 0x%#x%#x\n",
(unsigned)mmap->size,
(unsigned)mmap->base_addr_high,
(unsigned)mmap->base_addr_low,
(unsigned)mmap->type,
(unsigned)mmap->length_high,
(unsigned)mmap->length_low);
}
/* Construct an LDT entry in the GDT */
{
seg_desc_t the_ldt_desc;
the_ldt_desc.granularity = 0x0;
the_ldt_desc.opsize = 0x1;
the_ldt_desc.reserved = 0x0;
the_ldt_desc.avail = 0x0;
the_ldt_desc.present = 0x1;
the_ldt_desc.dpl = 0x0;
the_ldt_desc.sys = 0x0;
the_ldt_desc.type = 0x2;
SET_LDT_PARAMS(the_ldt_desc, &ldt, ldt_size);
ldt_desc_ptr = the_ldt_desc;
lldt(KERNEL_LDT);
}
/* Construct a TSS entry in the GDT */
{
seg_desc_t the_tss_desc;
the_tss_desc.granularity = 0x0;
the_tss_desc.opsize = 0x0;
the_tss_desc.reserved = 0x0;
the_tss_desc.avail = 0x0;
the_tss_desc.seg_lim_19_16 = TSS_SIZE & 0x000F0000;
the_tss_desc.present = 0x1;
the_tss_desc.dpl = 0x0;
the_tss_desc.sys = 0x0;
the_tss_desc.type = 0x9;
the_tss_desc.seg_lim_15_00 = TSS_SIZE & 0x0000FFFF;
SET_TSS_PARAMS(the_tss_desc, &tss, tss_size);
tss_desc_ptr = the_tss_desc;
tss.ldt_segment_selector = KERNEL_LDT;
tss.ss0 = KERNEL_DS;
tss.esp0 = 0x800000;
ltr(KERNEL_TSS);
}
/* Init the PIC */
i8259_init();
idt_init();
lidt(idt_desc_ptr);
/* Initialize devices, memory, filesystem, enable device interrupts on the
* PIC, any other initialization stuff... */
/* Enable interrupts */
/* Do not enable the following until after you have set up your
* IDT correctly otherwise QEMU will triple fault and simple close
* without showing you any output */
/*printf("Enabling Interrupts\n");
sti();*/
#ifdef RUN_TESTS
/* Run tests */
launch_tests();
#endif
/* Execute the first program ("shell") ... */
/* Spin (nicely, so we don't chew up cycles) */
asm volatile (".1: hlt; jmp .1;");
}
#include "keyboard.h"
struct keyboard{
char key;
char value;
char press_shift;
};
struct keyboard keyboard_scancode[119];
void init_keyboard_table(struct keyboard* keyboard_scancode){
keyboard_scancode[0].key = 0x01; //esc
keyboard_scancode[0].value = 0x1B;
keyboard_scancode[1].key = 0x02; //1
keyboard_scancode[1].value = 0x31;
keyboard_scancode[1].press_shift = 0x21;//!
keyboard_scancode[2].key = 0x03; //2
keyboard_scancode[2].value = 0x32;
keyboard_scancode[2].press_shift = 0x40;//@
keyboard_scancode[3].key = 0x04; //3
keyboard_scancode[3].value = 0x33;
keyboard_scancode[3].press_shift = 0x23;//#
keyboard_scancode[4].key = 0x05; //4
keyboard_scancode[4].value = 0x34;
keyboard_scancode[4].press_shift = 0x24;//$
keyboard_scancode[5].key = 0x06; //5
keyboard_scancode[5].value = 0x35;
keyboard_scancode[5].press_shift = 0x25;//%
keyboard_scancode[6].key = 0x07; //6
keyboard_scancode[6].value = 0x36;
keyboard_scancode[6].press_shift = 0x5E;//^
keyboard_scancode[7].key = 0x08; //7
keyboard_scancode[7].value = 0x37;
keyboard_scancode[7].press_shift = 0x26;//&
keyboard_scancode[8].key = 0x09; //8
keyboard_scancode[8].value = 0x38;
keyboard_scancode[8].press_shift = 0x2A;//*
keyboard_scancode[9].key = 0x0A; //9
keyboard_scancode[9].value = 0x39;
keyboard_scancode[9].press_shift = 0x28;//(
keyboard_scancode[10].key = 0x0B;//0
keyboard_scancode[10].value = 0x30;
keyboard_scancode[10].press_shift = 0x29;//)
keyboard_scancode[11].key = 0x81;//esc released
keyboard_scancode[12].key = 0x82;//1 released
keyboard_scancode[13].key = 0x83;//2 released
keyboard_scancode[14].key = 0x84;//3 released
keyboard_scancode[15].key = 0x85;//4 released
keyboard_scancode[16].key = 0x86;//5 released
keyboard_scancode[17].key = 0x87;//6 released
keyboard_scancode[18].key = 0x88;//7 released
keyboard_scancode[19].key = 0x89;//8 released
keyboard_scancode[20].key = 0x8A;//9 released
keyboard_scancode[21].key = 0x8B;//0 released
//alphabets
keyboard_scancode[22].key = 0x10;//q
keyboard_scancode[22].value = 0x71;
keyboard_scancode[22].press_shift = 0x51;//Q
keyboard_scancode[23].key = 0x11;//w
keyboard_scancode[23].value = 0x77;
keyboard_scancode[23].press_shift = 0x57;//W
keyboard_scancode[24].key = 0x12;//e
keyboard_scancode[24].value = 0x65;
keyboard_scancode[24].press_shift = 0x45;//E
keyboard_scancode[25].key = 0x13;//r
keyboard_scancode[25].value = 0x72;
keyboard_scancode[25].press_shift = 0x52;//R
keyboard_scancode[26].key = 0x14;//t
keyboard_scancode[26].value = 0x74;
keyboard_scancode[26].press_shift = 0x54;//T
keyboard_scancode[27].key = 0x15;//y
keyboard_scancode[27].value = 0x79;
keyboard_scancode[27].press_shift = 0x59;//Y
keyboard_scancode[28].key = 0x16;//u
keyboard_scancode[28].value = 0x75;
keyboard_scancode[28].press_shift = 0x55;//U
keyboard_scancode[29].key = 0x17;//i
keyboard_scancode[29].value = 0x69;
keyboard_scancode[29].press_shift = 0x49;//I
keyboard_scancode[30].key = 0x18;//o
keyboard_scancode[30].value = 0x6F;
keyboard_scancode[30].press_shift = 0x4F;//O
keyboard_scancode[31].key = 0x19;//p
keyboard_scancode[31].value = 0x70;
keyboard_scancode[31].press_shift = 0x50;//P
keyboard_scancode[32].key = 0x1E;//a
keyboard_scancode[32].value = 0x61;
keyboard_scancode[32].press_shift = 0x41;//A
keyboard_scancode[33].key = 0x1F;//s
keyboard_scancode[33].value = 0x73;
keyboard_scancode[33].press_shift = 0x53;//S
keyboard_scancode[34].key = 0x20;//d
keyboard_scancode[34].value = 0x64;
keyboard_scancode[34].press_shift = 0x44;//D
keyboard_scancode[35].key = 0x21;//f
keyboard_scancode[35].value = 0x66;
keyboard_scancode[35].press_shift = 0x46;//F
keyboard_scancode[36].key = 0x22;//g
keyboard_scancode[36].value = 0x67;
keyboard_scancode[36].press_shift = 0x47;//G
keyboard_scancode[37].key = 0x23;//h
keyboard_scancode[37].value = 0x68;
keyboard_scancode[37].press_shift = 0x48;//H
keyboard_scancode[38].key = 0x24;//j
keyboard_scancode[38].value = 0x6A;
keyboard_scancode[38].press_shift = 0x4A;//J
keyboard_scancode[39].key = 0x25;//k
keyboard_scancode[39].value = 0x6B;
keyboard_scancode[39].press_shift = 0x4B;//K
keyboard_scancode[40].key = 0x26;//l
keyboard_scancode[40].value = 0x6C;
keyboard_scancode[40].press_shift = 0x4C;//L
keyboard_scancode[41].key = 0x2C;//z
keyboard_scancode[41].value = 0x7A;
keyboard_scancode[41].press_shift = 0x5A;//Z
keyboard_scancode[42].key = 0x2D;//x
keyboard_scancode[42].value = 0x78;
keyboard_scancode[42].press_shift = 0x58;//X
keyboard_scancode[43].key = 0x2E;//c
keyboard_scancode[43].value = 0x63;
keyboard_scancode[43].press_shift = 0x43;//C
keyboard_scancode[44].key = 0x2F;//v
keyboard_scancode[44].value = 0x76;
keyboard_scancode[44].press_shift = 0x56;//V
keyboard_scancode[45].key = 0x30;//b
keyboard_scancode[45].value = 0x62;
keyboard_scancode[45].press_shift = 0x42;//B
keyboard_scancode[46].key = 0x31;//n
keyboard_scancode[46].value = 0x6E;
keyboard_scancode[46].press_shift = 0x4E;//N
keyboard_scancode[47].key = 0x32;//m
keyboard_scancode[47].value = 0x6D;
keyboard_scancode[47].press_shift = 0x4D;//M
keyboard_scancode[48].key = 0x90;//q released
keyboard_scancode[49].key = 0x91;//w released
keyboard_scancode[50].key = 0x92;//e released
keyboard_scancode[51].key = 0x93;//r released
keyboard_scancode[52].key = 0x94;//t released
keyboard_scancode[53].key = 0x95;//y released
keyboard_scancode[54].key = 0x96;//u released
keyboard_scancode[55].key = 0x97;//i released
keyboard_scancode[56].key = 0x98;//o released
keyboard_scancode[57].key = 0x99;//p released
keyboard_scancode[58].key = 0x9E;//a released
keyboard_scancode[59].key = 0x9F;//s released
keyboard_scancode[60].key = 0xA0;//d released
keyboard_scancode[61].key = 0xA1;//f released
keyboard_scancode[62].key = 0xA2;//g released
keyboard_scancode[63].key = 0xA3;//h released
keyboard_scancode[64].key = 0xA4;//j released
keyboard_scancode[65].key = 0xA5;//k released
keyboard_scancode[66].key = 0xA6;//l released
keyboard_scancode[67].key = 0xAC;//z released
keyboard_scancode[68].key = 0xAD;//x released
keyboard_scancode[69].key = 0xAE;//c released
keyboard_scancode[70].key = 0xAF;//v released
keyboard_scancode[71].key = 0xB0;//b released
keyboard_scancode[72].key = 0xB1;//n released
keyboard_scancode[73].key = 0xB2;//m released
/* left shift press & release */
keyboard_scancode[74].key = 0x2A;//l shift
keyboard_scancode[75].key = 0xAA;//l shift released
/* right shift press & release */
keyboard_scancode[76].key = 0x36;//r shift
keyboard_scancode[77].key = 0xB6;//r shift released
/* capslock press & release */
keyboard_scancode[78].key = 0x3A;//capslock
keyboard_scancode[79].key = 0xBA;//capslock released
/* backspace press */
keyboard_scancode[80].key = 0x0E;//backspace
/* enter press */
keyboard_scancode[81].key = 0x1C;//enter
keyboard_scancode[81].value = 0x0A; //'\n'
keyboard_scancode[81].press_shift = 0x0A;//'\n'
/* other char */
keyboard_scancode[82].key = 0x0C;//-
keyboard_scancode[82].value = 0x2D;//-
keyboard_scancode[82].press_shift = 0x5F;//_
keyboard_scancode[83].key = 0x0D;//=
keyboard_scancode[83].value = 0x3D;//=
keyboard_scancode[83].press_shift = 0x2B;//+
keyboard_scancode[84].key = 0x1A;//[
keyboard_scancode[84].value = 0x5B;//[
keyboard_scancode[84].press_shift = 0x7B;//{
keyboard_scancode[85].key = 0x1B;//]
keyboard_scancode[85].value = 0x5D;//]
keyboard_scancode[85].press_shift = 0x7D;//}
keyboard_scancode[86].key = 0x27;//;
keyboard_scancode[86].value = 0x3B;//;
keyboard_scancode[86].press_shift = 0x3A;//:
keyboard_scancode[87].key = 0x28;//'
keyboard_scancode[87].value = 0x27;//'
keyboard_scancode[87].press_shift = 0x22;//"
keyboard_scancode[88].key = 0x29;//`
keyboard_scancode[88].value = 0x60;//`
keyboard_scancode[88].press_shift = 0x7E;//~
keyboard_scancode[89].key = 0x2B;//\
keyboard_scancode[89].value = 0x5C;//'\'
keyboard_scancode[89].press_shift = 0x7C;//|
keyboard_scancode[90].key = 0x33;//,
keyboard_scancode[90].value = 0x2C;//,
keyboard_scancode[90].press_shift = 0x3C;//<
keyboard_scancode[91].key = 0x34;//.
keyboard_scancode[91].value = 0x2E;//.
keyboard_scancode[91].press_shift = 0x3E;//>
keyboard_scancode[92].key = 0x35;//"/"
keyboard_scancode[92].value = 0x2F;// "/"
keyboard_scancode[92].press_shift = 0x3F;// ?
keyboard_scancode[93].key = 0x39;//space
keyboard_scancode[93].value = 0x20;//space
keyboard_scancode[93].press_shift = 0x20;//space
keyboard_scancode[94].key = 0x8C;//- release
keyboard_scancode[95].key = 0x8D;//= release
keyboard_scancode[96].key = 0x8E;//backsapce release
keyboard_scancode[97].key = 0x9A;//[ release
keyboard_scancode[98].key = 0x9B;//] release
keyboard_scancode[99].key = 0x9C;//enter release
keyboard_scancode[100].key = 0xA8;//' release
keyboard_scancode[101].key = 0xA9;//` release
keyboard_scancode[102].key = 0xAB;//\ release
keyboard_scancode[103].key = 0xB3;//, release
keyboard_scancode[104].key = 0xB4;//. release
keyboard_scancode[105].key = 0xB5;//'/' release
keyboard_scancode[106].key = 0xA7;//; release
keyboard_scancode[107].key = 0x9C;//enter release
keyboard_scancode[108].key = 0xB9;//space release
keyboard_scancode[109].key = 0x8E;//backspace released
/* left control press & realease */
keyboard_scancode[110].key = 0x1D;//r control pressing
keyboard_scancode[111].key = 0x9D;//l control released
/* left alt press & release */
keyboard_scancode[112].key = 0x38;//left control pressing
keyboard_scancode[113].key = 0xB8;//left alt released
/* F1 F2 F3 */
keyboard_scancode[114].key = 0x3B;//F1 pressing
keyboard_scancode[115].key = 0xBB;//F1 released
keyboard_scancode[116].key = 0x3C;//F2 pressing
keyboard_scancode[117].key = 0xBC;//F2 released
keyboard_scancode[118].key = 0x3D;//F3 pressing
keyboard_scancode[119].key = 0xBD;//F3 released
}
void init_keyboard(){
init_keyboard_table(keyboard_scancode);
enable_irq(0x01);
}
#ifndef KEYBOARD_H
#define KEYBOARD_H
#include "lib.h"
#include "i8259.h"
void init_keyboard();
#endif
#include "paging.h"
//#include "terminal.h"
//#include "process.h"
#define entry_num 1024
#define page_size 4096
#define kernel 0x400000
#define shell 0x800000
#define kernel_page 1
#define user_prog 32
#define vid_mem 33
#define video_mem_start 0xB8
#define bit_1 0x2
#define bit_01 0x3
#define bit_012 0x7
#define bit_017 0x83
#define bit_0127 0x87
#define term0_vid 0xB9
#define term1_vid 0xBA
#define term2_vid 0xBB
#define _4mb 0x400000
#define proc_max 6
static uint32_t page_directory_entry[entry_num] __attribute__((aligned(page_size)));
static uint32_t page_table_entry[entry_num] __attribute__((aligned(page_size)));
static uint32_t vid_page_table_entry[entry_num] __attribute__((aligned(page_size)));
/*interface:
* initialize the paging with different terminals
*/
void init_paging(){
int index;
for (index = 0; index < entry_num; index++){ /* unpresent */
page_directory_entry[index] = bit_1;
page_table_entry[index] = (index * page_size) | bit_1;
}
page_directory_entry[0] = ((unsigned)page_table_entry) | bit_01; /* video memory */
page_table_entry[video_mem_start] |= bit_01;
page_table_entry[term0_vid] |= bit_01;
page_table_entry[term1_vid] |= bit_01;
page_table_entry[term2_vid] |= bit_01;
page_directory_entry[kernel_page] = kernel | bit_017;/* kernel 4mb page */
paging_asm_init();
}
/* interface:
*
* gives memory for the new process
*/
int32_t proc_remap(int pid){
if (pid < 0 || pid >= proc_max){
return -1;
}
page_directory_entry[user_prog] = (shell + _4mb * pid) | bit_0127;
asm volatile(
"movl %0, %%eax\n" /*now flush TLB */
"movl %%eax, %%cr3\n"
:
: "r" (page_directory_entry)
: "eax", "memory", "cc"
);
return 0;
}
/* vid_remap
* Input: terminal id
* Output: none
* function: map the video memory for process run
* in current terminal, dump memory for process run
* in inactive terminal
*/
void vid_remap(int term_id){
page_directory_entry[vid_mem] = ((uint32_t)vid_page_table_entry) | bit_012;
//vid_page_table_entry[term_id] = vid_addr(term_id) | bit_012; /* vid_page_table entry 0, 1, 2 are used by terminal 0, 1, 2 seperately */
asm volatile( /* now flush TLB */
"movl %0, %%eax\n"
"movl %%eax, %%cr3\n"
:
: "r" (page_directory_entry)
: "eax"
);
}
/* paging_asm_init
* Input: none
* Output: none
* side effect: set Paging flag at cr0 bit 31
* set page size extension flag at cr4 bit 4
* set cr3 as base address of page directory at 0
*/
void paging_asm_init(){
asm volatile(
"movl %0, %%eax;" /* page directory base address is saved in cr3 */
"movl %%eax, %%cr3;"
:
: "r" (page_directory_entry)
: "eax","memory", "cc"
);
asm volatile (
"movl %%cr4, %%eax;"
"orl $0x00000010, %%eax;" /* second bit set to 1 which enables the PSE */
"movl %%eax, %%cr4;"
"movl %%cr0, %%eax;"
"orl $0x80000000, %%eax;" /* set paging flag(PG) */
"movl %%eax, %%cr0;"
:
:
: "eax", "memory", "cc"
);
}
#ifndef PAGING_H
#define PAGING_H
#include "types.h"
#include "lib.h"
void init_paging();
void paging_asm_init();
int32_t proc_remap(int pid);
void vid_remap(int term_id);
#endif
#include "rtc.h"
void init_rtc(){
unsigned char copy;
char rtc_rate;
copy = inb(0x71);
outb(0x8B, 0x70);
outb((copy | 0x40), 0x71);
rtc_rate = 0x0F;
outb(0x8A, 0x70);
copy = inb(0x71);
outb(0x8A, 0x70);
outb(((copy & 0xF0) | 0x0F), 0x71);
enable_irq(0x02);
enable_irq(0x08);
}
#ifndef RTC_H
#define RTC_H
#include "lib.h"
#include "i8259.h"
void init_rtc();
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment