tomoyuki-nakabayashi / Rustemu86

Apache License 2.0
5 stars 0 forks source link

bootloader最初のprintまで #35

Open tomoyuki-nakabayashi opened 6 years ago

tomoyuki-nakabayashi commented 6 years ago

$ oobjdump -d -m i386 -Maddr16,data16 -M intel bootloader


bootloader:     file format elf64-x86-64

Disassembly of section .bootloader:

0000000000007c00 <_stack_end>:
    7c00:   31 c0                   xor    ax,ax
    7c02:   8e d8                   mov    ds,ax
    7c04:   8e c0                   mov    es,ax
    7c06:   8e d0                   mov    ss,ax
    7c08:   8e e0                   mov    fs,ax
    7c0a:   8e e8                   mov    gs,ax
    7c0c:   fc                      cld    
    7c0d:   bc 00 7c                mov    sp,0x7c00
    7c10:   67 8d 35 16 7d 00 00    addr32 lea si,ds:0x7d16
    7c17:   66 e8 90 00 00 00       call   7cad <println>

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

0000000000007c23 <enter_protected_mode>:
    7c23:   fa                      cli    
    7c24:   1e                      push   ds
    7c25:   06                      push   es
    7c26:   67 0f 01 15 ad 7d 00    addr32 lgdtw ds:0x7dad
    7c2d:   00 
    7c2e:   0f 20 c0                mov    eax,cr0
    7c31:   0c 01                   or     al,0x1
    7c33:   0f 22 c0                mov    cr0,eax
    7c36:   eb 00                   jmp    7c38 <protected_mode>

0000000000007c38 <protected_mode>:
    7c38:   bb 08 00                mov    bx,0x8
    7c3b:   8e db                   mov    ds,bx
    7c3d:   8e c3                   mov    es,bx
    7c3f:   24 fe                   and    al,0xfe
    7c41:   0f 22 c0                mov    cr0,eax

0000000000007c44 <unreal_mode>:
    7c44:   07                      pop    es
    7c45:   1f                      pop    ds
    7c46:   fb                      sti    
    7c47:   bb 01 0f                mov    bx,0xf01
    7c4a:   66 b8 00 8f 0b 00       mov    eax,0xb8f00
    7c50:   3e 67 89 18             mov    WORD PTR ds:[eax],bx

0000000000007c54 <check_int13h_extensions>:
    7c54:   b4 41                   mov    ah,0x41
    7c56:   bb aa 55                mov    bx,0x55aa
    7c59:   cd 13                   int    0x13
    7c5b:   0f 82 9c 00             jb     7cfb <no_int13h_extensions>

0000000000007c5f <load_second_stage_from_disk>:
    7c5f:   67 66 8d 05 00 7e 00    addr32 lea eax,ds:0x7e00
    7c66:   00 
    7c67:   a3 c5 7d                mov    ds:0x7dc5,ax
    7c6a:   67 66 8d 1d 00 5e 01    addr32 lea ebx,ds:0x15e00
    7c71:   00 
    7c72:   66 29 c3                sub    ebx,eax
    7c75:   66 c1 eb 09             shr    ebx,0x9
    7c79:   67 89 1d c3 7d 00 00    addr32 mov WORD PTR ds:0x7dc3,bx
    7c80:   67 66 8d 1d 00 7c 00    addr32 lea ebx,ds:0x7c00
    7c87:   00 
    7c88:   66 29 d8                sub    eax,ebx
    7c8b:   66 c1 e8 09             shr    eax,0x9
    7c8f:   66 a3 c9 7d             mov    ds:0x7dc9,eax
    7c93:   67 8d 35 c1 7d 00 00    addr32 lea si,ds:0x7dc1
    7c9a:   b4 42                   mov    ah,0x42
    7c9c:   cd 13                   int    0x13
    7c9e:   72 64                   jb     7d04 <second_stage_load_failed>

0000000000007ca0 <jump_to_second_stage>:
    7ca0:   67 66 8d 05 43 7e 00    addr32 lea eax,ds:0x7e43
    7ca7:   00 
    7ca8:   66 ff e0                jmp    eax

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

0000000000007cad <println>:
    7cad:   66 e8 0c 00 00 00       call   7cbf <print>
    7cb3:   b0 0d                   mov    al,0xd
    7cb5:   66 e8 15 00 00 00       call   7cd0 <print_char>
    7cbb:   b0 0a                   mov    al,0xa
    7cbd:   eb 11                   jmp    7cd0 <print_char>

0000000000007cbf <print>:
    7cbf:   fc                      cld    

