tomoyuki-nakabayashi / Rustemu86

Apache License 2.0
5 stars 0 forks source link

BlosOSのブートローダーを動かす #26

Open tomoyuki-nakabayashi opened 6 years ago

tomoyuki-nakabayashi commented 6 years ago

https://github.com/rust-osdev/bootloader/blob/master/linker.ld

メモリマップはこれ。 さて、長い闘いになるかな。

tomoyuki-nakabayashi commented 6 years ago

16bitと32bitモードをそれぞれコプロとして持って状態を引き継ぐ、という作りでどうだろうか?

tomoyuki-nakabayashi commented 6 years ago

まぁその前にデバッグ機能が必要だな。

tomoyuki-nakabayashi commented 6 years ago
00000000  31C0              xor ax,ax
00000002  8ED8              mov ds,ax
00000004  8EC0              mov es,ax
00000006  8ED0              mov ss,ax
00000008  8EE0              mov fs,ax
0000000A  8EE8              mov gs,ax
tomoyuki-nakabayashi commented 6 years ago

MOV 0x8E RM

op1: ModR/M reg op2: ModR/M rm

tomoyuki-nakabayashi commented 6 years ago
    # clear the direction flag (e.g. go forward in memory when using
    # instructions like lodsb)
    cld

なんじゃらほい?

tomoyuki-nakabayashi commented 6 years ago

image

なるほど。

tomoyuki-nakabayashi commented 6 years ago

global_asm、そんなものもあるのか。

bootloader/main.rs

global_asm!(include_str!("boot.s"));
tomoyuki-nakabayashi commented 6 years ago

bootloader:     file format elf64-x86-64

Disassembly of section .bootloader:

0000000000007c00 <_stack_end>:
    7c00:   31 c0                   xor    %eax,%eax
    7c02:   8e d8                   mov    %eax,%ds
    7c04:   8e c0                   mov    %eax,%es
    7c06:   8e d0                   mov    %eax,%ss
    7c08:   8e e0                   mov    %eax,%fs
    7c0a:   8e e8                   mov    %eax,%gs
    7c0c:   fc                      cld    
    7c0d:   bc 00 7c 67 8d          mov    $0x8d677c00,%esp
    7c12:   35 16 7d 00 00          xor    $0x7d16,%eax
    7c17:   66 e8 90 00             callw  7cab <spin>
    ...

0000000000007c1d <enable_a20>:
    7c1d:   e4 92                   in     $0x92,%al
    7c1f:   0c 02                   or     $0x2,%al
    7c21:   e6 92                   out    %al,$0x92

0000000000007c23 <enter_protected_mode>:
    7c23:   fa                      cli    
    7c24:   1e                      (bad)  
    7c25:   06                      (bad)  
    7c26:   67 0f 01 15 ad 7d 00    lgdt   0x7dad(%eip)        # f9db <_ZN4core3fmt5write17h4a9b46e3234c9fa5E+0x26b>
    7c2d:   00 
    7c2e:   0f 20 c0                mov    %cr0,%rax
    7c31:   0c 01                   or     $0x1,%al
    7c33:   0f 22 c0                mov    %rax,%cr0
    7c36:   eb 00                   jmp    7c38 <protected_mode>

0000000000007c38 <protected_mode>:
    7c38:   bb 08 00 8e db          mov    $0xdb8e0008,%ebx
    7c3d:   8e c3                   mov    %ebx,%es
    7c3f:   24 fe                   and    $0xfe,%al
    7c41:   0f 22 c0                mov    %rax,%cr0

0000000000007c44 <unreal_mode>:
    7c44:   07                      (bad)  
    7c45:   1f                      (bad)  
    7c46:   fb                      sti    
    7c47:   bb 01 0f 66 b8          mov    $0xb8660f01,%ebx
    7c4c:   00 8f 0b 00 3e 67       add    %cl,0x673e000b(%rdi)
    7c52:   89 18                   mov    %ebx,(%rax)

