第一句子网 - 唯美句子、句子迷、好句子大全
第一句子网 > 【OS学习笔记】三十二 保护模式九:分页机制对应的汇编代码之---内核代码

【OS学习笔记】三十二 保护模式九:分页机制对应的汇编代码之---内核代码

时间:2024-07-11 00:58:01

相关推荐

【OS学习笔记】三十二 保护模式九:分页机制对应的汇编代码之---内核代码

本片文章是以下两篇文章:

【OS学习笔记】三十 保护模式九:段页式内存管理机制概述【OS学习笔记】三十一 保护模式九:页目录、页表和页三者的关系详解

对应的内核汇编代码。

;代码清单16-1;文件名:c16_core.asm;文件说明:保护模式微型核心程序 ;以下常量定义部分。内核的大部分内容都应当固定core_code_seg_selequ 0x38 ;内核代码段选择子core_data_seg_selequ 0x30 ;内核数据段选择子 sys_routine_seg_sel equ 0x28 ;系统公共例程代码段的选择子 video_ram_seg_selequ 0x20 ;视频显示缓冲区的段选择子core_stack_seg_sel equ 0x18 ;内核堆栈段选择子mem_0_4_gb_seg_sel equ 0x08 ;整个0-4GB内存的段的选择子;-------------------------------------------------------------------------------;以下是系统核心的头部,用于加载核心程序 core_lengthdd core_end ;核心程序总长度#00sys_routine_seg dd section.sys_routine.start;系统公用例程段位置#04core_data_seg dd section.core_data.start;核心数据段位置#08core_code_seg dd section.core_code.start;核心代码段位置#0ccore_entry dd start;核心代码段入口点#10dw core_code_seg_sel;===============================================================================[bits 32];===============================================================================SECTION sys_routine vstart=0;系统公共例程代码段 ;-------------------------------------------------------------------------------;字符串显示例程put_string: ;显示0终止的字符串并移动光标 ;输入:DS:EBX=串地址push ecx.getc:mov cl,[ebx]or cl,cljz .exitcall put_charinc ebxjmp .getc.exit:pop ecxretf ;段间返回;-------------------------------------------------------------------------------put_char:;在当前光标处显示一个字符,并推进;光标。仅用于段内调用 ;输入:CL=字符ASCII码 pushad;以下取当前光标位置mov dx,0x3d4mov al,0x0eout dx,alinc dx ;0x3d5in al,dx ;高字mov ah,aldec dx ;0x3d4mov al,0x0fout dx,alinc dx ;0x3d5in al,dx ;低字mov bx,ax;BX=代表光标位置的16位数cmp cl,0x0d ;回车符?jnz .put_0amov ax,bxmov bl,80div blmul blmov bx,axjmp .set_cursor.put_0a:cmp cl,0x0a ;换行符?jnz .put_otheradd bx,80jmp .roll_screen.put_other: ;正常显示字符push esmov eax,video_ram_seg_sel;0x800b8000段的选择子mov es,eaxshl bx,1mov [es:bx],clpop es;以下将光标位置推进一个字符shr bx,1inc bx.roll_screen:cmp bx,2000 ;光标超出屏幕?滚屏jl .set_cursorpush dspush esmov eax,video_ram_seg_selmov ds,eaxmov es,eaxcldmov esi,0xa0 ;小心!32位模式下movsb/w/d mov edi,0x00 ;使用的是esi/edi/ecx mov ecx,1920rep movsdmov bx,3840 ;清除屏幕最底一行mov ecx,80;32位程序应该使用ECX.cls:mov word[es:bx],0x0720add bx,2loop .clspop espop dsmov bx,1920.set_cursor:mov dx,0x3d4mov al,0x0eout dx,alinc dx ;0x3d5mov al,bhout dx,aldec dx ;0x3d4mov al,0x0fout dx,alinc dx ;0x3d5mov al,blout dx,alpopadret ;-------------------------------------------------------------------------------read_hard_disk_0: ;从硬盘读取一个逻辑扇区;EAX=逻辑扇区号;DS:EBX=目标缓冲区地址;返回:EBX=EBX+512push eax push ecxpush edxpush eaxmov dx,0x1f2mov al,1out dx,al;读取的扇区数inc dx ;0x1f3pop eaxout dx,al;LBA地址7~0inc dx ;0x1f4mov cl,8shr eax,clout dx,al;LBA地址15~8inc dx ;0x1f5shr eax,clout dx,al;LBA地址23~16inc dx ;0x1f6shr eax,clor al,0xe0;第一硬盘 LBA地址27~24out dx,alinc dx ;0x1f7mov al,0x20 ;读命令out dx,al.waits:in al,dxand al,0x88cmp al,0x08jnz .waits;不忙,且硬盘已准备好数据传输 mov ecx,256 ;总共要读取的字数mov dx,0x1f0.readw:in ax,dxmov [ebx],axadd ebx,2loop .readwpop edxpop ecxpop eaxretf ;段间返回 ;-------------------------------------------------------------------------------;汇编语言程序是极难一次成功,而且调试非常困难。这个例程可以提供帮助 put_hex_dword:;在当前光标处以十六进制形式显示;一个双字并推进光标 ;输入:EDX=要转换并显示的数字;输出:无pushadpush dsmov ax,core_data_seg_sel ;切换到核心数据段 mov ds,axmov ebx,bin_hex;指向核心数据段内的转换表mov ecx,8.xlt: rol edx,4mov eax,edxand eax,0x0000000fxlatpush ecxmov cl,al call put_charpop ecxloop .xltpop dspopadretf;-------------------------------------------------------------------------------set_up_gdt_descriptor: ;在GDT内安装一个新的描述符;输入:EDX:EAX=描述符 ;输出:CX=描述符的选择子push eaxpush ebxpush edxpush dspush esmov ebx,core_data_seg_sel;切换到核心数据段mov ds,ebxsgdt [pgdt] ;以便开始处理GDTmov ebx,mem_0_4_gb_seg_selmov es,ebxmovzx ebx,word [pgdt] ;GDT界限inc bx ;GDT总字节数,也是下一个描述符偏移add ebx,[pgdt+2] ;下一个描述符的线性地址mov [es:ebx],eaxmov [es:ebx+4],edxadd word [pgdt],8 ;增加一个描述符的大小lgdt [pgdt] ;对GDT的更改生效mov ax,[pgdt] ;得到GDT界限值xor dx,dxmov bx,8div bx ;除以8,去掉余数mov cx,axshl cx,3 ;将索引号移到正确位置pop espop dspop edxpop ebxpop eaxretf;-------------------------------------------------------------------------------make_seg_descriptor: ;构造存储器和系统的段描述符;输入:EAX=线性基地址;EBX=段界限;ECX=属性。各属性位都在原始;位置,无关的位清零 ;返回:EDX:EAX=描述符mov edx,eaxshl eax,16or ax,bx ;描述符前32位(EAX)构造完毕and edx,0xffff0000 ;清除基地址中无关的位rol edx,8bswap edx;装配基址的31~24和23~16 (80486+)xor bx,bxor edx,ebx;装配段界限的高4位or edx,ecx;装配属性retf;-------------------------------------------------------------------------------make_gate_descriptor: ;构造门的描述符(调用门等);输入:EAX=门代码在段内偏移地址; BX=门代码所在段的选择子 ; CX=段类型及属性等(各属;性位都在原始位置);返回:EDX:EAX=完整的描述符push ebxpush ecxmov edx,eaxand edx,0xffff0000 ;得到偏移地址高16位 or dx,cx ;组装属性部分到EDXand eax,0x0000ffff ;得到偏移地址低16位 shl ebx,16or eax,ebx;组装段选择子部分pop ecxpop ebxretf;-------------------------------------------------------------------------------allocate_a_4k_page:;分配一个4KB的页;输入:无;输出:EAX=页的物理地址push ebxpush ecxpush edxpush dsmov eax,core_data_seg_selmov ds,eaxxor eax,eax.b1:bts [page_bit_map],eaxjnc .b2inc eaxcmp eax,page_map_len*8jl .b1mov ebx,message_3call sys_routine_seg_sel:put_stringhlt ;没有可以分配的页,停机 .b2:shl eax,12;乘以4096(0x1000) pop dspop edxpop ecxpop ebxret;-------------------------------------------------------------------------------alloc_inst_a_page:;分配一个页,并安装在当前活动的;层级分页结构中;输入:EBX=页的线性地址push eaxpush ebxpush esipush dsmov eax,mem_0_4_gb_seg_selmov ds,eax;检查该线性地址所对应的页表是否存在mov esi,ebxand esi,0xffc00000shr esi,20;得到页目录索引,并乘以4 or esi,0xfffff000 ;页目录自身的线性地址+表内偏移 test dword [esi],0x00000001 ;P位是否为“1”。检查该线性地址是 jnz .b1 ;否已经有对应的页表;创建该线性地址所对应的页表 call allocate_a_4k_page ;分配一个页做为页表 or eax,0x00000007mov [esi],eax ;在页目录中登记该页表.b1:;开始访问该线性地址所对应的页表 mov esi,ebxshr esi,10and esi,0x003ff000 ;或者0xfffff000,因高10位是零 or esi,0xffc00000 ;得到该页表的线性地址;得到该线性地址在页表内的对应条目(页表项) and ebx,0x003ff000shr ebx,10;相当于右移12位,再乘以4or esi,ebx;页表项的线性地址 call allocate_a_4k_page ;分配一个页,这才是要安装的页or eax,0x00000007mov [esi],eax pop dspop esipop ebxpop eaxretf ;-------------------------------------------------------------------------------create_copy_cur_pdir: ;创建新页目录,并复制当前页目录内容;输入:无;输出:EAX=新页目录的物理地址 push dspush espush esipush edipush ebxpush ecxmov ebx,mem_0_4_gb_seg_selmov ds,ebxmov es,ebxcall allocate_a_4k_page mov ebx,eaxor ebx,0x00000007mov [0xfffffff8],ebxmov esi,0xfffff000 ;ESI->当前页目录的线性地址mov edi,0xffffe000 ;EDI->新页目录的线性地址mov ecx,1024 ;ECX=要复制的目录项数cldrepe movsd pop ecxpop ebxpop edipop esipop espop dsretf;-------------------------------------------------------------------------------terminate_current_task: ;终止当前任务;注意,执行此例程时,当前任务仍在;运行中。此例程其实也是当前任务的;一部分 mov eax,core_data_seg_selmov ds,eaxpushfdpop edxtest dx,0100_0000_0000_0000B ;测试NT位jnz .b1 ;当前任务是嵌套的,到.b1执行iretd jmp far [program_man_tss];程序管理器任务 .b1: iretdsys_routine_end:;===============================================================================SECTION core_data vstart=0 ;系统核心的数据段 ;------------------------------------------------------------------------------- pgdt dw 0 ;用于设置和修改GDT dd 0page_bit_mapdb 0xff,0xff,0xff,0xff,0xff,0x55,0x55,0xffdb 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xffdb 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xffdb 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xffdb 0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55db 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00db 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00db 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00page_map_lenequ $-page_bit_map;符号地址检索表salt:salt_1 db '@PrintString'times 256-($-salt_1) db 0dd put_stringdw sys_routine_seg_selsalt_2 db '@ReadDiskData'times 256-($-salt_2) db 0dd read_hard_disk_0dw sys_routine_seg_selsalt_3 db '@PrintDwordAsHexString'times 256-($-salt_3) db 0dd put_hex_dworddw sys_routine_seg_selsalt_4 db '@TerminateProgram'times 256-($-salt_4) db 0dd terminate_current_taskdw sys_routine_seg_selsalt_item_len equ $-salt_4salt_itemsequ ($-salt)/salt_item_lenmessage_0 db ' Working in system core,protect mode.'db 0x0d,0x0a,0message_1 db ' Paging is enabled.System core is mapped to'db ' address 0x80000000.',0x0d,0x0a,0message_2 db 0x0d,0x0adb ' System wide CALL-GATE mounted.',0x0d,0x0a,0message_3 db '********No more pages********',0message_4 db 0x0d,0x0a,' Task switching...@_@',0x0d,0x0a,0message_5 db 0x0d,0x0a,' Processor HALT.',0bin_hexdb '0123456789ABCDEF';put_hex_dword子过程用的查找表 core_buf times 512 db 0;内核用的缓冲区cpu_brnd0 db 0x0d,0x0a,' ',0cpu_brand times 52 db 0cpu_brnd1 db 0x0d,0x0a,0x0d,0x0a,0;任务控制块链tcb_chain dd 0;内核信息core_next_laddr dd 0x80100000 ;内核空间中下一个可分配的线性地址 program_man_tss dd 0 ;程序管理器的TSS描述符选择子 dw 0core_data_end:;===============================================================================SECTION core_code vstart=0;-------------------------------------------------------------------------------fill_descriptor_in_ldt: ;在LDT内安装一个新的描述符;输入:EDX:EAX=描述符;EBX=TCB基地址;输出:CX=描述符的选择子push eaxpush edxpush edipush dsmov ecx,mem_0_4_gb_seg_selmov ds,ecxmov edi,[ebx+0x0c] ;获得LDT基地址xor ecx,ecxmov cx,[ebx+0x0a] ;获得LDT界限inc cx ;LDT的总字节数,即新描述符偏移地址mov [edi+ecx+0x00],eaxmov [edi+ecx+0x04],edx ;安装描述符add cx,8 dec cx ;得到新的LDT界限值 mov [ebx+0x0a],cx ;更新LDT界限值到TCBmov ax,cxxor dx,dxmov cx,8div cxmov cx,axshl cx,3 ;左移3位,并且or cx,0000_0000_0000_0100B ;使TI位=1,指向LDT,最后使RPL=00 pop dspop edipop edxpop eaxret;-------------------------------------------------------------------------------load_relocate_program: ;加载并重定位用户程序;输入: PUSH 逻辑扇区号;PUSH 任务控制块基地址;输出:无 pushadpush dspush esmov ebp,esp ;为访问通过堆栈传递的参数做准备mov ecx,mem_0_4_gb_seg_selmov es,ecx;清空当前页目录的前半部分(对应低2GB的局部地址空间) mov ebx,0xfffff000xor esi,esi.b1:mov dword [es:ebx+esi*4],0x00000000inc esicmp esi,512jl .b1;以下开始分配内存并加载用户程序mov eax,core_data_seg_selmov ds,eax;切换DS到内核数据段mov eax,[ebp+12*4] ;从堆栈中取出用户程序起始扇区号mov ebx,core_buf ;读取程序头部数据call sys_routine_seg_sel:read_hard_disk_0;以下判断整个程序有多大mov eax,[core_buf] ;程序尺寸mov ebx,eaxand ebx,0xfffff000 ;使之4KB对齐 add ebx,0x1000 test eax,0x00000fff;程序的大小正好是4KB的倍数吗? cmovnz eax,ebx ;不是。使用凑整的结果mov ecx,eaxshr ecx,12;程序占用的总4KB页数 mov eax,mem_0_4_gb_seg_sel ;切换DS到0-4GB的段mov ds,eaxmov eax,[ebp+12*4] ;起始扇区号mov esi,[ebp+11*4] ;从堆栈中取得TCB的基地址.b2:mov ebx,[es:esi+0x06] ;取得可用的线性地址add dword [es:esi+0x06],0x1000call sys_routine_seg_sel:alloc_inst_a_pagepush ecxmov ecx,8.b3:call sys_routine_seg_sel:read_hard_disk_0inc eaxloop .b3pop ecxloop .b2;在内核地址空间内创建用户任务的TSSmov eax,core_data_seg_sel;切换DS到内核数据段mov ds,eaxmov ebx,[core_next_laddr];用户任务的TSS必须在全局空间上分配 call sys_routine_seg_sel:alloc_inst_a_pageadd dword [core_next_laddr],4096mov [es:esi+0x14],ebx ;在TCB中填写TSS的线性地址 mov word [es:esi+0x12],103 ;在TCB中填写TSS的界限值 ;在用户任务的局部地址空间内创建LDT mov ebx,[es:esi+0x06] ;从TCB中取得可用的线性地址add dword [es:esi+0x06],0x1000call sys_routine_seg_sel:alloc_inst_a_pagemov [es:esi+0x0c],ebx ;填写LDT线性地址到TCB中 ;建立程序代码段描述符mov eax,0x00000000mov ebx,0x000fffff mov ecx,0x00c0f800 ;4KB粒度的代码段描述符,特权级3call sys_routine_seg_sel:make_seg_descriptormov ebx,esi ;TCB的基地址call fill_descriptor_in_ldtor cx,0000_0000_0000_0011B ;设置选择子的特权级为3mov ebx,[es:esi+0x14] ;从TCB中获取TSS的线性地址mov [es:ebx+76],cx ;填写TSS的CS域 ;建立程序数据段描述符mov eax,0x00000000mov ebx,0x000fffff mov ecx,0x00c0f200 ;4KB粒度的数据段描述符,特权级3call sys_routine_seg_sel:make_seg_descriptormov ebx,esi ;TCB的基地址call fill_descriptor_in_ldtor cx,0000_0000_0000_0011B ;设置选择子的特权级为3mov ebx,[es:esi+0x14] ;从TCB中获取TSS的线性地址mov [es:ebx+84],cx ;填写TSS的DS域 mov [es:ebx+72],cx ;填写TSS的ES域mov [es:ebx+88],cx ;填写TSS的FS域mov [es:ebx+92],cx ;填写TSS的GS域;将数据段作为用户任务的3特权级固有堆栈 mov ebx,[es:esi+0x06] ;从TCB中取得可用的线性地址add dword [es:esi+0x06],0x1000call sys_routine_seg_sel:alloc_inst_a_pagemov ebx,[es:esi+0x14] ;从TCB中获取TSS的线性地址mov [es:ebx+80],cx ;填写TSS的SS域mov edx,[es:esi+0x06] ;堆栈的高端线性地址 mov [es:ebx+56],edx;填写TSS的ESP域 ;在用户任务的局部地址空间内创建0特权级堆栈mov ebx,[es:esi+0x06] ;从TCB中取得可用的线性地址add dword [es:esi+0x06],0x1000call sys_routine_seg_sel:alloc_inst_a_pagemov eax,0x00000000mov ebx,0x000fffffmov ecx,0x00c09200 ;4KB粒度的堆栈段描述符,特权级0call sys_routine_seg_sel:make_seg_descriptormov ebx,esi ;TCB的基地址call fill_descriptor_in_ldtor cx,0000_0000_0000_0000B ;设置选择子的特权级为0mov ebx,[es:esi+0x14] ;从TCB中获取TSS的线性地址mov [es:ebx+8],cx ;填写TSS的SS0域mov edx,[es:esi+0x06] ;堆栈的高端线性地址mov [es:ebx+4],edx ;填写TSS的ESP0域 ;在用户任务的局部地址空间内创建1特权级堆栈mov ebx,[es:esi+0x06] ;从TCB中取得可用的线性地址add dword [es:esi+0x06],0x1000call sys_routine_seg_sel:alloc_inst_a_pagemov eax,0x00000000mov ebx,0x000fffffmov ecx,0x00c0b200 ;4KB粒度的堆栈段描述符,特权级1call sys_routine_seg_sel:make_seg_descriptormov ebx,esi ;TCB的基地址call fill_descriptor_in_ldtor cx,0000_0000_0000_0001B ;设置选择子的特权级为1mov ebx,[es:esi+0x14] ;从TCB中获取TSS的线性地址mov [es:ebx+16],cx ;填写TSS的SS1域mov edx,[es:esi+0x06] ;堆栈的高端线性地址mov [es:ebx+12],edx;填写TSS的ESP1域 ;在用户任务的局部地址空间内创建2特权级堆栈mov ebx,[es:esi+0x06] ;从TCB中取得可用的线性地址add dword [es:esi+0x06],0x1000call sys_routine_seg_sel:alloc_inst_a_pagemov eax,0x00000000mov ebx,0x000fffffmov ecx,0x00c0d200 ;4KB粒度的堆栈段描述符,特权级2call sys_routine_seg_sel:make_seg_descriptormov ebx,esi ;TCB的基地址call fill_descriptor_in_ldtor cx,0000_0000_0000_0010B ;设置选择子的特权级为2mov ebx,[es:esi+0x14] ;从TCB中获取TSS的线性地址mov [es:ebx+24],cx ;填写TSS的SS2域mov edx,[es:esi+0x06] ;堆栈的高端线性地址mov [es:ebx+20],edx;填写TSS的ESP2域 ;重定位SALT mov eax,mem_0_4_gb_seg_sel ;访问任务的4GB虚拟地址空间时用 mov es,eaxmov eax,core_data_seg_selmov ds,eaxcldmov ecx,[es:0x0c] ;U-SALT条目数 mov edi,[es:0x08] ;U-SALT在4GB空间内的偏移 .b4:push ecxpush edimov ecx,salt_itemsmov esi,salt.b5:push edipush esipush ecxmov ecx,64;检索表中,每条目的比较次数 repe cmpsd;每次比较4字节 jnz .b6mov eax,[esi] ;若匹配,则esi恰好指向其后的地址mov [es:edi-256],eax;将字符串改写成偏移地址 mov ax,[esi+4]or ax,0000000000000011B ;以用户程序自己的特权级使用调用门;故RPL=3 mov [es:edi-252],ax;回填调用门选择子 .b6:pop ecxpop esiadd esi,salt_item_lenpop edi ;从头比较 loop .b5pop ediadd edi,256pop ecxloop .b4;在GDT中登记LDT描述符mov esi,[ebp+11*4] ;从堆栈中取得TCB的基地址mov eax,[es:esi+0x0c] ;LDT的起始线性地址movzx ebx,word [es:esi+0x0a] ;LDT段界限mov ecx,0x00408200 ;LDT描述符,特权级0call sys_routine_seg_sel:make_seg_descriptorcall sys_routine_seg_sel:set_up_gdt_descriptormov [es:esi+0x10],cx;登记LDT选择子到TCB中mov ebx,[es:esi+0x14] ;从TCB中获取TSS的线性地址mov [es:ebx+96],cx ;填写TSS的LDT域 mov word [es:ebx+0],0 ;反向链=0mov dx,[es:esi+0x12];段长度(界限)mov [es:ebx+102],dx;填写TSS的I/O位图偏移域 mov word [es:ebx+100],0 ;T=0mov eax,[es:0x04] ;从任务的4GB地址空间获取入口点 mov [es:ebx+32],eax;填写TSS的EIP域 pushfdpop edxmov [es:ebx+36],edx;填写TSS的EFLAGS域 ;在GDT中登记TSS描述符mov eax,[es:esi+0x14] ;从TCB中获取TSS的起始线性地址movzx ebx,word [es:esi+0x12] ;段长度(界限)mov ecx,0x00408900 ;TSS描述符,特权级0call sys_routine_seg_sel:make_seg_descriptorcall sys_routine_seg_sel:set_up_gdt_descriptormov [es:esi+0x18],cx;登记TSS选择子到TCB;创建用户任务的页目录;注意!页的分配和使用是由页位图决定的,可以不占用线性地址空间 call sys_routine_seg_sel:create_copy_cur_pdirmov ebx,[es:esi+0x14] ;从TCB中获取TSS的线性地址mov dword [es:ebx+28],eax;填写TSS的CR3(PDBR)域pop es ;恢复到调用此过程前的es段 pop ds ;恢复到调用此过程前的ds段popadret 8;丢弃调用本过程前压入的参数 ;-------------------------------------------------------------------------------append_to_tcb_link:;在TCB链上追加任务控制块;输入:ECX=TCB线性基地址push eaxpush edxpush dspush esmov eax,core_data_seg_sel;令DS指向内核数据段 mov ds,eaxmov eax,mem_0_4_gb_seg_sel ;令ES指向0..4GB段mov es,eaxmov dword [es: ecx+0x00],0 ;当前TCB指针域清零,以指示这是最;后一个TCBmov eax,[tcb_chain];TCB表头指针or eax,eax;链表为空?jz .notcb .searc:mov edx,eaxmov eax,[es: edx+0x00]or eax,eaxjnz .searcmov [es: edx+0x00],ecxjmp .retpc.notcb: mov [tcb_chain],ecx;若为空表,直接令表头指针指向TCB.retpc:pop espop dspop edxpop eaxret;-------------------------------------------------------------------------------start:mov ecx,core_data_seg_sel;令DS指向核心数据段 mov ds,ecxmov ecx,mem_0_4_gb_seg_sel ;令ES指向4GB数据段 mov es,ecxmov ebx,message_0call sys_routine_seg_sel:put_string;显示处理器品牌信息 mov eax,0x80000002cpuidmov [cpu_brand + 0x00],eaxmov [cpu_brand + 0x04],ebxmov [cpu_brand + 0x08],ecxmov [cpu_brand + 0x0c],edxmov eax,0x80000003cpuidmov [cpu_brand + 0x10],eaxmov [cpu_brand + 0x14],ebxmov [cpu_brand + 0x18],ecxmov [cpu_brand + 0x1c],edxmov eax,0x80000004cpuidmov [cpu_brand + 0x20],eaxmov [cpu_brand + 0x24],ebxmov [cpu_brand + 0x28],ecxmov [cpu_brand + 0x2c],edxmov ebx,cpu_brnd0 ;显示处理器品牌信息 call sys_routine_seg_sel:put_stringmov ebx,cpu_brandcall sys_routine_seg_sel:put_stringmov ebx,cpu_brnd1call sys_routine_seg_sel:put_string;准备打开分页机制;创建系统内核的页目录表PDT;页目录表清零 mov ecx,1024 ;1024个目录项mov ebx,0x00020000 ;页目录的物理地址xor esi,esi.b1:mov dword [es:ebx+esi],0x00000000 ;页目录表项清零 add esi,4loop .b1;在页目录内创建指向页目录自己的目录项mov dword [es:ebx+4092],0x00020003 ;在页目录内创建与线性地址0x00000000对应的目录项mov dword [es:ebx+0],0x00021003 ;写入目录项(页表的物理地址和属性);创建与上面那个目录项相对应的页表,初始化页表项 mov ebx,0x00021000 ;页表的物理地址xor eax,eax ;起始页的物理地址 xor esi,esi.b2: mov edx,eaxor edx,0x00000003 mov [es:ebx+esi*4],edx ;登记页的物理地址add eax,0x1000 ;下一个相邻页的物理地址 inc esicmp esi,256 ;仅低端1MB内存对应的页才是有效的 jl .b2.b3: ;其余的页表项置为无效mov dword [es:ebx+esi*4],0x00000000 inc esicmp esi,1024jl .b3 ;令CR3寄存器指向页目录,并正式开启页功能 mov eax,0x00020000 ;PCD=PWT=0mov cr3,eaxmov eax,cr0or eax,0x80000000mov cr0,eax ;开启分页机制;在页目录内创建与线性地址0x80000000对应的目录项mov ebx,0xfffff000 ;页目录自己的线性地址 mov esi,0x80000000 ;映射的起始地址shr esi,22;线性地址的高10位是目录索引shl esi,2mov dword [es:ebx+esi],0x00021003 ;写入目录项(页表的物理地址和属性);目标单元的线性地址为0xFFFFF200;将GDT中的段描述符映射到线性地址0x80000000sgdt [pgdt]mov ebx,[pgdt+2]or dword [es:ebx+0x10+4],0x80000000or dword [es:ebx+0x18+4],0x80000000or dword [es:ebx+0x20+4],0x80000000or dword [es:ebx+0x28+4],0x80000000or dword [es:ebx+0x30+4],0x80000000or dword [es:ebx+0x38+4],0x80000000add dword [pgdt+2],0x80000000;GDTR也用的是线性地址 lgdt [pgdt]jmp core_code_seg_sel:flush ;刷新段寄存器CS,启用高端线性地址 flush:mov eax,core_stack_seg_selmov ss,eaxmov eax,core_data_seg_selmov ds,eaxmov ebx,message_1call sys_routine_seg_sel:put_string;以下开始安装为整个系统服务的调用门。特权级之间的控制转移必须使用门mov edi,salt ;C-SALT表的起始位置 mov ecx,salt_items ;C-SALT表的条目数量 .b4:push ecx mov eax,[edi+256] ;该条目入口点的32位偏移地址 mov bx,[edi+260] ;该条目入口点的段选择子 mov cx,1_11_0_1100_000_00000B;特权级3的调用门(3以上的特权级才;允许访问),0个参数(因为用寄存器;传递参数,而没有用栈) call sys_routine_seg_sel:make_gate_descriptorcall sys_routine_seg_sel:set_up_gdt_descriptormov [edi+260],cx ;将返回的门描述符选择子回填add edi,salt_item_len ;指向下一个C-SALT条目 pop ecxloop .b4;对门进行测试 mov ebx,message_2call far [salt_1+256] ;通过门显示信息(偏移量将被忽略) ;为程序管理器的TSS分配内存空间mov ebx,[core_next_laddr]call sys_routine_seg_sel:alloc_inst_a_pageadd dword [core_next_laddr],4096;在程序管理器的TSS中设置必要的项目 mov word [es:ebx+0],0 ;反向链=0mov eax,cr3mov dword [es:ebx+28],eax;登记CR3(PDBR)mov word [es:ebx+96],0 ;没有LDT。处理器允许没有LDT的任务。mov word [es:ebx+100],0 ;T=0mov word [es:ebx+102],103;没有I/O位图。0特权级事实上不需要。;创建程序管理器的TSS描述符,并安装到GDT中 mov eax,ebx ;TSS的起始线性地址mov ebx,103 ;段长度(界限)mov ecx,0x00408900 ;TSS描述符,特权级0call sys_routine_seg_sel:make_seg_descriptorcall sys_routine_seg_sel:set_up_gdt_descriptormov [program_man_tss+4],cx ;保存程序管理器的TSS描述符选择子 ;任务寄存器TR中的内容是任务存在的标志,该内容也决定了当前任务是谁。;下面的指令为当前正在执行的0特权级任务“程序管理器”后补手续(TSS)。ltr cx;现在可认为“程序管理器”任务正执行中;创建用户任务的任务控制块 mov ebx,[core_next_laddr]call sys_routine_seg_sel:alloc_inst_a_pageadd dword [core_next_laddr],4096mov dword [es:ebx+0x06],0;用户任务局部空间的分配从0开始。mov word [es:ebx+0x0a],0xffff;登记LDT初始的界限到TCB中mov ecx,ebxcall append_to_tcb_link ;将此TCB添加到TCB链中 push dword 50 ;用户程序位于逻辑50扇区push ecx ;压入任务控制块起始线性地址 call load_relocate_program mov ebx,message_4call sys_routine_seg_sel:put_stringcall far [es:ecx+0x14] ;执行任务切换。mov ebx,message_5call sys_routine_seg_sel:put_stringhltcore_code_end:;-------------------------------------------------------------------------------SECTION core_trail;-------------------------------------------------------------------------------core_end:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。