Example #1
0
void
idt_init(void)
{
	extern struct Segdesc gdt[];
	
	// LAB 3: Your code here.
	assert(sizeof(struct Gatedesc) == SZ_GATEDESC);
	assert(sizeof(struct Pseudodesc) == SZ_PSEUDODESC);
	assert(sizeof(struct Trapframe) == SIZEOF_STRUCT_TRAPFRAME);
	clog("idt_pd = %u, %p", idt_pd.pd_lim, idt_pd.pd_base);
	//print_idt(idt);
	setup_idt();
	//print_idt(idt);

	// Setup a TSS so that we get the right stack
	// when we trap to the kernel.
	ts.ts_esp0 = KSTACKTOP;
	ts.ts_ss0 = GD_KD;

	// Initialize the TSS field of the gdt.
	gdt[GD_TSS >> 3] = SEG16(STS_T32A, (uint32_t) (&ts),
					sizeof(struct Taskstate), 0);
	gdt[GD_TSS >> 3].sd_s = 0;

	// Load the TSS
	ltr(GD_TSS);

	// Load the IDT
	asm volatile("lidt idt_pd");
}
Example #2
0
File: kernel.c Project: sklccwu/ff
void kernel(){
	setup_idt();
	clean_screen();
	print_mem_info();
	mm_init();
	init_8259A();
	*(int*)0xf0000000 = 0;	//cause pf interrupt
	//asm volatile("int $0xff");
	while(1);
}
Example #3
0
void kmain(struct multiboot* b, uint32_t magic)
{
	hide_cursor();
	cls();
	setup_gdt();
	setup_idt();
	if(b->mods_count == 1)
	{
		uint32_t mods_start_addr=*(uint32_t*)(b->mods_addr);
		uint32_t mods_end_addr=*(uint32_t*)(b->mods_addr + 4);
		if(((uint32_t)&__kernel_end - KERNEL_VMA) < mods_end_addr) kernel_end_addr=(mods_end_addr & 0xFFFFF000) + 0x1000;
	}
	setup_bitmap();
	setup_vmm();
	setup_pic();
	setup_tasking();
	setup_process_tree();
	set_timer_freq(100);

	pci_init();
	setup_network();

	b=(struct multiboot*)((uint8_t*)b+KERNEL_VMA);
	uint32_t mods_addr=*(uint32_t*)(b->mods_addr + KERNEL_VMA) + KERNEL_VMA;
	root_fs=ext2_fs_init((uint8_t*)mods_addr);
	struct inode *devfs=devfs_init();
	struct inode *devfs_root=vfs_search((struct inode*)root_fs, "/dev");
	if(devfs_root)
	{
		vfs_mount(devfs, devfs_root);
		register_tty_driver();
		register_kbd_driver();
		register_rtl8139_driver();
	}
	else kprintf("Could not mount /dev, no such directory\n");

	kprintf("\n%@Welcome to tapiOS!%@\n\n", 0x05, 0x07, b->mods_count, 0x03);

	struct inode *node=vfs_search((struct inode*)root_fs, "/bin/init");
	if(node)
	{
		struct file *init=vfs_open(node, NULL, O_RDONLY);
		uint8_t *init_mem=kmalloc(node->size);
		int read=vfs_read(init, init_mem, node->size);
		vaddr_t entrypoint=init_elf_get_entry_point(init_mem);
		setup_initial_process(entrypoint);
	}
	else kprintf("Init not found\n");
	__asm__ volatile("hltloop: hlt; jmp hltloop");

	PANIC();
}
Example #4
0
/**
 * @brief Función principal del kernel. Esta rutina recibe el control del
 * codigo en ensamblador de start.S.
 *
 * 	@param magic  Número mágico pasado por GRUB al código de start,S
 *  @param multiboot_info Apuntador a la estructura de información multiboot
 */
void cmain(unsigned int magic, void * multiboot_info) {
	unsigned int i;
	unsigned int allocations;

	char * addr;

	/* Referencia a la estructura de datos multiboot_header en start.S */
	extern multiboot_header_t multiboot_header;

	multiboot_info_location = (unsigned int) multiboot_info;

	cls();

	/* Configurar y cargar la GDT definida en pm.c*/
	setup_gdt();

	/* Configurar y cargar la IDT definida en idt.c */
	setup_idt();

	/* Configurar las excepciones definidas en IA-32 */
	setup_exceptions();

	/* Configurar las IRQ */
	setup_irq();

	/* Configurar el mapa de bits de memoria del kernel */
	setup_memory();

	printf("Kernel started\n");

	/* Probar la gestion de unidades de memoria */

	/* Reservar una unidad  */
	addr = allocate_unit();
	printf("Allocated address: %x = %d\n", addr, addr);

	addr = allocate_unit_region(0xFFFF);

	printf("Allocated region: %x\n", addr);

	addr = allocate_unit();

	printf("Allocated address: %x\n", addr);

	printf("Last allocated address: %x, %u\n",addr, addr);

	inline_assembly("sti");

	printf("Kernel finished\n");

}
Example #5
0
/*
 * Bootstrap-CPU start; we came from head.S
 */