0000000000007c54 <check_int13h_extensions>:
    7c54:   b4 41                   mov    $0x41,%ah
    7c56:   bb aa 55 cd 13          mov    $0x13cd55aa,%ebx
    7c5b:   0f 82 9c 00 67 66       jb     66677cfd <__bootloader_end+0x66661efd>

0000000000007c5f <load_second_stage_from_disk>:
    7c5f:   67 66 8d 05 00 7e 00    lea    0x7e00(%eip),%ax        # fa67 <_ZN4core3fmt5write17h4a9b46e3234c9fa5E+0x2f7>
    7c66:   00 
    7c67:   a3 c5 7d 67 66 8d 1d    movabs %eax,0x5e001d8d66677dc5
    7c6e:   00 5e 
    7c70:   01 00                   add    %eax,(%rax)
    7c72:   66 29 c3                sub    %ax,%bx
    7c75:   66 c1 eb 09             shr    $0x9,%bx
    7c79:   67 89 1d c3 7d 00 00    mov    %ebx,0x7dc3(%eip)        # fa43 <_ZN4core3fmt5write17h4a9b46e3234c9fa5E+0x2d3>
    7c80:   67 66 8d 1d 00 7c 00    lea    0x7c00(%eip),%bx        # f888 <_ZN4core3fmt5write17h4a9b46e3234c9fa5E+0x118>
    7c87:   00 
    7c88:   66 29 d8                sub    %bx,%ax
    7c8b:   66 c1 e8 09             shr    $0x9,%ax
    7c8f:   66 a3 c9 7d 67 8d 35    movabs %ax,0x7dc1358d677dc9
    7c96:   c1 7d 00 
    7c99:   00 b4 42 cd 13 72 64    add    %dh,0x647213cd(%rdx,%rax,2)

0000000000007ca0 <jump_to_second_stage>:
    7ca0:   67 66 8d 05 43 7e 00    lea    0x7e43(%eip),%ax        # faeb <_ZN4core3fmt5write17h4a9b46e3234c9fa5E+0x37b>
    7ca7:   00 
    7ca8:   66 ff e0                jmpw   *%ax

0000000000007cab <spin>:
    7cab:   eb fe                   jmp    7cab <spin>

0000000000007cad <println>:
    7cad:   66 e8 0c 00             callw  7cbd <println+0x10>
    7cb1:   00 00                   add    %al,(%rax)
    7cb3:   b0 0d                   mov    $0xd,%al
    7cb5:   66 e8 15 00             callw  7cce <print_done>
    7cb9:   00 00                   add    %al,(%rax)
    7cbb:   b0 0a                   mov    $0xa,%al
    7cbd:   eb 11                   jmp    7cd0 <print_char>

0000000000007cbf <print>:
    7cbf:   fc                      cld    

0000000000007cc0 <print_loop>:
    7cc0:   67 ac                   lods   %ds:(%esi),%al
    7cc2:   84 c0                   test   %al,%al
    7cc4:   74 08                   je     7cce <print_done>
    7cc6:   66 e8 04 00             callw  7cce <print_done>
    7cca:   00 00                   add    %al,(%rax)
    7ccc:   eb f2                   jmp    7cc0 <print_loop>

0000000000007cce <print_done>:
    7cce:   66 c3                   retw   

0000000000007cd0 <print_char>:
    7cd0:   b4 0e                   mov    $0xe,%ah
    7cd2:   cd 10                   int    $0x10
    7cd4:   66 c3                   retw   

0000000000007cd6 <print_hex>:
    7cd6:   b9 04 00 88 f8          mov    $0xf8880004,%ecx

0000000000007cd9 <.lp>:
    7cd9:   88 f8                   mov    %bh,%al
    7cdb:   c0 e8 04                shr    $0x4,%al
    7cde:   3c 0a                   cmp    $0xa,%al
    7ce0:   72 02                   jb     7ce4 <.below_0xA>
    7ce2:   04 07                   add    $0x7,%al

