示例#1
0
/*
 * AP cpu's call this to sync up protected mode.
 *
 * WARNING!  We must ensure that the cpu is sufficiently initialized to
 * be able to use to the FP for our optimized bzero/bcopy code before
 * we enter more mainstream C code.
 *
 * WARNING! %fs is not set up on entry.  This routine sets up %fs.
 */
void
init_secondary(void)
{
	int	gsel_tss;
	int	x, myid = bootAP;
	u_int	cr0;
	struct mdglobaldata *md;
	struct privatespace *ps;

	ps = &CPU_prvspace[myid];

	gdt_segs[GPRIV_SEL].ssd_base = (int)ps;
	gdt_segs[GPROC0_SEL].ssd_base =
		(int) &ps->mdglobaldata.gd_common_tss;
	ps->mdglobaldata.mi.gd_prvspace = ps;

	for (x = 0; x < NGDT; x++) {
		ssdtosd(&gdt_segs[x], &gdt[myid * NGDT + x].sd);
	}

	r_gdt.rd_limit = NGDT * sizeof(gdt[0]) - 1;
	r_gdt.rd_base = (int) &gdt[myid * NGDT];
	lgdt(&r_gdt);			/* does magic intra-segment return */

	lidt(&r_idt);

	lldt(_default_ldt);
	mdcpu->gd_currentldt = _default_ldt;

	gsel_tss = GSEL(GPROC0_SEL, SEL_KPL);
	gdt[myid * NGDT + GPROC0_SEL].sd.sd_type = SDT_SYS386TSS;

	md = mdcpu;	/* loaded through %fs:0 (mdglobaldata.mi.gd_prvspace)*/

	md->gd_common_tss.tss_esp0 = 0;	/* not used until after switch */
	md->gd_common_tss.tss_ss0 = GSEL(GDATA_SEL, SEL_KPL);
	md->gd_common_tss.tss_ioopt = (sizeof md->gd_common_tss) << 16;
	md->gd_tss_gdt = &gdt[myid * NGDT + GPROC0_SEL].sd;
	md->gd_common_tssd = *md->gd_tss_gdt;
	ltr(gsel_tss);

	/*
	 * Set to a known state:
	 * Set by mpboot.s: CR0_PG, CR0_PE
	 * Set by cpu_setregs: CR0_NE, CR0_MP, CR0_TS, CR0_WP, CR0_AM
	 */
	cr0 = rcr0();
	cr0 &= ~(CR0_CD | CR0_NW | CR0_EM);
	load_cr0(cr0);
	pmap_set_opt();		/* PSE/4MB pages, etc */

	/* set up CPU registers and state */
	cpu_setregs();

	/* set up FPU state on the AP */
	npxinit(__INITIAL_NPXCW__);

	/* set up SSE registers */
	enable_sse();
}
示例#2
0
文件: cpu.c 项目: mosconi/openbsd
/*
 * The CPU ends up here when it's ready to run
 * XXX should share some of this with init386 in machdep.c
 * for now it jumps into an infinite loop.
 */
void
cpu_hatch(void *v)
{
	struct cpu_info *ci = (struct cpu_info *)v;
	int s;

	cpu_init_idt();
	lapic_enable();
	lapic_startclock();
	lapic_set_lvt();
	gdt_init_cpu(ci);

	lldt(0);

	npxinit(ci);

	cpu_init(ci);

	/* Re-initialise memory range handling on AP */
	if (mem_range_softc.mr_op != NULL)
		mem_range_softc.mr_op->initAP(&mem_range_softc);

	s = splhigh();		/* XXX prevent softints from running here.. */
	lapic_tpr = 0;
	enable_intr();
	if (mp_verbose)
		printf("%s: CPU at apid %ld running\n",
		    ci->ci_dev.dv_xname, ci->ci_cpuid);
	nanouptime(&ci->ci_schedstate.spc_runtime);
	splx(s);

	SCHED_LOCK(s);
	cpu_switchto(NULL, sched_chooseproc());
}
示例#3
0
/*
 * Set up proc0's TSS and LDT.
 */
void
x86_64_proc0_tss_ldt_init(void)
{
	struct pcb *pcb;
	int x;

	gdt_init();

	cpu_info_primary.ci_curpcb = pcb = &proc0.p_addr->u_pcb;

	pcb->pcb_flags = 0;
	pcb->pcb_tss.tss_iobase =
	    (u_int16_t)((caddr_t)pcb->pcb_iomap - (caddr_t)&pcb->pcb_tss);
	for (x = 0; x < sizeof(pcb->pcb_iomap) / 4; x++)
		pcb->pcb_iomap[x] = 0xffffffff;

	pcb->pcb_ldt_sel = pmap_kernel()->pm_ldt_sel =
	    GSYSSEL(GLDT_SEL, SEL_KPL);
	pcb->pcb_cr0 = rcr0();
	pcb->pcb_tss.tss_rsp0 = (u_int64_t)proc0.p_addr + USPACE - 16;
	pcb->pcb_tss.tss_ist[0] = (u_int64_t)proc0.p_addr + PAGE_SIZE;
	proc0.p_md.md_regs = (struct trapframe *)pcb->pcb_tss.tss_rsp0 - 1;
	proc0.p_md.md_tss_sel = tss_alloc(pcb);

	ltr(proc0.p_md.md_tss_sel);
	lldt(pcb->pcb_ldt_sel);
}
示例#4
0
/*
 * The CPU ends up here when its ready to run
 * This is called from code in mptramp.s; at this point, we are running
 * in the idle pcb/idle stack of the new cpu.  When this function returns,
 * this processor will enter the idle loop and start looking for work.
 *
 * XXX should share some of this with init386 in machdep.c
 */
void
cpu_hatch(void *v)
{
	struct cpu_info *ci = (struct cpu_info *)v;
	int s;

	cpu_init_msrs(ci);

	cpu_probe_features(ci);
	cpu_feature &= ci->ci_feature_flags;

#ifdef DEBUG
	if (ci->ci_flags & CPUF_PRESENT)
		panic("%s: already running!?", ci->ci_dev->dv_xname);
#endif

	ci->ci_flags |= CPUF_PRESENT;

	lapic_enable();
	lapic_initclocks();

	while ((ci->ci_flags & CPUF_GO) == 0)
		delay(10);
#ifdef DEBUG
	if (ci->ci_flags & CPUF_RUNNING)
		panic("%s: already running!?", ci->ci_dev->dv_xname);
#endif

	lcr0(ci->ci_idle_pcb->pcb_cr0);
	cpu_init_idt();
	lapic_set_lvt();
	gdt_init_cpu(ci);
	fpuinit(ci);

	lldt(GSYSSEL(GLDT_SEL, SEL_KPL));

	cpu_init(ci);

	s = splhigh();
	lcr8(0);
	enable_intr();

	microuptime(&ci->ci_schedstate.spc_runtime);
	splx(s);

	SCHED_LOCK(s);
	cpu_switchto(NULL, sched_chooseproc());
}
示例#5
0
}