0000000000007cc0 <print_loop>:
    7cc0:   67 ac                   lods   al,BYTE PTR ds:[esi]
    7cc2:   84 c0                   test   al,al
    7cc4:   74 08                   je     7cce <print_done>
    7cc6:   66 e8 04 00 00 00       call   7cd0 <print_char>
    7ccc:   eb f2                   jmp    7cc0 <print_loop>

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

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

0000000000007cd6 <print_hex>:
    7cd6:   b9 04 00                mov    cx,0x4

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

0000000000007ce4 <.below_0xA>:
    7ce4:   04 30                   add    al,0x30
    7ce6:   66 e8 e4 ff ff ff       call   7cd0 <print_char>
    7cec:   c1 e3 04                shl    bx,0x4
    7cef:   e2 e8                   loop   7cd9 <.lp>
    7cf1:   66 c3                   ret    

0000000000007cf3 <error>:
    7cf3:   66 e8 b4 ff ff ff       call   7cad <println>
    7cf9:   eb b0                   jmp    7cab <spin>

0000000000007cfb <no_int13h_extensions>:
    7cfb:   67 8d 35 62 7d 00 00    addr32 lea si,ds:0x7d62
    7d02:   eb ef                   jmp    7cf3 <error>

0000000000007d04 <second_stage_load_failed>:
    7d04:   67 8d 35 83 7d 00 00    addr32 lea si,ds:0x7d83
    7d0b:   eb e6                   jmp    7cf3 <error>

0000000000007d0d <kernel_load_failed>:
    7d0d:   67 8d 35 18 7e 00 00    addr32 lea si,ds:0x7e18
    7d14:   eb dd                   jmp    7cf3 <error>

0000000000007d16 <boot_start_str>:
    7d16:   42                      inc    dx
    7d17:   6f                      outs   dx,WORD PTR ds:[si]
    7d18:   6f                      outs   dx,WORD PTR ds:[si]
    7d19:   74 69                   je     7d84 <second_stage_load_failed_str+0x1>
    7d1b:   6e                      outs   dx,BYTE PTR ds:[si]
    7d1c:   67 20 28                and    BYTE PTR [eax],ch
    7d1f:   66 69 72 73 74 20 73    imul   esi,DWORD PTR [bp+si+0x73],0x74732074
    7d26:   74 
    7d27:   61                      popa   
    7d28:   67 65 29 2e             sub    WORD PTR gs:[esi],bp
    7d2c:   2e                      cs
    7d2d:   2e                      cs
    ...

0000000000007d2f <second_stage_start_str>:
    7d2f:   42                      inc    dx
    7d30:   6f                      outs   dx,WORD PTR ds:[si]
    7d31:   6f                      outs   dx,WORD PTR ds:[si]
    7d32:   74 69                   je     7d9d <second_stage_load_failed_str+0x1a>
    7d34:   6e                      outs   dx,BYTE PTR ds:[si]
    7d35:   67 20 28                and    BYTE PTR [eax],ch
    7d38:   73 65                   jae    7d9f <second_stage_load_failed_str+0x1c>
    7d3a:   63 6f 6e                arpl   WORD PTR [bx+0x6e],bp
    7d3d:   64 20 73 74             and    BYTE PTR fs:[bp+di+0x74],dh
    7d41:   61                      popa   
    7d42:   67 65 29 2e             sub    WORD PTR gs:[esi],bp
    7d46:   2e                      cs
    7d47:   2e                      cs
    ...

0000000000007d49 <error_str>:
    7d49:   45                      inc    bp
    7d4a:   72 72                   jb     7dbe <flatdesc+0x5>
    7d4c:   6f                      outs   dx,WORD PTR ds:[si]
    7d4d:   72 3a                   jb     7d89 <second_stage_load_failed_str+0x6>
    7d4f:   20 00                   and    BYTE PTR [bx+si],al

0000000000007d51 <no_cpuid_str>:
    7d51:   4e                      dec    si
    7d52:   6f                      outs   dx,WORD PTR ds:[si]
    7d53:   20 43 50                and    BYTE PTR [bp+di+0x50],al
    7d56:   55                      push   bp
    7d57:   49                      dec    cx
    7d58:   44                      inc    sp
    7d59:   20 73 75                and    BYTE PTR [bp+di+0x75],dh
    7d5c:   70 70                   jo     7dce <dap_start_lba+0x5>
    7d5e:   6f                      outs   dx,WORD PTR ds:[si]
    7d5f:   72 74                   jb     7dd5 <dap_start_lba+0xc>
    ...

