XuSenfeng

个人站

复读了,更新随缘,有的文件不全或者图片缺失具体看我的笔记库(https://github.com/XuSenfeng/note)


加载器loader的实现

目录

加载器loader的实现

static void show_msg(const char * msg)
{
    char c;
    while((c = *msg++) != '\0')
    {
        asm(
    	    "mov $0xe, %%ah\n\t"
    	    "mov %[ch], %%al\n\t"
    	    "int $0x10"::[ch]"r"(c)
        );
	   
    }
}

具体的写法参照补充知识2

获取内存信息

Detecting Memory (x86) - OSDev Wiki

这篇文章有详细介绍个汇总内存检测方式

For the first call to the function, point ES:DI at the destination buffer for the list. Clear EBX. Set EDX to the magic number 0x534D4150. Set EAX to 0xE820 (note that the upper 16-bits of EAX should be set to 0). Set ECX to 24. Do an INT 0x15.

If the first call to the function is successful, EAX will be set to 0x534D4150, and the Carry flag will be clear. EBX will be set to some non-zero value, which must be preserved for the next call to the function. CL will contain the number of bytes actually stored at ES:DI (probably 20).

For the subsequent calls to the function: increment DI by your list entry size, reset EAX to 0xE820, and ECX to 24. When you reach the end of the list, EBX may reset to 0. If you call the function again with EBX = 0, the list will start over. If EBX does not reset to 0, the function will return with Carry set when you try to access the entry after the last valid entry.

调用的时候按照上面进行设置, 会把内容保存在ES:DI里面, 之后再次调用知道EBX=0表示读取完成

  • First uint64_t = Base address
  • Second uint64_t = Length of “region” (if this value is 0, ignore the entry)
  • Next uint32_t = Region “type”
    • Type 1: Usable (normal) RAM
    • Type 2: Reserved - unusable
    • Type 3: ACPI reclaimable memory
    • Type 4: ACPI NVS memory
    • Type 5: Area containing bad memory
  • Next uint32_t = ACPI 3.0 Extended Attributes bitfield (if 24 bytes are returned, instead of 20)
    • Bit 0 of the Extended Attributes indicates if the entire entry should be ignored (if the bit is clear). This is going to be a huge compatibility problem because most current OSs won’t read this bit and won’t ignore the entry.
    • Bit 1 of the Extended Attributes indicates if the entry is non-volatile (if the bit is set) or not. The standard states that “Memory reported as non-volatile may require characterization to determine its suitability for use as conventional RAM.”
    • The remaining 30 bits of the Extended Attributes are currently undefined.

进入保护模式

主要有四种模式, 实模式, 保护模式, 虚拟8086模式, AI32模式, 这里主要使用的模式是实模式和保护模式

只有在实模式才能使用BIOS, 进入保护模式之后扩大寄存器的范围

  • 切换的流程

关闭中断->打开A20地址线->加载GDT表->设置CR0使能保护模式->远跳转, 清空流水线

远跳转会清空流水线, 清空掉16位的流水线(经一个指令的执行分开为好几部分)

这时候对常见的汇编应用进行封装, 在头文件中使用关键字static inline, 因为inline只是建议使用内联函数, 不一定会执行, 如果函数被当做正常的函数运行, static会使得函数不会被重复包含

  • 打开A20

A20 Line - OSDev Wiki

in al, 0x92
or al, 2
out 0x92, al

On most newer computers starting with the IBM PS/2, the chipset has a FAST A20 option that can quickly enable the A20 line. To enable A20 this way, there is no need for delay loops or polling, just 3 simple instructions.