void sched_init(void)
{
	/* 初始化进程控制块指针数组 */
	/* nothing.在bss段,已被清零 */
	
	proc[0] = &init_proc.proc;
	current = proc[0];
	ticks = 0;

	/* 设置进程0的ldt和tss描述符 */
	set_ldt_desc(0, V_KERNEL_ZONE_START+(unsigned long)&(proc[0]->ldt));
	set_tss_desc(0, V_KERNEL_ZONE_START+(unsigned long)&(proc[0]->tss));
	/* 装载进程0的ldt和tss选择符,第一次需我们来加载 */
	lldt(_LDT(0));
	ltr(_TSS(0));
	/* 挂载时钟中断处理程序 */
	set_idt(INT_R0, timer_int, NR_TIMER_INT);
	/* 设置8253定时器芯片 */
	out_b(0x43, 0x36);
	out_b(0x40, (LATCH & 0xff));
	out_b(0x40, ((LATCH>>8) & 0xff));
	/* 挂载系统调用处理程序 */
示例#6
0
/* 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");
			
			BOOT_BLOCK_PTR = (uint32_t *)mod->mod_start;
			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    = 0;
		the_ldt_desc.opsize         = 1;
		the_ldt_desc.reserved       = 0;
		the_ldt_desc.avail          = 0;
		the_ldt_desc.present        = 1;
		the_ldt_desc.dpl            = 0x0;
		the_ldt_desc.sys            = 0;
		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    = 0;
		the_tss_desc.opsize         = 0;
		the_tss_desc.reserved       = 0;
		the_tss_desc.avail          = 0;
		the_tss_desc.seg_lim_19_16  = TSS_SIZE & 0x000F0000;
		the_tss_desc.present        = 1;
		the_tss_desc.dpl            = 0x0;
		the_tss_desc.sys            = 0;
		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);
	}
	/* disable all interrupts on PIC */

	/* Init the PIC */
	i8259_init();
	
	init_idt();
	int i;
	for(i = 0; i < 16; i++)
	{
		disable_irq(i);
	}

	/* Initialize devices, memory, filesystem, enable device interrupts on the
	 * PIC, any other initialization stuff... */\
	paging_init();
	rtc_init();
	init_fs();
	enable_irq(PIC_1);
	terminal_open(0);
	clear();

	/* 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();
	//write(1, "abcdefghijklmnopqrstuvwxyz", 26);
	/*
	printf("testing terminal \n");*/

	/*
	unsigned char buf[4000] = "a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk\nl\nm\nn\no\np\nq\nr\ns\nt\nu\nv\nw\nx\ny\nz\n1\n2\n3\n4\n5\n6\n7\n8\n9\n0\n";
	write(1, buf, 4000);
	*/
	/*unsigned char buf2[4000] = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz\n";
	terminal_write(1, buf2, 4000);
	terminal_read(0, buf, 4000);
	terminal_write(1, buf, 4000);
	printf("done testing terminal \n");
	//clear(); // Clears the screen before test_interrupts
	//int x = 1/0;
	
	*/
	 
	// Tests for RTC
	/*
	for(i = 0; i < 1000000000; i++);
	rtc_read();
	int j = 1024;
	rtc_write(&j,4);
	for(i = 0; i < 1000000000; i++);
	rtc_read();
	*/


	/*testing open and read file*/
	
	/*uint8_t filename[] = "verylargetxtwithverylongname.tx";
	uint8_t buffer[3000];
	int res;
	res = filesystem_open(filename);
	if(res == -1)
		terminal_write((const uint8_t *)"not able to open the file", 200);
	else
	{
		filesystem_read(0, buffer, 3000);
			terminal_write(buffer, 3000);
    }*/
	
	
	/*testing read directory*/
	//dirread();
	
	/* Execute the first program (`shell') ... */

	 uint8_t filename[] = "shell";	
	 execute(filename);
	 /*execute(filename);
	 execute(filename);
	 execute(filename);
	 execute(filename);*/
	 

	/* Spin (nicely, so we don't chew up cycles) */
	asm volatile(".1: hlt; jmp .1;");
}
示例#7
0
/* 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;
	uint8_t* filesystem_address;

	/* 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) {
			filesystem_address = (uint8_t*)mod->mod_start;
			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    = 0;
		the_ldt_desc.opsize         = 1;
		the_ldt_desc.reserved       = 0;
		the_ldt_desc.avail          = 0;
		the_ldt_desc.present        = 1;
		the_ldt_desc.dpl            = 0x0;
		the_ldt_desc.sys            = 0;
		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    = 0;
		the_tss_desc.opsize         = 0;
		the_tss_desc.reserved       = 0;
		the_tss_desc.avail          = 0;
		the_tss_desc.seg_lim_19_16  = TSS_SIZE & 0x000F0000;
		the_tss_desc.present        = 1;
		the_tss_desc.dpl            = 0x0;
		the_tss_desc.sys            = 0;
		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);
	}
	

	int z = 0;
	reset_scr();

	//set the IDT	
	set_idt();
	lidt(idt_desc_ptr);
	
	/* Init the PIC */
	i8259_init();

	/* Initialize devices, memory, filesystem, enable device interrupts on the
	 * PIC, any other initialization stuff... */

	//init paging
	init_paging();
			
	//init filesystem
	init_filesys(filesystem_address);
	
	//init keyboard
	init_keyboard();

	//init the rtc
	init_rtc();

	//init the mouse
	init_mouse();

	//clear the screen
	reset_scr();
	
	// boot_screen();
	for(z = 0; z < 3; z++)
	{
		terminal_init();
	}
	node* buffer = screens[0];
	reset_buf(buffer);
	//display the status bar

	/* Enable interrupts */
	
	sti();
	/* 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 */


	boot_screen();
	//sample mario sound
	for(z = 0; z < 500000; z++)
		play_sound(1000);

	for(z = 0; z < 500000; z++)
		play_sound(100);

	for(z = 0; z < 500000; z++)
		play_sound(900);

	for(z = 0; z < 50000; z++)
		play_sound(200);

	for(z = 0; z < 500000; z++)
		play_sound(800);

	for(z = 0; z < 500000; z++)
		play_sound(300);

	for(z = 0; z < 500000; z++)
		play_sound(700);

	for(z = 0; z < 500000; z++)
		play_sound(400);

	for(z = 0; z < 500000; z++)
		play_sound(600);

	for(z = 0; z < 500000; z++)
		play_sound(500);

	for(z = 0; z < 500000; z++)
		play_sound(500);

	for(z = 0; z < 500000; z++)
		play_sound(1000);

	for(z = 0; z < 500000; z++)
		play_sound(400);

	for(z = 0; z < 500000; z++)
		play_sound(300);

	for(z = 0; z < 500000; z++)
		play_sound(300);

	for(z = 0; z < 500000; z++)
		play_sound(200);

	//boot_screen();
	nosound();
	reset_scr();

	//init PIT for sound, timer
	init_pit(0, 100);

	/////////////////////////////////////////////////////////////

//	void imperial();

	status_bar();
	/* Execute the first program (`shell') ... */
	uint8_t fname[33] = "shell";
	execute(fname);

	/* We should never get to this point */
	//printf("Initial shell halted.");

	/* Spin (nicely, so we don't chew up cycles) */
	asm volatile(".1: hlt; jmp .1;");
}
示例#8
0
/*
 * AP cpu's call this to sync up protected mode.
 *
 * WARNING! %gs is not set up on entry.  This routine sets up %gs.
 */
void
init_secondary(void)
{
	int	gsel_tss;
	int	x, myid = bootAP;
	u_int64_t msr, cr0;
	struct mdglobaldata *md;
	struct privatespace *ps;

	ps = &CPU_prvspace[myid];

	gdt_segs[GPROC0_SEL].ssd_base =
		(long) &ps->mdglobaldata.gd_common_tss;
	ps->mdglobaldata.mi.gd_prvspace = ps;

	/* We fill the 32-bit segment descriptors */
	for (x = 0; x < NGDT; x++) {
		if (x != GPROC0_SEL && x != (GPROC0_SEL + 1))
			ssdtosd(&gdt_segs[x], &gdt[myid * NGDT + x]);
	}
	/* And now a 64-bit one */
	ssdtosyssd(&gdt_segs[GPROC0_SEL],
	    (struct system_segment_descriptor *)&gdt[myid * NGDT + GPROC0_SEL]);

	r_gdt.rd_limit = NGDT * sizeof(gdt[0]) - 1;
	r_gdt.rd_base = (long) &gdt[myid * NGDT];
	lgdt(&r_gdt);			/* does magic intra-segment return */

	/* lgdt() destroys the GSBASE value, so we load GSBASE after lgdt() */
	wrmsr(MSR_FSBASE, 0);		/* User value */
	wrmsr(MSR_GSBASE, (u_int64_t)ps);
	wrmsr(MSR_KGSBASE, 0);		/* XXX User value while we're in the kernel */

	lidt(&r_idt_arr[mdcpu->mi.gd_cpuid]);

#if 0
	lldt(_default_ldt);
	mdcpu->gd_currentldt = _default_ldt;
#endif

	gsel_tss = GSEL(GPROC0_SEL, SEL_KPL);
	gdt[myid * NGDT + GPROC0_SEL].sd_type = SDT_SYSTSS;

	md = mdcpu;	/* loaded through %gs:0 (mdglobaldata.mi.gd_prvspace)*/

	md->gd_common_tss.tss_rsp0 = 0;	/* not used until after switch */
#if 0 /* JG XXX */
	md->gd_common_tss.tss_ioopt = (sizeof md->gd_common_tss) << 16;
#endif
	md->gd_tss_gdt = &gdt[myid * NGDT + GPROC0_SEL];
	md->gd_common_tssd = *md->gd_tss_gdt;

	/* double fault stack */
	md->gd_common_tss.tss_ist1 =
		(long)&md->mi.gd_prvspace->idlestack[
			sizeof(md->mi.gd_prvspace->idlestack)];

	ltr(gsel_tss);

	/*
	 * Set to a known state:
	 * Set by mpboot.s: CR0_PG, CR0_PE
	 * Set by cpu_setregs: CR0_NE, CR0_MP, CR0_TS, CR0_WP, CR0_AM
	 */
	cr0 = rcr0();
	cr0 &= ~(CR0_CD | CR0_NW | CR0_EM);
	load_cr0(cr0);

	/* Set up the fast syscall stuff */
	msr = rdmsr(MSR_EFER) | EFER_SCE;
	wrmsr(MSR_EFER, msr);
	wrmsr(MSR_LSTAR, (u_int64_t)IDTVEC(fast_syscall));
	wrmsr(MSR_CSTAR, (u_int64_t)IDTVEC(fast_syscall32));
	msr = ((u_int64_t)GSEL(GCODE_SEL, SEL_KPL) << 32) |
	      ((u_int64_t)GSEL(GUCODE32_SEL, SEL_UPL) << 48);
	wrmsr(MSR_STAR, msr);
	wrmsr(MSR_SF_MASK, PSL_NT|PSL_T|PSL_I|PSL_C|PSL_D|PSL_IOPL);

	pmap_set_opt();		/* PSE/4MB pages, etc */
	pmap_init_pat();	/* Page Attribute Table */

	/* set up CPU registers and state */
	cpu_setregs();

	/* set up SSE/NX registers */
	initializecpu(myid);

	/* set up FPU state on the AP */
	npxinit(__INITIAL_FPUCW__);

	/* disable the APIC, just to be SURE */
	lapic->svr &= ~APIC_SVR_ENABLE;
}
示例#9
0
/*
 * The CPU ends up here when its ready to run
 * This is called from code in mptramp.s; at this point, we are running
 * in the idle pcb/idle stack of the new cpu.  When this function returns,
 * this processor will enter the idle loop and start looking for work.
 *
 * XXX should share some of this with init386 in machdep.c
 */
