2022-01-21 03:43:13 +00:00
|
|
|
|
//
|
|
|
|
|
|
// Created by longjin on 2022/1/20.
|
|
|
|
|
|
//
|
|
|
|
|
|
|
2022-01-23 15:17:52 +00:00
|
|
|
|
#include "common/glib.h"
|
|
|
|
|
|
#include "common/printk.h"
|
2022-02-10 05:45:38 +00:00
|
|
|
|
#include "common/kprint.h"
|
2022-01-25 10:04:18 +00:00
|
|
|
|
#include "exception/gate.h"
|
|
|
|
|
|
#include "exception/trap.h"
|
2022-01-29 04:52:25 +00:00
|
|
|
|
#include "exception/irq.h"
|
2022-04-08 12:04:12 +00:00
|
|
|
|
#include <exception/softirq.h>
|
2022-08-03 06:11:14 +00:00
|
|
|
|
#include <lib/libUI/screen_manager.h>
|
2022-08-03 09:13:46 +00:00
|
|
|
|
#include <lib/libUI/textui.h>
|
2022-01-27 06:58:14 +00:00
|
|
|
|
#include "mm/mm.h"
|
2022-02-28 08:15:44 +00:00
|
|
|
|
#include "mm/slab.h"
|
2022-02-10 05:45:38 +00:00
|
|
|
|
#include "process/process.h"
|
2022-02-16 06:07:53 +00:00
|
|
|
|
#include "syscall/syscall.h"
|
2022-04-05 06:40:26 +00:00
|
|
|
|
#include "smp/smp.h"
|
2022-04-07 12:21:28 +00:00
|
|
|
|
#include <smp/ipi.h>
|
2022-04-09 13:11:07 +00:00
|
|
|
|
#include <sched/sched.h>
|
2022-01-23 15:17:52 +00:00
|
|
|
|
|
2022-04-19 12:56:01 +00:00
|
|
|
|
#include <filesystem/fat32/fat32.h>
|
|
|
|
|
|
|
2022-03-12 16:31:46 +00:00
|
|
|
|
#include "driver/multiboot2/multiboot2.h"
|
|
|
|
|
|
#include "driver/acpi/acpi.h"
|
2022-03-18 11:18:36 +00:00
|
|
|
|
#include "driver/keyboard/ps2_keyboard.h"
|
|
|
|
|
|
#include "driver/mouse/ps2_mouse.h"
|
2022-03-21 09:13:15 +00:00
|
|
|
|
#include "driver/disk/ata.h"
|
2022-03-22 10:02:37 +00:00
|
|
|
|
#include "driver/pci/pci.h"
|
2022-07-17 09:24:27 +00:00
|
|
|
|
#include <driver/usb/usb.h>
|
2022-03-30 07:36:00 +00:00
|
|
|
|
#include "driver/disk/ahci/ahci.h"
|
2022-04-07 16:18:58 +00:00
|
|
|
|
#include <driver/timers/rtc/rtc.h>
|
2022-04-08 04:20:53 +00:00
|
|
|
|
#include <driver/timers/HPET/HPET.h>
|
2022-07-11 02:56:24 +00:00
|
|
|
|
#include <time/timer.h>
|
2022-04-15 07:23:17 +00:00
|
|
|
|
#include <driver/uart/uart.h>
|
2022-05-20 11:37:26 +00:00
|
|
|
|
#include <driver/video/video.h>
|
2022-03-12 16:31:46 +00:00
|
|
|
|
|
2022-07-11 13:47:28 +00:00
|
|
|
|
#include <driver/interrupt/apic/apic_timer.h>
|
2022-08-03 06:11:14 +00:00
|
|
|
|
|
2022-04-10 13:30:16 +00:00
|
|
|
|
ul bsp_idt_size, bsp_gdt_size;
|
2022-01-28 06:46:15 +00:00
|
|
|
|
|
2022-08-04 15:22:26 +00:00
|
|
|
|
#pragma GCC push_options
|
|
|
|
|
|
#pragma GCC optimize("O0")
|
2022-04-10 13:30:16 +00:00
|
|
|
|
struct gdtr gdtp;
|
|
|
|
|
|
struct idtr idtp;
|
|
|
|
|
|
void reload_gdt()
|
|
|
|
|
|
{
|
2022-04-15 07:23:17 +00:00
|
|
|
|
|
|
|
|
|
|
gdtp.size = bsp_gdt_size - 1;
|
2022-04-10 13:30:16 +00:00
|
|
|
|
gdtp.gdt_vaddr = (ul)phys_2_virt((ul)&GDT_Table);
|
|
|
|
|
|
|
|
|
|
|
|
asm volatile("lgdt (%0) \n\t" ::"r"(&gdtp)
|
|
|
|
|
|
: "memory");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void reload_idt()
|
|
|
|
|
|
{
|
2022-04-15 07:23:17 +00:00
|
|
|
|
|
|
|
|
|
|
idtp.size = bsp_idt_size - 1;
|
2022-04-10 13:30:16 +00:00
|
|
|
|
idtp.idt_vaddr = (ul)phys_2_virt((ul)&IDT_Table);
|
2022-04-15 07:23:17 +00:00
|
|
|
|
// kdebug("gdtvaddr=%#018lx", p.gdt_vaddr);
|
|
|
|
|
|
// kdebug("gdt size=%d", p.size);
|
2022-04-10 13:30:16 +00:00
|
|
|
|
|
|
|
|
|
|
asm volatile("lidt (%0) \n\t" ::"r"(&idtp)
|
|
|
|
|
|
: "memory");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-02-12 14:14:51 +00:00
|
|
|
|
// 初始化系统各模块
|
|
|
|
|
|
void system_initialize()
|
2022-01-23 15:17:52 +00:00
|
|
|
|
{
|
2022-02-22 12:45:32 +00:00
|
|
|
|
|
2022-08-03 06:11:14 +00:00
|
|
|
|
uart_init(COM1, 115200);
|
|
|
|
|
|
video_init();
|
|
|
|
|
|
|
2022-08-03 09:13:46 +00:00
|
|
|
|
scm_init();
|
|
|
|
|
|
textui_init();
|
2022-08-04 14:14:54 +00:00
|
|
|
|
kinfo("Kernel Starting...");
|
2022-08-03 09:13:46 +00:00
|
|
|
|
|
2022-04-10 13:30:16 +00:00
|
|
|
|
// 重新加载gdt和idt
|
|
|
|
|
|
ul tss_item_addr = (ul)phys_2_virt(0x7c00);
|
2022-04-15 07:23:17 +00:00
|
|
|
|
|
|
|
|
|
|
_stack_start = head_stack_start; // 保存init proc的栈基地址(由于之后取消了地址重映射,因此必须在这里重新保存)
|
2022-04-10 13:30:16 +00:00
|
|
|
|
kdebug("_stack_start=%#018lx", _stack_start);
|
2022-04-13 09:58:06 +00:00
|
|
|
|
|
2022-02-13 14:41:35 +00:00
|
|
|
|
load_TR(10); // 加载TR寄存器
|
2022-04-13 09:58:06 +00:00
|
|
|
|
set_tss64((uint *)&initial_tss[0], _stack_start, _stack_start, _stack_start, tss_item_addr,
|
2022-04-06 10:36:56 +00:00
|
|
|
|
tss_item_addr, tss_item_addr, tss_item_addr, tss_item_addr, tss_item_addr, tss_item_addr);
|
2022-02-22 12:45:32 +00:00
|
|
|
|
|
2022-04-05 06:40:26 +00:00
|
|
|
|
cpu_core_info[0].stack_start = _stack_start;
|
2022-04-13 09:58:06 +00:00
|
|
|
|
cpu_core_info[0].tss_vaddr = (uint64_t)&initial_tss[0];
|
2022-04-10 13:30:16 +00:00
|
|
|
|
kdebug("cpu_core_info[0].tss_vaddr=%#018lx", cpu_core_info[0].tss_vaddr);
|
|
|
|
|
|
kdebug("cpu_core_info[0].stack_start%#018lx", cpu_core_info[0].stack_start);
|
2022-04-15 07:23:17 +00:00
|
|
|
|
|
2022-01-25 10:04:18 +00:00
|
|
|
|
// 初始化中断描述符表
|
2022-02-22 14:17:27 +00:00
|
|
|
|
sys_vector_init();
|
2022-01-27 06:58:14 +00:00
|
|
|
|
|
2022-02-12 14:14:51 +00:00
|
|
|
|
// 初始化内存管理单元
|
2022-01-27 06:58:14 +00:00
|
|
|
|
mm_init();
|
2022-08-04 13:40:26 +00:00
|
|
|
|
|
2022-08-03 06:11:14 +00:00
|
|
|
|
// 内存管理单元初始化完毕后,需要立即重新初始化显示驱动。
|
|
|
|
|
|
// 原因是,系统启动初期,framebuffer被映射到48M地址处,
|
|
|
|
|
|
// mm初始化完毕后,若不重新初始化显示驱动,将会导致错误的数据写入内存,从而造成其他模块崩溃
|
2022-05-20 11:37:26 +00:00
|
|
|
|
// 对显示模块进行低级初始化,不启用double buffer
|
2022-08-03 09:13:46 +00:00
|
|
|
|
scm_reinit();
|
2022-08-03 06:11:14 +00:00
|
|
|
|
|
2022-05-20 11:37:26 +00:00
|
|
|
|
|
2022-04-13 09:58:06 +00:00
|
|
|
|
// =========== 重新设置initial_tss[0]的ist
|
2022-08-15 09:57:05 +00:00
|
|
|
|
uchar *ptr = (uchar *)kzalloc(STACK_SIZE, 0) + STACK_SIZE;
|
2022-04-15 07:23:17 +00:00
|
|
|
|
((struct process_control_block *)(ptr - STACK_SIZE))->cpu_id = 0;
|
2022-04-13 09:58:06 +00:00
|
|
|
|
|
|
|
|
|
|
initial_tss[0].ist1 = (ul)ptr;
|
|
|
|
|
|
initial_tss[0].ist2 = (ul)ptr;
|
|
|
|
|
|
initial_tss[0].ist3 = (ul)ptr;
|
|
|
|
|
|
initial_tss[0].ist4 = (ul)ptr;
|
|
|
|
|
|
initial_tss[0].ist5 = (ul)ptr;
|
|
|
|
|
|
initial_tss[0].ist6 = (ul)ptr;
|
|
|
|
|
|
initial_tss[0].ist7 = (ul)ptr;
|
|
|
|
|
|
// ===========================
|
2022-04-15 07:23:17 +00:00
|
|
|
|
|
2022-03-13 04:53:32 +00:00
|
|
|
|
acpi_init();
|
2022-04-08 13:26:42 +00:00
|
|
|
|
|
2022-01-29 04:52:25 +00:00
|
|
|
|
// 初始化中断模块
|
2022-04-10 13:30:16 +00:00
|
|
|
|
sched_init();
|
2022-02-22 14:17:27 +00:00
|
|
|
|
irq_init();
|
2022-04-10 13:30:16 +00:00
|
|
|
|
|
2022-04-08 12:04:12 +00:00
|
|
|
|
softirq_init();
|
2022-04-22 13:30:48 +00:00
|
|
|
|
current_pcb->cpu_id = 0;
|
|
|
|
|
|
current_pcb->preempt_count = 0;
|
2022-02-16 06:07:53 +00:00
|
|
|
|
// 先初始化系统调用模块
|
|
|
|
|
|
syscall_init();
|
2022-08-01 12:40:27 +00:00
|
|
|
|
io_mfence();
|
2022-04-10 13:30:16 +00:00
|
|
|
|
// 再初始化进程模块。顺序不能调转
|
|
|
|
|
|
sched_init();
|
2022-08-01 12:40:27 +00:00
|
|
|
|
io_mfence();
|
2022-04-15 07:23:17 +00:00
|
|
|
|
|
2022-04-10 13:30:16 +00:00
|
|
|
|
timer_init();
|
2022-02-21 14:59:57 +00:00
|
|
|
|
|
2022-08-01 12:40:27 +00:00
|
|
|
|
// 这里必须加内存屏障,否则会出错
|
|
|
|
|
|
io_mfence();
|
2022-04-10 13:30:16 +00:00
|
|
|
|
smp_init();
|
2022-08-01 12:40:27 +00:00
|
|
|
|
io_mfence();
|
|
|
|
|
|
|
2022-03-16 05:54:26 +00:00
|
|
|
|
cpu_init();
|
2022-05-21 13:49:56 +00:00
|
|
|
|
ps2_keyboard_init();
|
2022-04-04 10:42:22 +00:00
|
|
|
|
// ps2_mouse_init();
|
|
|
|
|
|
// ata_init();
|
2022-03-22 10:02:37 +00:00
|
|
|
|
pci_init();
|
2022-08-04 15:22:26 +00:00
|
|
|
|
io_mfence();
|
2022-03-30 07:36:00 +00:00
|
|
|
|
ahci_init();
|
2022-04-22 13:30:48 +00:00
|
|
|
|
|
2022-02-28 14:26:15 +00:00
|
|
|
|
// test_slab();
|
2022-03-12 16:31:46 +00:00
|
|
|
|
// test_mm();
|
2022-04-12 03:54:44 +00:00
|
|
|
|
|
2022-04-15 07:23:17 +00:00
|
|
|
|
// process_init();
|
2022-07-11 10:40:23 +00:00
|
|
|
|
HPET_init();
|
2022-08-01 12:40:27 +00:00
|
|
|
|
io_mfence();
|
2022-07-27 13:05:33 +00:00
|
|
|
|
HPET_measure_freq();
|
2022-08-01 12:40:27 +00:00
|
|
|
|
io_mfence();
|
2022-07-11 10:40:23 +00:00
|
|
|
|
// current_pcb->preempt_count = 0;
|
|
|
|
|
|
// kdebug("cpu_get_core_crysral_freq()=%ld", cpu_get_core_crysral_freq());
|
2022-08-04 13:40:26 +00:00
|
|
|
|
|
2022-04-19 07:13:59 +00:00
|
|
|
|
process_init();
|
2022-08-03 09:13:46 +00:00
|
|
|
|
// 启用double buffer
|
|
|
|
|
|
scm_enable_double_buffer();
|
2022-08-01 12:40:27 +00:00
|
|
|
|
io_mfence();
|
2022-07-11 10:40:23 +00:00
|
|
|
|
|
2022-04-26 16:39:02 +00:00
|
|
|
|
// fat32_init();
|
2022-07-11 10:40:23 +00:00
|
|
|
|
HPET_enable();
|
2022-08-01 11:43:21 +00:00
|
|
|
|
|
2022-08-01 12:40:27 +00:00
|
|
|
|
io_mfence();
|
2022-07-11 13:47:28 +00:00
|
|
|
|
// 系统初始化到此结束,剩下的初始化功能应当放在初始内核线程中执行
|
|
|
|
|
|
apic_timer_init();
|
2022-08-01 12:40:27 +00:00
|
|
|
|
io_mfence();
|
2022-08-04 14:14:54 +00:00
|
|
|
|
|
2022-08-04 15:22:26 +00:00
|
|
|
|
// 这里不能删除,否则在O1会报错
|
|
|
|
|
|
// while (1)
|
|
|
|
|
|
// pause();
|
2022-01-25 10:04:18 +00:00
|
|
|
|
}
|
2022-02-12 14:14:51 +00:00
|
|
|
|
|
2022-01-25 10:04:18 +00:00
|
|
|
|
//操作系统内核从这里开始执行
|
|
|
|
|
|
void Start_Kernel(void)
|
|
|
|
|
|
{
|
2022-04-15 07:23:17 +00:00
|
|
|
|
|
2022-04-09 13:11:07 +00:00
|
|
|
|
// 获取multiboot2的信息
|
|
|
|
|
|
uint64_t mb2_info, mb2_magic;
|
|
|
|
|
|
__asm__ __volatile__("movq %%r15, %0 \n\t"
|
|
|
|
|
|
"movq %%r14, %1 \n\t"
|
2022-04-10 13:30:16 +00:00
|
|
|
|
"movq %%r13, %2 \n\t"
|
|
|
|
|
|
"movq %%r12, %3 \n\t"
|
|
|
|
|
|
: "=r"(mb2_info), "=r"(mb2_magic), "=r"(bsp_gdt_size), "=r"(bsp_idt_size)::"memory");
|
|
|
|
|
|
reload_gdt();
|
|
|
|
|
|
reload_idt();
|
2022-04-15 07:23:17 +00:00
|
|
|
|
|
2022-04-13 09:58:06 +00:00
|
|
|
|
// 重新设置TSS描述符
|
|
|
|
|
|
set_tss_descriptor(10, (void *)(&initial_tss[0]));
|
2022-04-10 13:30:16 +00:00
|
|
|
|
|
2022-04-09 13:11:07 +00:00
|
|
|
|
mb2_info &= 0xffffffff;
|
|
|
|
|
|
mb2_magic &= 0xffffffff;
|
2022-04-15 06:15:32 +00:00
|
|
|
|
multiboot2_magic = (uint)mb2_magic;
|
2022-04-10 13:30:16 +00:00
|
|
|
|
multiboot2_boot_info_addr = mb2_info + PAGE_OFFSET;
|
2022-08-01 12:40:27 +00:00
|
|
|
|
io_mfence();
|
2022-02-12 14:14:51 +00:00
|
|
|
|
system_initialize();
|
2022-08-01 12:40:27 +00:00
|
|
|
|
io_mfence();
|
2022-02-22 12:45:32 +00:00
|
|
|
|
|
2022-04-07 12:21:28 +00:00
|
|
|
|
while (1)
|
2022-08-04 15:22:26 +00:00
|
|
|
|
pause();
|
2022-01-24 12:56:59 +00:00
|
|
|
|
}
|
2022-01-21 14:04:24 +00:00
|
|
|
|
|
2022-01-24 12:56:59 +00:00
|
|
|
|
void ignore_int()
|
|
|
|
|
|
{
|
2022-02-22 12:54:31 +00:00
|
|
|
|
kwarn("Unknown interrupt or fault at RIP.\n");
|
2022-08-01 11:43:21 +00:00
|
|
|
|
while (1)
|
|
|
|
|
|
;
|
2022-08-01 12:40:27 +00:00
|
|
|
|
}
|
2022-08-04 15:22:26 +00:00
|
|
|
|
#pragma GCC pop_options
|