0000000000007d62 <no_int13h_extensions_str>:
    7d62:   4e                      dec    si
    7d63:   6f                      outs   dx,WORD PTR ds:[si]
    7d64:   20 73 75                and    BYTE PTR [bp+di+0x75],dh
    7d67:   70 70                   jo     7dd9 <dap_start_lba+0x10>
    7d69:   6f                      outs   dx,WORD PTR ds:[si]
    7d6a:   72 74                   jb     7de0 <dap_start_lba+0x17>
    7d6c:   20 66 6f                and    BYTE PTR [bp+0x6f],ah
    7d6f:   72 20                   jb     7d91 <second_stage_load_failed_str+0xe>
    7d71:   69 6e 74 31 33          imul   bp,WORD PTR [bp+0x74],0x3331
    7d76:   68 20 65                push   0x6520
    7d79:   78 74                   js     7def <dap_start_lba+0x26>
    7d7b:   65 6e                   outs   dx,BYTE PTR gs:[si]
    7d7d:   73 69                   jae    7de8 <dap_start_lba+0x1f>
    7d7f:   6f                      outs   dx,WORD PTR ds:[si]
    7d80:   6e                      outs   dx,BYTE PTR ds:[si]
    7d81:   73 00                   jae    7d83 <second_stage_load_failed_str>

0000000000007d83 <second_stage_load_failed_str>:
    7d83:   46                      inc    si
    7d84:   61                      popa   
    7d85:   69 6c 65 64 20          imul   bp,WORD PTR [si+0x65],0x2064
    7d8a:   74 6f                   je     7dfb <dap_start_lba+0x32>
    7d8c:   20 6c 6f                and    BYTE PTR [si+0x6f],ch
    7d8f:   61                      popa   
    7d90:   64 20 73 65             and    BYTE PTR fs:[bp+di+0x65],dh
    7d94:   63 6f 6e                arpl   WORD PTR [bx+0x6e],bp
    7d97:   64 20 73 74             and    BYTE PTR fs:[bp+di+0x74],dh
    7d9b:   61                      popa   
    7d9c:   67 65 20 6f 66          and    BYTE PTR gs:[edi+0x66],ch
    7da1:   20 62 6f                and    BYTE PTR [bp+si+0x6f],ah
    7da4:   6f                      outs   dx,WORD PTR ds:[si]
    7da5:   74 6c                   je     7e13 <_second_stage_start_addr+0x13>
    7da7:   6f                      outs   dx,WORD PTR ds:[si]
    7da8:   61                      popa   
    7da9:   64 65 72 00             fs gs jb 7dad <gdtinfo>

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

0000000000007db1 <gdt>:
    ...

0000000000007db9 <flatdesc>:
    7db9:   ff                      (bad)  
    7dba:   ff 00                   inc    WORD PTR [bx+si]
    7dbc:   00 00                   add    BYTE PTR [bx+si],al
    7dbe:   92                      xchg   dx,ax
    7dbf:   cf                      iret   
    ...

0000000000007dc1 <dap>:
    7dc1:   10 00                   adc    BYTE PTR [bx+si],al

0000000000007dc3 <dap_blocks>:
    ...

0000000000007dc5 <dap_buffer_addr>:
    ...

0000000000007dc7 <dap_buffer_seg>:
    ...

0000000000007dc9 <dap_start_lba>:
    ...
    7dfd:   00 55 aa                add    BYTE PTR [di-0x56],dl
tomoyuki-nakabayashi commented 6 years ago
    7c10:   67 8d 35 16 7d 00 00    addr32 lea si,ds:0x7d16

0x67: address size override prefix

これmodrmがめんどいパターンだな。

Mode: 00, Reg: 110, R/M: 101

image

r/m関係ないのか? 32bitアドレスの16bitを代入すれば良いだけ?

1: In protected/compatibility mode, this is just disp32, but in long mode this is [RIP]+disp32 (for 64-bit addresses) or [EIP]+disp32

あー、protected/compatibility modeだとPC相対できないから、RIP/EIP + disp32になっているところは、ただのdispなのか。なるほど。

tomoyuki-nakabayashi commented 6 years ago
    7c17:   66 e8 90 00 00 00       call   7cad <println>

さて、問題児来たね。

operand override prefixつきなので、32-bitで、PC相対ジャンプ。

tomoyuki-nakabayashi commented 6 years ago

先に、push/pop作ってしまうか。と思ったが、その前にload/storeか。

tomoyuki-nakabayashi commented 6 years ago
    7c79:   67 89 1d c3 7d 00 00    addr32 mov WORD PTR ds:0x7dc3,bx

load命令。対応するstoreも作らないと。