void
cpu_hatch(void *v)
{
	struct cpu_info *ci = (struct cpu_info *)v;
	int s;

	cpu_init_msrs(ci);

#ifdef DEBUG
	if (ci->ci_flags & CPUF_PRESENT)
		panic("%s: already running!?", ci->ci_dev->dv_xname);
#endif

	ci->ci_flags |= CPUF_PRESENT;

	lapic_enable();
	lapic_startclock();

	if ((ci->ci_flags & CPUF_IDENTIFIED) == 0) {
		/*
		 * We need to wait until we can identify, otherwise dmesg
		 * output will be messy.
		 */
		while ((ci->ci_flags & CPUF_IDENTIFY) == 0)
			delay(10);

		identifycpu(ci);

		/* Signal we're done */
		atomic_clearbits_int(&ci->ci_flags, CPUF_IDENTIFY);
		/* Prevent identifycpu() from running again */
		atomic_setbits_int(&ci->ci_flags, CPUF_IDENTIFIED);
	}

	while ((ci->ci_flags & CPUF_GO) == 0)
		delay(10);
#ifdef DEBUG
	if (ci->ci_flags & CPUF_RUNNING)
		panic("%s: already running!?", ci->ci_dev->dv_xname);
#endif

	lcr0(ci->ci_idle_pcb->pcb_cr0);
	cpu_init_idt();
	lapic_set_lvt();
	gdt_init_cpu(ci);
	fpuinit(ci);

	lldt(0);

	cpu_init(ci);

	s = splhigh();
	lcr8(0);
	enable_intr();

	microuptime(&ci->ci_schedstate.spc_runtime);
	splx(s);

	SCHED_LOCK(s);
	cpu_switchto(NULL, sched_chooseproc());
}
示例#10
0
/* Check if MAGIC is valid and print the Multiboot information structure
   pointed by ADDR. */
void
entry (unsigned long magic, unsigned long addr)
{
    /* Initialize the idt */
    init_idt();

    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;

        starting_address = mod->mod_start;

        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    = 0;
        the_ldt_desc.opsize         = 1;
        the_ldt_desc.reserved       = 0;
        the_ldt_desc.avail          = 0;
        the_ldt_desc.present        = 1;
        the_ldt_desc.dpl            = 0x0;
        the_ldt_desc.sys            = 0;
        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    = 0;
        the_tss_desc.opsize         = 0;
        the_tss_desc.reserved       = 0;
        the_tss_desc.avail          = 0;
        the_tss_desc.seg_lim_19_16  = TSS_SIZE & 0x000F0000;
        the_tss_desc.present        = 1;
        the_tss_desc.dpl            = 0x0;
        the_tss_desc.sys            = 0;
        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);
    }

    /* Initialize the PIC */
    i8259_init();

    /* Initialize devices, memory, filesystem, enable device interrupts on the
     * PIC, any other initialization stuff... */
    initialize_paging();

    /* initialize the RTC to 2Hz */
    rtc_initialize();

    /* initialize keyboard */
    initialize_keyboard();
    /* initialize function pointers */
    func_init();
    /* TESTING FILE SYSTEMS */
    init_file_systems(starting_address);

    init_terminals();
    //printf("Init File Systems Done\n");
    //test_file_systems((uint8_t*)"frame0.txt");
    //printf("Done testing\n");
    /* FINISH FILE SYSTEMS TESTING */


    /* 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();



    // testing for changing RTC freq

    /*int32_t * freq;
    int32_t temp = 1024;
    freq = &temp;
    rtc_write(freq, 4);
    int i;
    for(i = 0; i < 10; i ++) {
    	rtc_read();
    	printf("testing");
    }

    temp = 256;
    rtc_write(freq, 4);

    for (i = 0; i < 20; i++) {
    	rtc_read();
    	printf("\n");
    	printf("second_test");
    }*/






    /*while(1) {
    	asm volatile("int $0x28");
    }*/
    //int d = 6 / 0;

    /* Execute the first program (`shell') ... */
    clear();
    execute((uint8_t*)"shell");
    /* Spin (nicely, so we don't chew up cycles) */
    asm volatile(".1: hlt; jmp .1;");
}
示例#11
0
/* Check if MAGIC is valid and print the Multiboot information structure
   pointed by ADDR. */
void
entry (unsigned long magic, unsigned long addr)
{
	uint32_t fileptr;// start of file system
	multiboot_info_t *mbi;

	/* Initialize the screen. */
	terminal_open();

	/* 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;
		fileptr = mod->mod_start;
		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++;
		}
	}
	/* 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    = 0;
		the_ldt_desc.opsize         = 1;
		the_ldt_desc.reserved       = 0;
		the_ldt_desc.avail          = 0;
		the_ldt_desc.present        = 1;
		the_ldt_desc.dpl            = 0x0;
		the_ldt_desc.sys            = 0;
		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    = 0;
		the_tss_desc.opsize         = 0;
		the_tss_desc.reserved       = 0;
		the_tss_desc.avail          = 0;
		the_tss_desc.seg_lim_19_16  = TSS_SIZE & 0x000F0000;
		the_tss_desc.present        = 1;
		the_tss_desc.dpl            = 0x0;
		the_tss_desc.sys            = 0;
		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);
	}
	/* Mask all IRQs again*/
	int j;
	for(j=0;j<16;j++)
	{
		disable_irq(j);
	}
	/* Init the PIC */
	i8259_init();
	
	/* Init IDT vector 0-20*/
	{
		int i;
		for(i=0;i<20;i++)
		{	
			if(i!=15)
			{
				//Define an idt_desc_t structure
				idt_desc_t idt_desc;
				//populate w/ correct values
				idt_desc.seg_selector=0x0010;
				idt_desc.present=1;
				idt_desc.size=1;
				idt_desc.dpl=0x0;
				idt_desc.reserved1=1;
				idt_desc.reserved2=1;
				idt_desc.reserved3=1;
				SET_IDT_ENTRY(idt_desc, ehandlers[i]);
				//Set new entry in table
				idt[i]=idt_desc;
			}
		}
	}
	/* Init IRQ Interrupts*/
	//Timer Chip
	{
		//Define an idt_desc_t structure
		idt_desc_t idt_desc;
		//populate w/ correct values
		idt_desc.seg_selector=0x0010;
		idt_desc.present=1;
		idt_desc.size=1;
		idt_desc.dpl=0x0;
		idt_desc.reserved1=1;
		idt_desc.reserved2=1;
		SET_IDT_ENTRY(idt_desc, irqhandlers[0]);
		//Set new entry in table
		idt[32]=idt_desc;
		//Disable IRQ masking
		//enable_irq(0);
	}
	//Keyboard
	{
		//Define an idt_desc_t structure
		idt_desc_t idt_desc;
		//populate w/ correct values
		idt_desc.seg_selector=0x0010;
		idt_desc.present=1;
		idt_desc.size=1;
		idt_desc.dpl=0x0;
		idt_desc.reserved1=1;
		idt_desc.reserved2=1;
		SET_IDT_ENTRY(idt_desc, irqhandlers[1]);
		//Set new entry in table
		idt[33]=idt_desc;
		//Disable IRQ masking
		//enable_irq(1);
	}
	//Real Time Clock
	{
		//Define an idt_desc_t structure
		idt_desc_t idt_desc;
		//populate w/ correct values
		idt_desc.seg_selector=0x0010;
		idt_desc.present=1;
		idt_desc.size=1;
		idt_desc.dpl=0x0;
		idt_desc.reserved1=1;
		idt_desc.reserved2=1;
		SET_IDT_ENTRY(idt_desc, irqhandlers[2]);
		//Set new entry in table
		idt[40]=idt_desc;
	//Disable IRQ masking
		//enable_irq(8);
	}
	/* Init System Call 0x80 */
	//Define an idt_desc_t structure
	idt_desc_t idt_desc;
	//populate w/ correct values
	idt_desc.seg_selector=0x0010;
	idt_desc.present=1;
	idt_desc.size=1;
	idt_desc.dpl=0x0;
	idt_desc.reserved1=1;
	idt_desc.reserved2=1;
	SET_IDT_ENTRY(idt_desc, systemcall);
	//Set new entry in table
	idt[128]=idt_desc;
			
	//Load new IDT
	lidt(idt_desc_ptr);

	/* Initialize devices, memory, filesystem, enable device interrupts on the
	 * PIC, any other initialization stuff... */
	paging_init();
	
	//Enable IRQ interrupts. 
	enable_irq(8);
	enable_irq(2);
	enable_irq(1);
	
	/* 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();
	/* This will be replaced by a system call after CP3 */
	rtc_open();
	filesys_init(fileptr); // start of filesystem
	
	while(1)
	{
		
		printf("Reading-> ");
		uint8_t buf[1024];
		int cnt = terminal_read(buf, 1024);
		buf[cnt] = '\0';
		puts ((int8_t*)"Typed:    ");
		puts ((int8_t*)buf);
		putc('\n');
		
	}
	/* Execute the first program (`shell') ... */
	/* Spin (nicely, so we don't chew up cycles) */
	asm volatile(".1: hlt; jmp .1;");
}
示例#12
0
文件: kernel.c 项目: bpenda/zbiggie
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);

	void * zbigfs_location = NULL;
	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++;
		}
		if (mod_count){
			module_t* zbigfsmod = (module_t*)mbi->mods_addr;
			zbigfs_location = (void *) zbigfsmod->mod_start;
		} else {
			printf("No module 0, zbigfs will FAIL!");
		}
	}
	/* 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    = 0;
		the_ldt_desc.opsize         = 1;
		the_ldt_desc.reserved       = 0;
		the_ldt_desc.avail          = 0;
		the_ldt_desc.present        = 1;
		the_ldt_desc.dpl            = 0x0;
		the_ldt_desc.sys            = 0;
		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    = 0;
		the_tss_desc.opsize         = 0;
		the_tss_desc.reserved       = 0;
		the_tss_desc.avail          = 0;
		the_tss_desc.seg_lim_19_16  = TSS_SIZE & 0x000F0000;
		the_tss_desc.present        = 1;
		the_tss_desc.dpl            = 0x0;
		the_tss_desc.sys            = 0;
		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; //8MB
		ltr(KERNEL_TSS);
	}
	//printf("Enabling Interrupts\n");


	
	/* Init the PIC */

	populate_idt();

	i8259_init();
	rtc_init();
	outb(PIT_MODIFY_LOW | PIT_MODIFY_HIGH | PIT_MODE_2 ,PIT_CTRL_ADDR);
	outb(PIT_DEFAULT_RATE_LOW,PIT_PORT0_CTRL_ADDR); //set low byte
	outb(PIT_DEFAULT_RATE_HIGH,PIT_PORT0_CTRL_ADDR); //set high byte