void __no_return kernel_start(void)
{
	/* Before anything else, zero the bss section. As said by C99:
	 * “All objects with static storage duration shall be inited
	 * before program startup”, and that the implicit init is done
	 * with zero. Kernel assembly code also assumes a zeroed BSS
	 * space */
	clear_bss();

	/*
	 * Very-early setup: Do not call any code that will use
	 * printk(), `current', per-CPU vars, or a spin lock.
	 */

	setup_idt();

	schedulify_this_code_path(BOOTSTRAP);

	/*
	 * Memory Management init
	 */

	print_info();

	/* First, don't override the ramdisk area (if any) */
	ramdisk_init();

	/* Then discover our physical memory map .. */
	e820_init();

	/* and tokenize the available memory into allocatable pages */
	pagealloc_init();

	/* With the page allocator in place, git rid of our temporary
	 * early-boot page tables and setup dynamic permanent ones */
	vm_init();

	/* MM basics done, enable dynamic heap memory to kernel code
	 * early on .. */
	kmalloc_init();

	/*
	 * Secondary-CPUs startup
	 */

	/* Discover our secondary-CPUs and system IRQs layout before
	 * initializing the local APICs */
	mptables_init();

	/* Remap and mask the PIC; it's just a disturbance */
	serial_init();
	pic_init();

	/* Initialize the APICs (and map their MMIO regs) before enabling
	 * IRQs, and before firing other cores using Inter-CPU Interrupts */
	apic_init();
	ioapic_init();

	/* SMP infrastructure ready, fire the CPUs! */
	smpboot_init();

	keyboard_init();

	/* Startup finished, roll-in the scheduler! */
	sched_init();
	local_irq_enable();

	/*
	 * Second part of kernel initialization (Scheduler is now on!)
	 */

	ext2_init();

	// Signal the secondary cores to run their own test-cases code.
	// They've been waiting for us (thread 0) till all of kernel
	// subsystems has been properly initialized.  Wait No More!
	smpboot_trigger_secondary_cores_testcases();

	run_test_cases();
	halt();
}
Example #6
0
static void extract_e820(void)
{
	int id = fw_cfg_file_id("etc/e820");
	uint32_t size;
	int nr_map;
	int i;

	if (id == -1)
		panic();

	size = fw_cfg_file_size(id);
	nr_map = size / sizeof(e820->map[0]) + 4;
	fw_cfg_file_select(id);

	e820 = malloc(offsetof(struct e820map, map[nr_map]));
	e820->nr_map = nr_map;
	e820->map[0] = (struct e820entry)
		{ .addr = 0, .size = 639 * 1024, .type = E820_RAM }; /* low RAM */
	e820->map[1] = (struct e820entry)
		{ .addr = 639 * 1024, .size = 1024, .type = E820_RESERVED }; /* EBDA */
	e820->map[2] = (struct e820entry)
		{ .addr = 0xd0000, .size = 128 * 1024, .type = E820_NVS }; /* ACPI tables */
	e820->map[3] = (struct e820entry)
		{ .addr = 0xf0000, .size = 64 * 1024, .type = E820_RESERVED }; /* firmware */
	fw_cfg_read(&e820->map[4], size);
	for (i = 4; i < e820->nr_map; i++)
		if (e820->map[i].addr == 0) {
			lowmem = e820->map[i].size;
			e820->map[i].addr = 1024 * 1024;
			e820->map[i].size -= 1024 * 1024;
			break;
		}

	e820_seg = ((uintptr_t) e820) >> 4;
}

static bool detect_cbfs_and_boot(void)
{
	size_t sz;
	void *base = pflash_base(1, &sz);

	if (!base)
		return false;

	return boot_from_cbfs(base, sz);
}