0000000000007ce4 <.below_0xA>:
    7ce4:   04 30                   add    $0x30,%al
    7ce6:   66 e8 e4 ff             callw  7cce <print_done>
    7cea:   ff                      (bad)  
    7ceb:   ff c1                   inc    %ecx
    7ced:   e3 04                   jrcxz  7cf3 <error>
    7cef:   e2 e8                   loop   7cd9 <.lp>
    7cf1:   66 c3                   retw   

0000000000007cf3 <error>:
    7cf3:   66 e8 b4 ff             callw  7cab <spin>
    7cf7:   ff                      (bad)  
    7cf8:   ff                      (bad)  
    7cf9:   eb b0                   jmp    7cab <spin>

0000000000007cfb <no_int13h_extensions>:
    7cfb:   67 8d 35 62 7d 00 00    lea    0x7d62(%eip),%esi        # fa64 <_ZN4core3fmt5write17h4a9b46e3234c9fa5E+0x2f4>
    7d02:   eb ef                   jmp    7cf3 <error>

0000000000007d04 <second_stage_load_failed>:
    7d04:   67 8d 35 83 7d 00 00    lea    0x7d83(%eip),%esi        # fa8e <_ZN4core3fmt5write17h4a9b46e3234c9fa5E+0x31e>
    7d0b:   eb e6                   jmp    7cf3 <error>

0000000000007d0d <kernel_load_failed>:
    7d0d:   67 8d 35 18 7e 00 00    lea    0x7e18(%eip),%esi        # fb2c <_ZN4core3fmt9Formatter12pad_integral17hda853aacc478d35fE+0x2c>
    7d14:   eb dd                   jmp    7cf3 <error>

0000000000007d16 <boot_start_str>:
    7d16:   42 6f                   rex.X outsl %ds:(%rsi),(%dx)
    7d18:   6f                      outsl  %ds:(%rsi),(%dx)
    7d19:   74 69                   je     7d84 <second_stage_load_failed_str+0x1>
    7d1b:   6e                      outsb  %ds:(%rsi),(%dx)
    7d1c:   67 20 28                and    %ch,(%eax)
    7d1f:   66 69 72 73 74 20       imul   $0x2074,0x73(%rdx),%si
    7d25:   73 74                   jae    7d9b <second_stage_load_failed_str+0x18>
    7d27:   61                      (bad)  
    7d28:   67 65 29 2e             sub    %ebp,%gs:(%esi)
    7d2c:   2e 2e 00 42 6f          cs add %al,%cs:0x6f(%rdx)

0000000000007d2f <second_stage_start_str>:
    7d2f:   42 6f                   rex.X outsl %ds:(%rsi),(%dx)
    7d31:   6f                      outsl  %ds:(%rsi),(%dx)
    7d32:   74 69                   je     7d9d <second_stage_load_failed_str+0x1a>
    7d34:   6e                      outsb  %ds:(%rsi),(%dx)
    7d35:   67 20 28                and    %ch,(%eax)
    7d38:   73 65                   jae    7d9f <second_stage_load_failed_str+0x1c>
    7d3a:   63 6f 6e                movslq 0x6e(%rdi),%ebp
    7d3d:   64 20 73 74             and    %dh,%fs:0x74(%rbx)
    7d41:   61                      (bad)  
    7d42:   67 65 29 2e             sub    %ebp,%gs:(%esi)
    7d46:   2e 2e 00 45 72          cs add %al,%cs:0x72(%rbp)

0000000000007d49 <error_str>:
    7d49:   45 72 72                rex.RB jb 7dbe <flatdesc+0x5>
    7d4c:   6f                      outsl  %ds:(%rsi),(%dx)
    7d4d:   72 3a                   jb     7d89 <second_stage_load_failed_str+0x6>
    7d4f:   20 00                   and    %al,(%rax)

0000000000007d51 <no_cpuid_str>:
    7d51:   4e 6f                   rex.WRX outsl %ds:(%rsi),(%dx)
    7d53:   20 43 50                and    %al,0x50(%rbx)
    7d56:   55                      push   %rbp
    7d57:   49                      rex.WB
    7d58:   44 20 73 75             and    %r14b,0x75(%rbx)
    7d5c:   70 70                   jo     7dce <dap_start_lba+0x5>
    7d5e:   6f                      outsl  %ds:(%rsi),(%dx)
    7d5f:   72 74                   jb     7dd5 <dap_start_lba+0xc>
    ...