/*
 * pc speaker code from osdev
 *
	uint32_t Div;
	uint8_t tmp;
		 
	//Set the PIT to the desired frequency
	Div = 1193180 / 1000;
	outb(0x43, 0xb6);
	outb(0x42, (uint8_t) (Div) );
	outb(0x42, (uint8_t) (Div >> 8));
							 
	 //And play the sound using the PC speaker
	tmp = inb(0x61);
	if (tmp != (tmp | 3)) {
		outb(0x61, tmp | 3);
	}
*/
	disable_irq(0);



	/* 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 */


	//Enable interrupts for the first time
	printf("Enabling Interrupts\n");
	sti();

	//Initialize paging and mount the filesysem
	init_paging();
	printf("Mounting module 0 as read-only zbigfs filesystem\n");
	zbigfs_mount(zbigfs_location);


	//Initialize PCBs and then clear the terminal so it looks nice
	init_pcbs();
	term_clear();

	/* Spin (nicely, so we don't chew up cycles) */
	asm volatile(".1: hlt; jmp .1;");
}
示例#13
0
/* 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();
	
	uint32_t filestart;
	/* 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);
			filestart = 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++;
		}
	}
	/* 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    = 0;
		the_ldt_desc.opsize         = 1;
		the_ldt_desc.reserved       = 0;
		the_ldt_desc.avail          = 0;
		the_ldt_desc.present        = 1;
		the_ldt_desc.dpl            = 0x0;
		the_ldt_desc.sys            = 0;
		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    = 0;
		the_tss_desc.opsize         = 0;
		the_tss_desc.reserved       = 0;
		the_tss_desc.avail          = 0;
		the_tss_desc.seg_lim_19_16  = TSS_SIZE & 0x000F0000;
		the_tss_desc.present        = 1;
		the_tss_desc.dpl            = 0x0;
		the_tss_desc.sys            = 0;
		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);
	}
		//Initialization of IDE
	{
		printf("Initilization of Idt table...");
		set_trap_gate(0,divide_error);
		set_trap_gate(1,debug);
		set_intr_gate(2,nmi);
		set_system_intr_gate(3,int3);	
		set_system_gate(4,overflow);
		set_system_gate(5,bounds);
		set_trap_gate(6,invalid_op);
		set_trap_gate(7,device_not_available);
		set_task_gate(8,31);	
		set_trap_gate(9,coprocessor_segment_overrun);
		set_trap_gate(10,invalid_TSS);
		set_trap_gate(11,segment_not_present);
		set_trap_gate(12,stack_segment);
		set_trap_gate(13,general_protection);
		set_trap_gate(14,page_fault);//intr
		set_trap_gate(16,coprocessor_error);
		set_trap_gate(17,alignment_check);
		set_trap_gate(18,machine_check);
		set_trap_gate(19,simd_coprocessor_error);
		set_system_gate(128,system_call);
		set_intr_gate(32,irq0);  //intr 
		set_intr_gate(33,irq1); //intr
		set_intr_gate(34,0); //intr
		set_intr_gate(40,irq8);  //intr   
		//set_intr_gate(44,irq12); //intr
		lidt(idt_desc_ptr);
		printf("ok!\n");
	}
	
	// Init the PIC 
	i8259_init();
	
	//Enable interrupts

 
	enable_irq(1);
	enable_irq(2);
	open_rtc();
    enable_irq(8);
	enable_irq(12);

    uint8_t file =0x00;
	
	//Enable paging
    printf("Enabling paging...\n");
	paging();
    printf("ok!\n");
	
	//Restore interrupts
	sti();

	//Mounting file system
	printf("Mounting filesystem...\n");
	open_terminal(&file);
	setstart(filestart);
	printf("ok!\n");
	
	//clear();
    //Executing first program shell
    uint8_t cmd[10]={"shell "};
#if debug_by_showing_dentries
test_dentries();
while(1);
#endif

    test_system_call((int32_t)cmd, NULL, 0, 2);

	asm volatile(".1: hlt; jmp .1;");
}
示例#14
0
文件: kernel.c 项目: zshandy/ECE391
/* Check if MAGIC is valid and print the Multiboot information structure
   pointed by ADDR. */
void
entry (unsigned long magic, unsigned long addr)
{
	uint32_t file_system_start;
	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;

		file_system_start = mod->mod_start;  // store mod_start as start address of file system
		
		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    = 0;
		the_ldt_desc.opsize         = 1;
		the_ldt_desc.reserved       = 0;
		the_ldt_desc.avail          = 0;
		the_ldt_desc.present        = 1;
		the_ldt_desc.dpl            = 0x0;
		the_ldt_desc.sys            = 0;
		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    = 0;
		the_tss_desc.opsize         = 0;
		the_tss_desc.reserved       = 0;
		the_tss_desc.avail          = 0;
		the_tss_desc.seg_lim_19_16  = TSS_SIZE & 0x000F0000;
		the_tss_desc.present        = 1;
		the_tss_desc.dpl            = 0x0;
		the_tss_desc.sys            = 0;
		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 - 4;
		ltr(KERNEL_TSS);
	}