int main(void)
{
	setup_hw();

	// Now go to the F-segment: we need to move away from flash area
	// in order to probe CBFS!
	asm("ljmp $0x8, $1f; 1:");

	setup_pci();
	setup_idt();
	fw_cfg_setup();
	extract_acpi();
	extract_e820();
	// extract_smbios();
	if (!detect_cbfs_and_boot())
		boot_from_fwcfg();
	panic();
}
Example #7
0
void mt_setup_gdt_idt(void)
{
	setup_gdt();
	setup_idt();
}
Example #8
0
File: main.cpp Project: orlv/fos
asmlinkage void init()
{
  init_memory();


  system->cli();

  out_banner();

  system->cpuid = new CPUID();

  system->gdt = new GDT;
  system->idt = new IDT;

  setup_idt();

  if(system->cpuid->features_edx & FEATURE_APIC && ENABLE_APIC) {
    printk("Using APIC\n");
    system->smp = new SMP;
    system->ic = new APIC(0);
    SysTimer = system->ic->getTimer();
//    system->sti();
  } else {
    printk("Using legacy ISA PIC and timer\n");
    system->ic = new PIC;
    SysTimer = new i8253;
    system->sti();
  }



  /*  TTY *tty1 = new TTY(80, 25);
  tty1->set_text_color(WHITE);
  stdout = tty1;*/

  printk("Memory size: %d Kb, free %dK (%dK high/%dK low)\n", system->pages_cnt*4, system->free_pages*4 + system->free_pages_DMA16*4, system->free_pages*4, system->free_pages_DMA16*4);
  extern multiboot_info_t *__mbi;
  initrb = new ModuleFS(__mbi);
  system->procman = new TProcMan;


  SysTimer->PeriodicalInt(1000, TimerCallSheduler);
  SysTimer->enable();

  printk("Kernel: start task switching\n");
 
  system->preempt.reset();  /* сбросим счетчик на 1 */
  system->preempt.enable();

  sched_yield();

  printk("-------------------------------------------------------------------------------\n" \
	 "All OK. Main kernel procedure done.\n"			\
	 "-------------------------------------------------------------------------------\n");

#if 0
  extern size_t volatile heap_free;
  i=0;
  while(1){
    i++;
    if(i == 0x60){
      printk("Memory size: %d Kb, free %dK (%dK high/%dK low), heap_free=%d \n", system->pages_cnt*4, system->free_pages.read()*4 + system->free_lowpages.read()*4, system->free_pages.read()*4, system->free_lowpages.read()*4, heap_free);
      i=0;
    }
    sched_yield();
  }
#else
  while(1) { printk(".");
    system->hlt(); }
#endif
}
Example #9
0
void setup_interrupts() {
	kdebug("setup_interrupts entry");
	kdebug("disable_interrupts");
	asm volatile ("	CLI");

	kdebug("calling configure_pics");
	configure_pics();

	kdebug("Clearing all of the handlers");
	kmemset((void *)idt, 0, 256 * 8);

	/*
	 * faults - IP on executing instruction
	 * 
	 * faults without error on stack : 0, 5, 6, 7, 19, 16
	 * faults with error on stack: 10, 11, 12, 13, 14, 17a
	 */
	kdebug("Adding handler for faults");
	add_handler(0, &fault_handler); add_handler(5, &fault_handler);
	add_handler(6, &fault_handler); add_handler(7, &fault_handler);
	add_handler(19, &fault_handler); add_handler(16, &fault_handler);

	add_handler(10, &fault_handler); add_handler(11, &fault_handler);
	add_handler(12, &fault_handler); add_handler(13, &fault_handler);
	add_handler(14, &fault_handler); add_handler(17, &fault_handler);
	
	/*
	 * traps - IP after executing instruction
	 *
	 * traps: 2, 3, 4
	 */
	kdebug("Adding handler for traps");
	add_handler(2, &trap_handler); add_handler(3, &trap_handler);
	add_handler(4, &trap_handler);

	/*
	 * aborts (with error): 8, 18
	 */
	kdebug("Adding handler for aborts");
	add_handler(8, &abort_handler); add_handler(18, &abort_handler);

	/*
	 * default interrupt handler for external interrupts
	 */
	int i;
	for (i=0; i<16; ++i) 
		shimmed_hwirq_handler_hooks[i] = 0;
	kdebug("Setting up default handler for external interrupts");
	extern void isr_shim_0(); extern void isr_shim_1(); extern void isr_shim_2();
	extern void isr_shim_3(); extern void isr_shim_4(); extern void isr_shim_5();
	extern void isr_shim_6(); extern void isr_shim_7(); extern void isr_shim_8();
	extern void isr_shim_9(); extern void isr_shim_10(); extern void isr_shim_11();
	extern void isr_shim_12(); extern void isr_shim_13(); extern void isr_shim_14();
	extern void isr_shim_15();
	add_handler(INTR_BASE + 0,  &isr_shim_0);
	add_handler(INTR_BASE + 1,  &isr_shim_1);
	add_handler(INTR_BASE + 2,  &isr_shim_2);
	add_handler(INTR_BASE + 3,  &isr_shim_3);
	add_handler(INTR_BASE + 4,  &isr_shim_4);
	add_handler(INTR_BASE + 5,  &isr_shim_5);
	add_handler(INTR_BASE + 6,  &isr_shim_6);
	add_handler(INTR_BASE + 7,  &isr_shim_7);
	add_handler(INTR_BASE + 8,  &isr_shim_8);
	add_handler(INTR_BASE + 9,  &isr_shim_9);
	add_handler(INTR_BASE + 10, &isr_shim_10);
	add_handler(INTR_BASE + 11, &isr_shim_11);
	add_handler(INTR_BASE + 12, &isr_shim_12);
	add_handler(INTR_BASE + 13, &isr_shim_13);
	add_handler(INTR_BASE + 14, &isr_shim_14);
	add_handler(INTR_BASE + 15, &isr_shim_15);

	kdebug("calling setup_idt");
	setup_idt();
	kdebug("enabling interrupts");
	asm volatile ("	STI");
}