0000000000007d62 <no_int13h_extensions_str>:
    7d62:   4e 6f                   rex.WRX outsl %ds:(%rsi),(%dx)
    7d64:   20 73 75                and    %dh,0x75(%rbx)
    7d67:   70 70                   jo     7dd9 <dap_start_lba+0x10>
    7d69:   6f                      outsl  %ds:(%rsi),(%dx)
    7d6a:   72 74                   jb     7de0 <dap_start_lba+0x17>
    7d6c:   20 66 6f                and    %ah,0x6f(%rsi)
    7d6f:   72 20                   jb     7d91 <second_stage_load_failed_str+0xe>
    7d71:   69 6e 74 31 33 68 20    imul   $0x20683331,0x74(%rsi),%ebp
    7d78:   65 78 74                gs js  7def <dap_start_lba+0x26>
    7d7b:   65 6e                   outsb  %gs:(%rsi),(%dx)
    7d7d:   73 69                   jae    7de8 <dap_start_lba+0x1f>
    7d7f:   6f                      outsl  %ds:(%rsi),(%dx)
    7d80:   6e                      outsb  %ds:(%rsi),(%dx)
    7d81:   73 00                   jae    7d83 <second_stage_load_failed_str>

0000000000007d83 <second_stage_load_failed_str>:
    7d83:   46 61                   rex.RX (bad) 
    7d85:   69 6c 65 64 20 74 6f    imul   $0x206f7420,0x64(%rbp,%riz,2),%ebp
    7d8c:   20 
    7d8d:   6c                      insb   (%dx),%es:(%rdi)
    7d8e:   6f                      outsl  %ds:(%rsi),(%dx)
    7d8f:   61                      (bad)  
    7d90:   64 20 73 65             and    %dh,%fs:0x65(%rbx)
    7d94:   63 6f 6e                movslq 0x6e(%rdi),%ebp
    7d97:   64 20 73 74             and    %dh,%fs:0x74(%rbx)
    7d9b:   61                      (bad)  
    7d9c:   67 65 20 6f 66          and    %ch,%gs:0x66(%edi)
    7da1:   20 62 6f                and    %ah,0x6f(%rdx)
    7da4:   6f                      outsl  %ds:(%rsi),(%dx)
    7da5:   74 6c                   je     7e13 <_second_stage_start_addr+0x13>
    7da7:   6f                      outsl  %ds:(%rsi),(%dx)
    7da8:   61                      (bad)  
    7da9:   64 65 72 00             fs gs jb 7dad <gdtinfo>

0000000000007dad <gdtinfo>:
    7dad:   0f 00                   (bad)  
    7daf:   b1 7d                   mov    $0x7d,%cl

0000000000007db1 <gdt>:
    ...

0000000000007db9 <flatdesc>:
    7db9:   ff                      (bad)  
    7dba:   ff 00                   incl   (%rax)
    7dbc:   00 00                   add    %al,(%rax)
    7dbe:   92                      xchg   %eax,%edx
    7dbf:   cf                      iret   
    ...

0000000000007dc1 <dap>:
    7dc1:   10 00                   adc    %al,(%rax)

0000000000007dc3 <dap_blocks>:
    ...

0000000000007dc5 <dap_buffer_addr>:
    ...

0000000000007dc7 <dap_buffer_seg>:
    ...

0000000000007dc9 <dap_start_lba>:
    ...
    7dfd:   00 55 aa                add    %dl,-0x56(%rbp)
tomoyuki-nakabayashi commented 6 years ago

良く見ると命令列がバグっているな。x86_64でdeasembleされているっぽい。

tomoyuki-nakabayashi commented 6 years ago
$ objdump -d -m i386 -Maddr16,data16 bootloader | less

こうか。