///////////////////////////////

	/* Initialize first 20 IDT entries for exceptions */
	/* Initialize interrupt descriptor entries defined in x86_desc.h*/
	int i;
	for (i=0; i<EXCEPTION_NUM; i++)
	{

		idt[i].seg_selector = KERNEL_CS;    // set up elements in the exception entry
		idt[i].reserved4 = 0;			
		idt[i].reserved3 = 0;		
		idt[i].reserved2 = 1;
		idt[i].reserved1 = 1;
		idt[i].size = 1;
		idt[i].reserved0 = 0;
		idt[i].dpl = 0;          
		idt[i].present = 1;

		SET_IDT_ENTRY(idt[i], reserved);
	}

	// /* set 20 idt entries for exceptions */
    init_exception();

	// set idt entry for RTC
	{ 
		idt[RTC_ENTRY].seg_selector = KERNEL_CS;   // set up elements in the RTC entry
		idt[RTC_ENTRY].reserved4 = 0;			
		idt[RTC_ENTRY].reserved3 = 0;		
		idt[RTC_ENTRY].reserved2 = 1;
		idt[RTC_ENTRY].reserved1 = 1;
		idt[RTC_ENTRY].size = 1;
		idt[RTC_ENTRY].reserved0 = 0;
		idt[RTC_ENTRY].dpl = 0;          
		idt[RTC_ENTRY].present = 1;

		SET_IDT_ENTRY(idt[RTC_ENTRY], rtc_wrapper);   // set idt entry for RTC
	}

	// set idt entry for keyboard
	{
		idt[KEYBRD_ENTRY].seg_selector = KERNEL_CS;   // set up elements in the keyboard entry
		idt[KEYBRD_ENTRY].reserved4 = 0;			
		idt[KEYBRD_ENTRY].reserved3 = 0;		
		idt[KEYBRD_ENTRY].reserved2 = 1;
		idt[KEYBRD_ENTRY].reserved1 = 1;
		idt[KEYBRD_ENTRY].size = 1;
		idt[KEYBRD_ENTRY].reserved0 = 0;
		idt[KEYBRD_ENTRY].dpl = 0;          
		idt[KEYBRD_ENTRY].present = 1;

		SET_IDT_ENTRY(idt[KEYBRD_ENTRY], keybrd_wrapper);   // set idt entry for keyboard

	} 

	//set idt entry for system call
	{
		idt[SYSCALL_ENTRY].seg_selector = KERNEL_CS;		
		idt[SYSCALL_ENTRY].reserved4 = 0;
		idt[SYSCALL_ENTRY].reserved3 = 0;
		idt[SYSCALL_ENTRY].reserved2 = 1;		
		idt[SYSCALL_ENTRY].reserved1 = 1;
		idt[SYSCALL_ENTRY].size = 1;
		idt[SYSCALL_ENTRY].reserved0 = 0;
		idt[SYSCALL_ENTRY].dpl = 3;
		idt[SYSCALL_ENTRY].present = 1;
		SET_IDT_ENTRY(idt[SYSCALL_ENTRY], syscall_wrapper);
	}
	
	{
		idt[MOUSE_VECTOR].seg_selector = KERNEL_CS;		
		idt[MOUSE_VECTOR].reserved4 = 0;
		idt[MOUSE_VECTOR].reserved3 = 0;
		idt[MOUSE_VECTOR].reserved2 = 1;		
		idt[MOUSE_VECTOR].reserved1 = 1;
		idt[MOUSE_VECTOR].size = 1;
		idt[MOUSE_VECTOR].reserved0 = 0;
		idt[MOUSE_VECTOR].dpl = 0;
		idt[MOUSE_VECTOR].present = 1;
		SET_IDT_ENTRY(idt[MOUSE_VECTOR], mouse_linkage);      
	}

	// set idt entry for pit
	{
		idt[PIT_ENTRY].seg_selector = KERNEL_CS;		
		idt[PIT_ENTRY].reserved4 = 0;
		idt[PIT_ENTRY].reserved3 = 0;
		idt[PIT_ENTRY].reserved2 = 1;		
		idt[PIT_ENTRY].reserved1 = 1;
		idt[PIT_ENTRY].size = 1;
		idt[PIT_ENTRY].reserved0 = 0;
		idt[PIT_ENTRY].dpl = 3;
		idt[PIT_ENTRY].present = 1;
		SET_IDT_ENTRY(idt[PIT_ENTRY], pit_wrapper);
	}




/////////////////////////////////

	// keyboard_handler(); 

	 //	test divide_error
	
	 // i=1/0;  

	// initialize devices
	// enable associated interrupts on PIC
	
	/* devices initialization ends*/	

	/* 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 */

	 i8259_init();   /* Init the PIC */ 

     // printf("Enabling Interrupts\n");


	 paging_init();    // set up paging

	 //testing purpose of paging
	 // uint8_t * test_ptr = 0x000B8000;
	 // * test_ptr = 1;
	 // // uint8_t * test_ptr = 0xB6000;
	 // *test_ptr = 1;

	 // printf("paging works!!\n");

	 // clear();   
	 sche_init();
	 pit_init();
	 rtc_init();     // init rtc  
	 keybrd_init();
	 init_mouse();

	 fs_init(file_system_start);   // init file system, test case: test_fs_init() is commented in the fs_init function
	 //sche_init();
	 for(i = 0; i < 30; i++){
		pid_status[i] = 0;
	 }
	 pid = -1;




   // enable interrupts on processor
	 /*****test cases*****/

	 // uint8_t* filename;
	 /*test rtc open*/
	 // rtc_open(filename);

	 /*test rtc write*/
	 // int32_t fd;
	 // uint8_t* buf;
	 // rtc_write(fd, buf, 2);

	 // // /*test rtc read*/ 
	 // while(1){
	 // 	rtc_read(fd, buf, 16);
	 // 	printf("rtc_read\n");
	 // }


	  //test_dir_read();
	  // read_test_text();  // read file depends on the READ_TEXT and READ_TXT flag on the top of file_system_driver.c 
	 //read_test_exe();
	 /*****test cases*****/


	//puts("here",1);
	 enable_irq(8);  //enable rtc interrupt 
	 enable_irq(1);  //enable keyboard interrupt
	 enable_irq(0);
	 sti();

	 //while(1){
		/*
		printf("here\n");
		dentry_t tmp_dentry;
		uint32_t tmp_eip, user_esp;
		uint8_t buf[4];
		read_dentry_by_name((uint8_t*)"testprint", &tmp_dentry);
		read_data(tmp_dentry.inode_num, 0, (uint8_t*)(0x08048000), 0x400000);	
		read_data(tmp_dentry.inode_num, 24, buf, 4);
		printf("here2\n");
		tmp_eip = 0x080481a4;	
		user_esp = 0x8000000 + 0x400000 - 4; //user space stack = 132MB - 4B
		
		tss.esp0 =0x800000-4;	//kernel stack = 128MB - 4B
		tss.ss0 = KERNEL_DS;
		asm volatile ("							\n\
				 cli 						\n\
				 movw %0, %%ax				\n\
				 movw %%ax, %%ds 			\n\
    			 pushl %0 					\n\
    			 pushl %1 					\n\
				 pushfl						\n\
				 popl %%eax 				\n\
    			 orl %2, %%eax 				\n\
    			 pushl %%eax 				\n\
    			 pushl %3 					\n\
    			 pushl %4 					\n\
				 iret 						\n\
    			 "
    			 :
    			 :"i"(USER_DS), "r"(user_esp), "r"(0x200), "i"(USER_CS), "r"(tmp_eip)
    			 :"eax", "memory"
    			 );
		*/
	 //}
	execute((uint8_t *) "shell");
	while(1){
		
	}


	/* Spin (nicely, so we don't chew up cycles) */


	// uint8_t test_read_buffer[TEST_READ_BUFFER_SIZE];										//initialize buffer for testing terminal_read
	// uint8_t test_write_buffer[TEST_WRITE_BUFFER_SIZE] = "teststringforwritefunction\n";     //initialize buffer for testing terminal_write
	// while(1){
	// 	memset(test_read_buffer, 0, TEST_READ_BUFFER_SIZE);
	// 	printf("Number of bytes read: %d\n", terminal_read(0, test_read_buffer, TEST_READ_BUFFER_SIZE-1));		//testing terminal_read
	// 	terminal_write(1, test_read_buffer, TEST_READ_BUFFER_SIZE);										//testing terminal _write
	// 	terminal_write(1, test_write_buffer, TEST_WRITE_BUFFER_SIZE);			
	// }

	asm volatile(".1: hlt; jmp .1;");
}
示例#15
0
/* 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    = 0;
		the_ldt_desc.opsize         = 1;
		the_ldt_desc.reserved       = 0;
		the_ldt_desc.avail          = 0;
		the_ldt_desc.present        = 1;
		the_ldt_desc.dpl            = 0x0;
		the_ldt_desc.sys            = 0;
		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    = 0;
		the_tss_desc.opsize         = 0;
		the_tss_desc.reserved       = 0;
		the_tss_desc.avail          = 0;
		the_tss_desc.seg_lim_19_16  = TSS_SIZE & 0x000F0000;
		the_tss_desc.present        = 1;
		the_tss_desc.dpl            = 0x0;
		the_tss_desc.sys            = 0;
		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);
	}

	fill_idt();
	lidt(idt_desc_ptr);	// Load IDT Pointer
	//Init the PIC
	i8259_init();
	pit_init();
	init_keyboard();
	rtc_init();
	terminal_open((uint8_t*)1);
	init_paging();


	/* initializing file systems */
	module_t * boot_fs = (module_t*)mbi->mods_addr;
	init_fs(boot_fs->mod_start);
	/* Done initializing fs */


	/* Initialize devices, memory, filesystem, enable device interrupts on the
	 * PIC, any other initialization stuff... */

	/* Enable interrupts */
	// printf("Enabling Interrupts\n");
	sti();

	// uint32_t bmap_val = 10;
	// char bitmap_temp[6];
    // itoa(bmap_val , bitmap_temp, 2);
    // // printf("BMAP VALUE - %d\n", bmap_val);
	// printf("%s\n", bitmap_temp);
    // char bitmap[6];
    // uint32_t l = strlen((uint8_t*)bitmap_temp);
    // uint32_t temp_idx;
    // for (temp_idx=0; temp_idx<5; temp_idx++){
    //     if (temp_idx<(5-l))
    //         bitmap[temp_idx] = '0';
    //     else
    //         bitmap[temp_idx] = bitmap_temp[temp_idx+l-5];
    // }
    // bitmap[5] = '\0';
    // printf("%s\n", bitmap);

	// Initialization for the process
	init_terminals();
	execute_func((uint8_t *)"shell");


	// Spin (nicely, so we don't chew up cycles)
	asm volatile(".1: hlt; jmp .1;");
}
示例#16
0
int
i386_set_ldt(struct proc *p, void *args, register_t *retval)
{
    int error, i, n;
    struct pcb *pcb = &p->p_addr->u_pcb;
    pmap_t pmap = p->p_vmspace->vm_map.pmap;
    struct i386_set_ldt_args ua;
    union descriptor *descv;
    size_t old_len, new_len, ldt_len;
    union descriptor *old_ldt, *new_ldt;

    if (user_ldt_enable == 0)
        return (ENOSYS);

    if ((error = copyin(args, &ua, sizeof(ua))) != 0)
        return (error);

    if (ua.start < 0 || ua.num < 0 || ua.start > 8192 || ua.num > 8192 ||
            ua.start + ua.num > 8192)
        return (EINVAL);

    descv = malloc(sizeof (*descv) * ua.num, M_TEMP, M_NOWAIT);
    if (descv == NULL)
        return (ENOMEM);

    if ((error = copyin(ua.desc, descv, sizeof (*descv) * ua.num)) != 0)
        goto out;

    /* Check descriptors for access violations. */
    for (i = 0; i < ua.num; i++) {
        union descriptor *desc = &descv[i];

        switch (desc->sd.sd_type) {
        case SDT_SYSNULL:
            desc->sd.sd_p = 0;
            break;
        case SDT_SYS286CGT:
        case SDT_SYS386CGT:
            /*
             * Only allow call gates targeting a segment
             * in the LDT or a user segment in the fixed
             * part of the gdt.  Segments in the LDT are
             * constrained (below) to be user segments.
             */
            if (desc->gd.gd_p != 0 &&
                    !ISLDT(desc->gd.gd_selector) &&
                    ((IDXSEL(desc->gd.gd_selector) >= NGDT) ||
                     (gdt[IDXSEL(desc->gd.gd_selector)].sd.sd_dpl !=
                      SEL_UPL))) {
                error = EACCES;
                goto out;
            }
            break;
        case SDT_MEMEC:
        case SDT_MEMEAC:
        case SDT_MEMERC:
        case SDT_MEMERAC:
            /* Must be "present" if executable and conforming. */
            if (desc->sd.sd_p == 0) {
                error = EACCES;
                goto out;
            }
            break;
        case SDT_MEMRO:
        case SDT_MEMROA:
        case SDT_MEMRW:
        case SDT_MEMRWA:
        case SDT_MEMROD:
        case SDT_MEMRODA:
        case SDT_MEMRWD:
        case SDT_MEMRWDA:
        case SDT_MEME:
        case SDT_MEMEA:
        case SDT_MEMER:
        case SDT_MEMERA:
            break;
        default:
            /*
             * Make sure that unknown descriptor types are
             * not marked present.
             */
            if (desc->sd.sd_p != 0) {
                error = EACCES;
                goto out;
            }
            break;
        }

        if (desc->sd.sd_p != 0) {
            /* Only user (ring-3) descriptors may be present. */
            if (desc->sd.sd_dpl != SEL_UPL) {
                error = EACCES;
                goto out;
            }
        }
    }

    /* allocate user ldt */
    simple_lock(&pmap->pm_lock);
    if (pmap->pm_ldt == 0 || (ua.start + ua.num) > pmap->pm_ldt_len) {
        if (pmap->pm_flags & PMF_USER_LDT)
            ldt_len = pmap->pm_ldt_len;
        else
            ldt_len = 512;
        while ((ua.start + ua.num) > ldt_len)
            ldt_len *= 2;
        new_len = ldt_len * sizeof(union descriptor);

        simple_unlock(&pmap->pm_lock);
        new_ldt = km_alloc(round_page(new_len), &kv_any,
                           &kp_dirty, &kd_nowait);
        if (new_ldt == NULL) {
            error = ENOMEM;
            goto out;
        }
        simple_lock(&pmap->pm_lock);

        if (pmap->pm_ldt != NULL && ldt_len <= pmap->pm_ldt_len) {
            /*
             * Another thread (re)allocated the LDT to
             * sufficient size while we were blocked in
             * km_alloc. Oh well. The new entries
             * will quite probably not be right, but
             * hey.. not our problem if user applications
             * have race conditions like that.
             */
            km_free(new_ldt, round_page(new_len), &kv_any,
                    &kp_dirty);
            goto copy;
        }

        old_ldt = pmap->pm_ldt;

        if (old_ldt != NULL) {
            old_len = pmap->pm_ldt_len * sizeof(union descriptor);
        } else {
            old_len = NLDT * sizeof(union descriptor);
            old_ldt = ldt;
        }

        memcpy(new_ldt, old_ldt, old_len);
        memset((caddr_t)new_ldt + old_len, 0, new_len - old_len);

        if (old_ldt != ldt)
            km_free(old_ldt, round_page(old_len),
                    &kv_any, &kp_dirty);

        pmap->pm_ldt = new_ldt;
        pmap->pm_ldt_len = ldt_len;

        if (pmap->pm_flags & PMF_USER_LDT)
            ldt_free(pmap);
        else
            pmap->pm_flags |= PMF_USER_LDT;
        ldt_alloc(pmap, new_ldt, new_len);
        pcb->pcb_ldt_sel = pmap->pm_ldt_sel;
        if (pcb == curpcb)
            lldt(pcb->pcb_ldt_sel);

    }
copy:
    /* Now actually replace the descriptors. */
    for (i = 0, n = ua.start; i < ua.num; i++, n++)
        pmap->pm_ldt[n] = descv[i];

    simple_unlock(&pmap->pm_lock);

    *retval = ua.start;

out:
    free(descv, M_TEMP);
    return (error);
}
示例#17
0
文件: kernel.c 项目: cpatchava/jarvis
/* 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    = 0;
		the_ldt_desc.opsize         = 1;
		the_ldt_desc.reserved       = 0;
		the_ldt_desc.avail          = 0;
		the_ldt_desc.present        = 1;
		the_ldt_desc.dpl            = 0x0;
		the_ldt_desc.sys            = 0;
		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    = 0;
		the_tss_desc.opsize         = 0;
		the_tss_desc.reserved       = 0;
		the_tss_desc.avail          = 0;
		the_tss_desc.seg_lim_19_16  = TSS_SIZE & 0x000F0000;
		the_tss_desc.present        = 1;
		the_tss_desc.dpl            = 0x0;
		the_tss_desc.sys            = 0;
		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);
	}

 /* Setup Trap Entries */                                                                       

  idt_desc_t idt_entry;       // setup idt_entry                                                 
  idt_entry.dpl       = 0;
  idt_entry.present     = 1;
  idt_entry.seg_selector    =KERNEL_CS;
  idt_entry.reserved3     =1; //40    
  idt_entry.reserved2     =1; //41   
  idt_entry.reserved1     =1; //42  
  idt_entry.size        =1; //43   
  idt_entry.reserved0     =0; //44   


  //0x00
  SET_IDT_ENTRY(idt_entry, divide_by_zero);
  idt[0x00] = idt_entry;
  
  /* Setup System Call Entry */
/* 
 idt_entry.dpl       = 3;
  idt_entry.present     = 1;
  idt_entry.seg_selector    =KERNEL_CS;
  idt_entry.reserved3     =1; //40
  idt_entry.reserved2     =1; //41
  idt_entry.reserved1     =1; //42
  idt_entry.size        =1; //43
  idt_entry.reserved0     =0; //44
  */
    
  /* Setup Interrupt Entries */
  idt_entry.dpl       = 0;
  idt_entry.present     = 1;
  idt_entry.seg_selector    =KERNEL_CS;
  idt_entry.reserved3     =0; //40
  idt_entry.reserved2     =1; //41
  idt_entry.reserved1     =1; //42
  idt_entry.size        =1; //43
  idt_entry.reserved0     =0; //44
  

	SET_IDT_ENTRY(idt_entry, divide_by_zero);
	idt[0x00] = idt_entry;	
	SET_IDT_ENTRY(idt_entry, debugger_interrupt);
	idt[0x01] = idt_entry;	
	SET_IDT_ENTRY(idt_entry, non_maskable_interrupt);
	idt[0x02] = idt_entry;	
	SET_IDT_ENTRY(idt_entry, breakpoint_interrupt);
	idt[0x03] = idt_entry;	
	SET_IDT_ENTRY(idt_entry, overflow_error);
	idt[0x04] = idt_entry;	
	SET_IDT_ENTRY(idt_entry, out_of_bounds_error);
	idt[0x05] = idt_entry;	
	SET_IDT_ENTRY(idt_entry, invalid_opcode_interrupt);
	idt[0x06] = idt_entry;	
	SET_IDT_ENTRY(idt_entry, coprocessor_not_avaliable);
	idt[0x07] = idt_entry;	
	SET_IDT_ENTRY(idt_entry, coprocessor_not_avaliable);
	idt[0x08] = idt_entry;	
	SET_IDT_ENTRY(idt_entry, coprocessor_segment_overrun);
	idt[0x09] = idt_entry;	
	SET_IDT_ENTRY(idt_entry, invalid_task_state_segment);
	idt[0x0A] = idt_entry;	
	SET_IDT_ENTRY(idt_entry, segment_not_present);
	idt[0x0B] = idt_entry;	
	SET_IDT_ENTRY(idt_entry, page_fault);
	idt[0x0C] = idt_entry;	
	SET_IDT_ENTRY(idt_entry, general_protection_fault);
	idt[0x0D] = idt_entry;
	SET_IDT_ENTRY(idt_entry, page_fault);
	idt[0x0E] = idt_entry;	
	SET_IDT_ENTRY(idt_entry, reserved_error);
	idt[0x0F] = idt_entry;	
	SET_IDT_ENTRY(idt_entry, reserved_error);
	idt[0x10] = idt_entry;	
	SET_IDT_ENTRY(idt_entry, reserved_error);
	idt[0x11] = idt_entry;	
	SET_IDT_ENTRY(idt_entry, reserved_error);
	idt[0x12] = idt_entry;	
  SET_IDT_ENTRY(idt_entry, reserved_error);
  idt[0x13] = idt_entry;
  SET_IDT_ENTRY(idt_entry, reserved_error);
  idt[0x14] = idt_entry;
  SET_IDT_ENTRY(idt_entry, reserved_error);
  idt[0x15] = idt_entry;
  SET_IDT_ENTRY(idt_entry, reserved_error);
  idt[0x16] = idt_entry;
  SET_IDT_ENTRY(idt_entry, reserved_error);
  idt[0x17] = idt_entry;
  SET_IDT_ENTRY(idt_entry, reserved_error);
  idt[0x18] = idt_entry;
  SET_IDT_ENTRY(idt_entry, reserved_error);
  idt[0x19] = idt_entry;
  SET_IDT_ENTRY(idt_entry, reserved_error);
  idt[0x1A] = idt_entry;
  SET_IDT_ENTRY(idt_entry, reserved_error);
  idt[0x1B] = idt_entry;
  SET_IDT_ENTRY(idt_entry, reserved_error);
  idt[0x1C] = idt_entry;
  SET_IDT_ENTRY(idt_entry, reserved_error);
  idt[0x1D] = idt_entry;
  SET_IDT_ENTRY(idt_entry, reserved_error);
  idt[0x1E] = idt_entry;
  SET_IDT_ENTRY(idt_entry, reserved_error);
  idt[0x1F] = idt_entry;

	/*Interrupt Entries setup*/

	clear();
	printf("Testing if its even coming here");

	SET_IDT_ENTRY(idt_entry, &_kb_wrapper);
	idt[0x21] = idt_entry;	
	//clear();	
	
	/* Init the PIC */
	cli();
	i8259_init();
	/* 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 */
	sti();
	printf("Enabling Interrupts");

	/* Execute the first program (`shell') ... */
	while(1);
	/* Spin (nicely, so we don't chew up cycles) */
	asm volatile(".1: hlt; jmp .1;");
}
示例#18
0
/* 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();

	paging_init();

	/* 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++;
		}
	}
	/* 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    = 0;
		the_ldt_desc.opsize         = 1;
		the_ldt_desc.reserved       = 0;
		the_ldt_desc.avail          = 0;
		the_ldt_desc.present        = 1;
		the_ldt_desc.dpl            = 0x0;
		the_ldt_desc.sys            = 0;
		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    = 0;
		the_tss_desc.opsize         = 0;
		the_tss_desc.reserved       = 0;
		the_tss_desc.avail          = 0;
		the_tss_desc.seg_lim_19_16  = TSS_SIZE & 0x000F0000;
		the_tss_desc.present        = 1;
		the_tss_desc.dpl            = 0x0;
		the_tss_desc.sys            = 0;
		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);
	}


	idt_desc_t interrupt;

	interrupt.seg_selector = KERNEL_CS;
	interrupt.dpl = 0;
	interrupt.size = 1;
	interrupt.reserved0 = 0;
	interrupt.reserved1 = 1;
	interrupt.reserved2 = 1;
	interrupt.reserved3 = 0;
	interrupt.reserved4 = 0;
	interrupt.present = 1;

	idt_desc_t divide_error_desc = interrupt;
	SET_IDT_ENTRY(divide_error_desc, divide_error);
	idt[0] = divide_error_desc;

	idt_desc_t reserved_desc = interrupt;
	SET_IDT_ENTRY(reserved_desc, reserved);
	idt[1] = reserved_desc;

	idt_desc_t nmi_interrupt_desc = interrupt;
	SET_IDT_ENTRY(nmi_interrupt_desc, nmi_interrupt);
	idt[2] = nmi_interrupt_desc;

	idt_desc_t breakpoint_desc = interrupt;
	SET_IDT_ENTRY(breakpoint_desc, breakpoint);
	idt[3] = breakpoint_desc;

	idt_desc_t overflow_desc = interrupt;
	SET_IDT_ENTRY(overflow_desc, overflow);
	idt[4] = overflow_desc;

	idt_desc_t bound_range_exceeded_desc = interrupt;
	SET_IDT_ENTRY(bound_range_exceeded_desc, bound_range_exceeded);
	idt[5] = bound_range_exceeded_desc;

	idt_desc_t invalid_opcode_desc = interrupt;
	SET_IDT_ENTRY(invalid_opcode_desc, invalid_opcode);
	idt[6] = invalid_opcode_desc;

	idt_desc_t device_not_available_desc = interrupt;
	SET_IDT_ENTRY(device_not_available_desc, device_not_available);
	idt[7] = device_not_available_desc;

	idt_desc_t double_fault_desc = interrupt;
	SET_IDT_ENTRY(double_fault_desc, double_fault);
	idt[8] = double_fault_desc;

	idt_desc_t coprocessor_segment_overrun_desc = interrupt;
	SET_IDT_ENTRY(coprocessor_segment_overrun_desc, coprocessor_segment_overrun);
	idt[9] = divide_error_desc;

	idt_desc_t invalid_tss_desc = interrupt;
	SET_IDT_ENTRY(invalid_tss_desc, invalid_tss);
	idt[10] = invalid_tss_desc;

	idt_desc_t segment_not_present_desc = interrupt;
	segment_not_present_desc.present = 0;
	SET_IDT_ENTRY(segment_not_present_desc, segment_not_present);
	idt[11] = segment_not_present_desc;

	idt_desc_t stack_segment_fault_desc = interrupt;
	SET_IDT_ENTRY(stack_segment_fault_desc, stack_segment_fault);
	idt[12] = stack_segment_fault_desc;

	idt_desc_t general_protection_desc = interrupt;
	SET_IDT_ENTRY(general_protection_desc, general_protection);
	idt[13] = general_protection_desc;

	idt_desc_t page_fault_desc = interrupt;
	SET_IDT_ENTRY(page_fault_desc, page_fault);
	idt[14] = page_fault_desc;

	idt_desc_t math_fault_desc = interrupt;
	SET_IDT_ENTRY(math_fault_desc, math_fault);
	idt[16] = math_fault_desc;

	idt_desc_t alignment_check_desc = interrupt;
	SET_IDT_ENTRY(alignment_check_desc, alignment_check);
	idt[17] = alignment_check_desc;

	idt_desc_t machine_check_desc = interrupt;
	SET_IDT_ENTRY(machine_check_desc, machine_check);
	idt[18] = machine_check_desc;

	idt_desc_t simd_desc = interrupt;
	SET_IDT_ENTRY(simd_desc, simd_floating_exception);
	idt[19] = simd_desc;

	idt_desc_t keyboard_desc = interrupt;
	SET_IDT_ENTRY(keyboard_desc, &keyboard_wrapper);
	idt[0x21] = keyboard_desc;

	idt_desc_t rtc_desc = interrupt;
	SET_IDT_ENTRY(rtc_desc, &rtc_wrapper);
	idt[0x28] = rtc_desc;

	// load idt
	lidt(idt_desc_ptr);

	/* Init the PIC */
	i8259_init();

	/* Initialize devices, memory, filesystem, enable device interrupts on the
	 * PIC, any other initialization stuff... */

	// initialize rtc
	rtc_init();

	// enable irq1 for keyboard
	 enable_irq(1);


	/* 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();

	int freq = 0x0010;
	write_rtc((char *) (&freq));

	// while(1) {
	// 	printf("h");
	// 	read_rtc();
	// }

	// int x = 3 / 0;
	// int * x = 0x12345000;
	// int y;
	// y = *x;

	/* Execute the first program (`shell') ... */

	/* Spin (nicely, so we don't chew up cycles) */
	asm volatile(".1: hlt; jmp .1;");
}
示例#19
0
文件: kernel.c 项目: ytang14/SimpleOS
/* 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;
	uint32_t filesys_start_addr;

	/* Clear the screen. */
	vga_text_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;
		filesys_start_addr = (uint32_t)mod->mod_start;
		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++;
		}
	}

	/* 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	= 0;
		the_ldt_desc.opsize			= 1;
		the_ldt_desc.reserved		= 0;
		the_ldt_desc.avail			= 0;
		the_ldt_desc.present		= 1;
		the_ldt_desc.dpl			= 0x0;
		the_ldt_desc.sys			= 0;
		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	= 0;
		the_tss_desc.opsize			= 0;
		the_tss_desc.reserved		= 0;
		the_tss_desc.avail			= 0;
		the_tss_desc.seg_lim_19_16	= TSS_SIZE & 0x000F0000;
		the_tss_desc.present		= 1;
		the_tss_desc.dpl			= 0x0;
		the_tss_desc.sys			= 0;
		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);
	}
	
	/* If kernel got to here, that means everything should be fine with the boot.
	 * Now we should initialize IDT, PIC, Syscall, paging, devices, filesystem,
	 * and any other initialization stuff... terminal_init() should be called at
	 * the very end of the initializations (after sti). */
	
	/* But first, let's clear the screen */
	vga_text_clear();
	
	/* Init the IDT */
	idt_init();

	/* Init the PIC */
	i8259_init();
	
	/* Init Syscall */
	syscall_init();
	
	/* Init Paging */
	paging_init();
	
	/* Init file system */
	filesys_init(filesys_start_addr);
	
	/* Init the Keyboard */
	keyboard_init();
	
	/* Init the RTC */
	rtc_init();

	/* Enable interrupts */
	sti();
	
	/* booting screen in mode X */
	start_up = 3;
	set_mode_x();
	load_bmp((uint8_t*)"boot", LOAD_FLAG_BOOT);
	while(start_up == 3);
	set_text_mode();
	
	/* Init the good looking terminal */
	terminal_init();
	
	/* wait for a key press to clear the start up screen */
	while(start_up == 2);

	vga_text_clear();
	welcome_and_credit();
	while(start_up == 1);
	
	/* Init the status bar for that terminal */
	terminal_stat_bar_init();
	terminal_clear();

	/* Init the PIT */
	pit_init();
	
	/* Execute the first program "shell" */
	pid_now = 1;
	pid_running[0] = 1;
	root_shell_flag = 1;
	execute((uint8_t*)"shell");

	/* Spin (nicely, so we don't chew up cycles) */
	asm volatile(".1: hlt; jmp .1;");
}
示例#20
0
int
amd64_set_ldt(struct proc *p, void *args, register_t *retval)
{
	int error, i, n;
	struct pcb *pcb = &p->p_addr->u_pcb;
	pmap_t pmap = p->p_vmspace->vm_map.pmap;
	struct amd64_set_ldt_args ua;
	union descriptor desc;

	if ((error = copyin(args, &ua, sizeof(ua))) != 0)
		return (error);

#ifdef	LDT_DEBUG
	printf("amd64_set_ldt: start=%d num=%d descs=%p\n", ua.start,
	    ua.num, ua.desc);
#endif

	if (ua.start < 0 || ua.num < 0)
		return (EINVAL);
	if (ua.start > 8192 || (ua.start + ua.num) > 8192)
		return (EINVAL);

	/*
	 * XXX LOCKING
	 */

	/* allocate user ldt */
	if (pmap->pm_ldt == 0 || (ua.start + ua.num) > pmap->pm_ldt_len) {
		size_t old_len, new_len;
		union descriptor *old_ldt, *new_ldt;

		if (pmap->pm_flags & PMF_USER_LDT) {
			old_len = pmap->pm_ldt_len * sizeof(union descriptor);
			old_ldt = pmap->pm_ldt;
		} else {
			old_len = NLDT * sizeof(union descriptor);
			old_ldt = ldt;
			pmap->pm_ldt_len = 512;
		}
		while ((ua.start + ua.num) > pmap->pm_ldt_len)
			pmap->pm_ldt_len *= 2;
		new_len = pmap->pm_ldt_len * sizeof(union descriptor);
		new_ldt = (union descriptor *)uvm_km_alloc(kernel_map, new_len);
		memcpy(new_ldt, old_ldt, old_len);
		memset((caddr_t)new_ldt + old_len, 0, new_len - old_len);
		pmap->pm_ldt = new_ldt;

		if (pmap->pm_flags & PCB_USER_LDT)
			ldt_free(pmap);
		else
			pmap->pm_flags |= PCB_USER_LDT;
		ldt_alloc(pmap, new_ldt, new_len);
		pcb->pcb_ldt_sel = pmap->pm_ldt_sel;
		if (pcb == curpcb)
			lldt(pcb->pcb_ldt_sel);

		/*
		 * XXX Need to notify other processors which may be
		 * XXX currently using this pmap that they need to
		 * XXX re-load the LDT.
		 */

		if (old_ldt != ldt)
			uvm_km_free(kernel_map, (vaddr_t)old_ldt, old_len);
#ifdef LDT_DEBUG
		printf("amd64_set_ldt(%d): new_ldt=%p\n", p->p_pid, new_ldt);
#endif
	}

	if (pcb == curpcb)
		savectx(curpcb);
	error = 0;

	/* Check descriptors for access violations. */
	for (i = 0, n = ua.start; i < ua.num; i++, n++) {
		if ((error = copyin(&ua.desc[i], &desc, sizeof(desc))) != 0)
			return (error);

		switch (desc.sd.sd_type) {
		case SDT_SYSNULL:
			desc.sd.sd_p = 0;
			break;
		case SDT_SYS286CGT:
		case SDT_SYS386CGT:
			/*
			 * Only allow call gates targeting a segment
			 * in the LDT or a user segment in the fixed
			 * part of the gdt.  Segments in the LDT are
			 * constrained (below) to be user segments.
			 */
			if (desc.gd.gd_p != 0 && !ISLDT(desc.gd.gd_selector) &&
			    ((IDXSEL(desc.gd.gd_selector) >= NGDT) ||
			     (gdt[IDXSEL(desc.gd.gd_selector)].sd.sd_dpl !=
				 SEL_UPL)))
				return (EACCES);
			break;
		case SDT_MEMEC:
		case SDT_MEMEAC:
		case SDT_MEMERC:
		case SDT_MEMERAC:
			/* Must be "present" if executable and conforming. */
			if (desc.sd.sd_p == 0)
				return (EACCES);
			break;
		case SDT_MEMRO:
		case SDT_MEMROA:
		case SDT_MEMRW:
		case SDT_MEMRWA:
		case SDT_MEMROD:
		case SDT_MEMRODA:
		case SDT_MEMRWD:
		case SDT_MEMRWDA:
		case SDT_MEME:
		case SDT_MEMEA:
		case SDT_MEMER:
		case SDT_MEMERA:
			break;
		default:
			/* Only care if it's present. */
			if (desc.sd.sd_p != 0)
				return (EACCES);
			break;
		}

		if (desc.sd.sd_p != 0) {
			/* Only user (ring-3) descriptors may be present. */
			if (desc.sd.sd_dpl != SEL_UPL)
				return (EACCES);
		}
	}

	/* Now actually replace the descriptors. */
	for (i = 0, n = ua.start; i < ua.num; i++, n++) {
		if ((error = copyin(&ua.desc[i], &desc, sizeof(desc))) != 0)
			goto out;

		pmap->pm_ldt[n] = desc;
	}

	*retval = ua.start;

out:
	return (error);
}
示例#21
0
/* 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++;
        }
    }
    /* 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    = 0;
        the_ldt_desc.opsize         = 1;
        the_ldt_desc.reserved       = 0;
        the_ldt_desc.avail          = 0;
        the_ldt_desc.present        = 1;
        the_ldt_desc.dpl            = 0x0;
        the_ldt_desc.sys            = 0;
        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    = 0;
        the_tss_desc.opsize         = 0;
        the_tss_desc.reserved       = 0;
        the_tss_desc.avail          = 0;
        the_tss_desc.seg_lim_19_16  = TSS_SIZE & 0x000F0000;
        the_tss_desc.present        = 1;
        the_tss_desc.dpl            = 0x0;
        the_tss_desc.sys            = 0;
        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);
    }

    /* Initialize IDT and fill its entries */
    init_idt();

    /* Init the PIC */
    i8259_init();

    /* Enable RTC interrupt */
    rtc_init();
    enable_irq(RTC_IRQ);


    /* Enbale Keyboard interrupt */
    enable_irq(KEYBOARD_IRQ);


    /*Enable PIT interrupt*/
    enable_irq(PIT_IRQ);

    /* Initialize devices, memory, filesystem, enable device interrupts on the
     * PIC, any other initialization stuff... */

    /* Initialize the Read-only File System */
    module_t* fs_mod = (module_t*)mbi->mods_addr;
    init_file_system(fs_mod->mod_start, fs_mod->mod_end);

    /* Initialize Paging */
    init_paging();

    /* 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();
    terminal_open();
    /* Test file_system driver */
    //test_file_system_driver();
    //Test for pit
    pit_init(0,2,20);
    /* Test the RTC driver */
    //rtc_test();
    while(1) {
        int8_t exec_cmd[15] = "shell";
        asm volatile("movl $2, %%eax; movl %0, %%ebx;int $0x80;"::"b"(exec_cmd));
        printf("ended exec!\n");
    }
    // asm volatile("movl $1, %%eax; movl %0, %%ebx;int $0x80;"::"b"(exec_cmd));

    /* Test terminal & keyboard driver */
    //test_terminal();


    /* Execute the first program (`shell') ... */
    //execlp("shell", NULL);

    /* Spin (nicely, so we don't chew up cycles) */
    asm volatile(".1: hlt; jmp .1;");
    //printf("after volatile\n");
}