Assembly 如何通过x86 TSS正确设置任务切换

Assembly 如何通过x86 TSS正确设置任务切换,assembly,gcc,x86,osdev,task-switching,Assembly,Gcc,X86,Osdev,Task Switching,我正在尝试开发一个基本内核,我想让它执行一个任务切换到我从连接的磁盘加载到内存中的一些代码 我试着遵循英特尔的第7章,但我的尝试似乎都导致了三重失误 特别是,我认为分页的设置有问题。我希望切换到的任务以其自己的任意页面映射运行,因此您将在下面的代码段中看到,此任务中的虚拟地址0x0应映射到物理地址0x2000000,这也是我加载代码的位置 我尝试设置分页时,为用户和内核页表和页目录定义了函数setup\u page\u directory\u和\u page\u tables中的int数组。内核

我正在尝试开发一个基本内核,我想让它执行一个任务切换到我从连接的磁盘加载到内存中的一些代码

我试着遵循英特尔的第7章,但我的尝试似乎都导致了三重失误

特别是,我认为分页的设置有问题。我希望切换到的任务以其自己的任意页面映射运行,因此您将在下面的代码段中看到,此任务中的虚拟地址0x0应映射到物理地址0x2000000,这也是我加载代码的位置

我尝试设置分页时,为用户和内核页表和页目录定义了函数
setup\u page\u directory\u和\u page\u tables
中的int数组。内核页面映射是身份映射,而用户的页面映射不是。 需要注意的一件有趣的事情是,当我更改分页设置以便用户的页面映射也是标识映射时,任务切换是成功的

可以获得整个代码(注意:相关分支是dev分支)

switch\u任务
永远不会返回CPU三重故障。我意识到,在设置用户的TSS时,简单地复制当前段寄存器和堆栈指针可能不是我想要做的,但应用程序中的第一条指令是
jmp$
,因此,由于我没有跳过段或使用堆栈,我认为它仍然可以工作

任务.h

#ifndef __TASK_H__

#include "system.h"

// Note: Fields ending in `_{l,u}16b` only use 16 bits.
// If such a field is defined with `unsigned int`, 
// it occupies the upper (u) or lower (l) 16 bits.
struct task_state_segment {
    unsigned int previous_task_link_l16b;
    unsigned int ESP0;
    unsigned int SS0_l16b;
    unsigned int ESP1;
    unsigned int SS1_l16b;
    unsigned int ESP2;
    unsigned int SS2_l16b;
    unsigned int CR3;
    unsigned int EIP;
    unsigned int EFLAGS;
    unsigned int EAX;
    unsigned int ECX;
    unsigned int EDX;
    unsigned int EBX;
    unsigned int ESP;
    unsigned int EBP;
    unsigned int ESI;
    unsigned int EDI;
    unsigned int ES_l16b;
    unsigned int CS_l16b;
    unsigned int SS_l16b;
    unsigned int DS_l16b;
    unsigned int FS_l16b;
    unsigned int GS_l16b;
    unsigned int LDT_u16b;
    unsigned int SSP;
}__attribute__((packed));

typedef struct task_state_segment tss;

void setup_tss();

void do_task_switch();

// #define USER_PAGE_DIR_SIZE 1024
// #define USER_PAGE_TABLE_SIZE 1024

#define USER_PAGE_DIR_SIZE 1024
#define USER_PAGE_TABLE_SIZE 1024

#endif // __TASK_H__

kernel_entry.asm

[bits 32]

global initialize_idt
global mem_map_buf_addr
global mem_map_buf_entry_count
global kernel_entry

extern main
extern idt_info_ptr

; kernel_entry expects the following information about the
; BIOS's memory map to be put on the stack:
;   the address of the buffer holding the memory map (top of stack)
;   the number of entries in the memory map.
kernel_entry:
  mov eax, [esp]
  mov [mem_map_buf_addr], eax
  mov eax, [esp+4]
  mov [mem_map_buf_entry_count], eax

  call main
jmp $

mem_map_buf_addr: dd 0x0
mem_map_buf_entry_count: dd 0x0

global pm_jump
pm_jump:
  jmp 0x8:pm_jmp_ret
pm_jmp_ret:
  mov ax, 0x10
    mov ds, ax
    mov ss, ax
    mov es, ax
    mov fs, ax
    mov gs, ax
  ret

extern kernel_page_directory
global setup_and_enable_paging
setup_and_enable_paging:
  ; point CR3 to page directory
  mov eax, kernel_page_directory
  or eax, 0x3
  mov cr3, eax

  ; set CRO.PG to 1
  mov ebx, cr0  ; set left-most bit of CPU special control register.
    or ebx, 0x80000000
    mov cr0, ebx

  ret

USER_TASK_GATE_GDT_IDX equ 7
USER_TASK_GATE equ 8 * USER_TASK_GATE_GDT_IDX

global switch_task
switch_task:
  call USER_TASK_GATE: 0x0
  ret

KERNEL_TASK_SEG_IDX equ 5
KERNEL_TASK_SEG equ 8 * KERNEL_TASK_SEG_IDX
kernel_task_selector: dw KERNEL_TASK_SEG

; Function to load kernel task register.
global load_kernel_tr
load_kernel_tr:
  ltr [kernel_task_selector]
  ret

global dummy_branch
dummy_branch:
  mov eax, 0xfadefade
  iret

initialize_idt:
  lidt [idt_info_ptr]
  ret

global enable_interrupts
enable_interrupts:
  sti
  ret

global disable_interrupts
disable_interrupts:
  cli
  ret

extern fault_handler
extern irq_handler

global isr_common
global isr0
global isr1
global isr2
global isr3
global isr4
global isr5
global isr6
global isr7
global isr8
global isr9
global isr10
global isr11
global isr12
global isr13
global isr14
global isr15
global isr16
global isr17
global isr18
global isr19
global isr20
global isr21
global isr22
global isr23
global isr24
global isr25
global isr26
global isr27
global isr28
global isr29
global isr30
global isr31

global irq0
global irq1
global irq2
global irq3
global irq4
global irq5
global irq6
global irq7
global irq8
global irq9
global irq10
global irq11
global irq12
global irq13
global irq14
global irq15

isr0:
  cli
  push byte 0
  push byte 0
  jmp isr_common

isr1:
  cli
  push byte 0
  push byte 1
  jmp isr_common

isr2:
  cli
  push byte 0
  push byte 2
  jmp isr_common

isr3:
  cli
  push byte 0
  push byte 3
  jmp isr_common

isr4:
  cli
  push byte 0
  push byte 4
  jmp isr_common

isr5:
  cli
  push byte 0
  push byte 5
  jmp isr_common

isr6:
  cli
  push byte 0
  push byte 6
  jmp isr_common

isr7:
  cli
  push byte 0
  push byte 7
  jmp isr_common

isr8:
  cli
  push byte 8
  jmp isr_common

isr9:
  cli
  push byte 0
  push byte 9
  jmp isr_common

isr10:
  cli
  push byte 10
  jmp isr_common

isr11:
  cli
  push byte 11
  jmp isr_common

isr12:
  cli
  push byte 12
  jmp isr_common

isr13:
  cli
  push byte 13
  jmp isr_common

isr14:
  cli
  push byte 14
  jmp isr_common

isr15:
  cli
  push byte 0
  push byte 15
  jmp isr_common

isr16:
  cli
  push byte 0
  push byte 16
  jmp isr_common

isr17:
  cli
  push byte 0
  push byte 17
  jmp isr_common

isr18:
  cli
  push byte 0
  push byte 18
  jmp isr_common

isr19:
  cli
  push byte 0
  push byte 19
  jmp isr_common

isr20:
  cli
  push byte 0
  push byte 20
  jmp isr_common

isr21:
  cli
  push byte 0
  push byte 21
  jmp isr_common

isr22:
  cli
  push byte 0
  push byte 22
  jmp isr_common

isr23:
  cli
  push byte 0
  push byte 23
  jmp isr_common

isr24:
  cli
  push byte 0
  push byte 24
  jmp isr_common

isr25:
  cli
  push byte 0
  push byte 25
  jmp isr_common

isr26:
  cli
  push byte 0
  push byte 26
  jmp isr_common

isr27:
  cli
  push byte 0
  push byte 27
  jmp isr_common

isr28:
  cli
  push byte 0
  push byte 28
  jmp isr_common

isr29:
  cli
  push byte 0
  push byte 29
  jmp isr_common

isr30:
  cli
  push byte 0
  push byte 30
  jmp isr_common

isr31:
  cli
  push byte 0
  push byte 31
  jmp isr_common

isr_common:
  pusha
  push ds
  push es
  push fs
  push gs
  mov ax, 0x10
  mov ds, ax
  mov es, ax
  mov fs, ax
  mov gs, ax
  mov eax, esp
  push eax
  mov eax, fault_handler
  call eax
  pop eax
  pop gs
  pop fs
  pop es
  pop ds
  popa
  add esp, 8
  iret

irq0:
  cli
  push byte 0
  push byte 32
  jmp irq_common

irq1:
  cli
  push byte 0
  push byte 33
  jmp irq_common

irq2:
  cli
  push byte 0
  push byte 34
  jmp irq_common

irq3:
  cli
  push byte 0
  push byte 35
  jmp irq_common

irq4:
  cli
  push byte 0
  push byte 36
  jmp irq_common

irq5:
  cli
  push byte 0
  push byte 37
  jmp irq_common

irq6:
  cli
  push byte 0
  push byte 38
  jmp irq_common

irq7:
  cli
  push byte 0
  push byte 39
  jmp irq_common

irq8:
  cli
  push byte 0
  push byte 40
  jmp irq_common

irq9:
  cli
  push byte 0
  push byte 41
  jmp irq_common

irq10:
  cli
  push byte 0
  push byte 42
  jmp irq_common

irq11:
  cli
  push byte 0
  push byte 43
  jmp irq_common

irq12:
  cli
  push byte 0
  push byte 44
  jmp irq_common

irq13:
  cli
  push byte 0
  push byte 45
  jmp irq_common

irq14:
  cli
  push byte 0
  push byte 46
  jmp irq_common

irq15:
  cli
  push byte 0
  push byte 47
  jmp irq_common


irq_common:
  pusha
  push ds
  push es
  push fs
  push gs
  mov ax, 0x10
  mov ds, ax
  mov es, ax
  mov fs, ax
  mov gs, ax
  mov eax, esp
  push eax
  mov eax, irq_handler
  call eax
  pop eax
  pop gs
  pop fs
  pop es
  pop ds
  popa
  add esp, 8
  iret

内核是用以下代码编译的:

C_SOURCES = $(wildcard kernel/*.c kernel/**/*.c drivers/**/*.c fs/*.c)

C_FLAGS = -Wall -O0 -m32 -fno-pie -fno-stack-protector -ffreestanding -fno-hosted -nolibc -nostdlib -g
C_FLAGS += -I./

OBJ = $(patsubst %.c, %.o, ${C_SOURCES})
kernel.bin: kernel/kernel_entry.o ${OBJ}
    ld -o kernel.bin -m elf_i386 $^ --oformat binary -T kernel.ld
%.o: %.c
    gcc ${C_FLAGS} -c $< -o $@

%.o: %.asm
    nasm $< -f elf -g -o $@
gcc -o app.bin -m32 -fno-pic -fno-pie -flinker-output=exec -Ttext=0x400000 -Wl,-emain app.s
加载的应用程序是app.s(不管什么都不做)

该应用程序由以下内容编译:

C_SOURCES = $(wildcard kernel/*.c kernel/**/*.c drivers/**/*.c fs/*.c)

C_FLAGS = -Wall -O0 -m32 -fno-pie -fno-stack-protector -ffreestanding -fno-hosted -nolibc -nostdlib -g
C_FLAGS += -I./

OBJ = $(patsubst %.c, %.o, ${C_SOURCES})
kernel.bin: kernel/kernel_entry.o ${OBJ}
    ld -o kernel.bin -m elf_i386 $^ --oformat binary -T kernel.ld
%.o: %.c
    gcc ${C_FLAGS} -c $< -o $@

%.o: %.asm
    nasm $< -f elf -g -o $@
gcc -o app.bin -m32 -fno-pic -fno-pie -flinker-output=exec -Ttext=0x400000 -Wl,-emain app.s
我通过使用Qemu命令
xp/16i 0x2000000
手动检查0x2000000处的字节,并将它们与
hextdump-C app.bin-n 16
进行比较,验证二进制文件(app.bin)是否正确加载到内存中。 有趣的是,
objdump-dapp.bin
的输出虽然包含与原始app.s类似的部分,但似乎没有匹配的字节。它以
f3 0f 1e..
哪个app.bin以
7f 45 4c..
开头。 我会把它贴在这里,剪下一些部分

objdump-d app.bin(已截断):

app.bin:文件格式elf32-i386
第节的分解。正文:
00400000 :
400000:f3 0f 1e fb endbr32
400004:31 ed xor%ebp,%ebp
400006:5e pop%esi
400007:89 e1 mov%esp,%ecx
400009:83 e4 f0和$0xfffffff0,%esp
40000c:50%推力eax
40000d:54%推力esp
40000e:52%推送edx
40000f:e8 22 00 00呼叫400036
400014:81 c3 c8 2f 00添加$0x2fc8,%ebx
40001a:8d 83 f4 d1 ff lea-0x2e0c(%ebx),%eax
400020:50%推力eax
400021:8d 83 84 d1 ff lea-0x2e7c(%ebx),%eax
400027:50%推力eax
400028:51%推力ecx
400029:56%esi
40002a:ff b3 1c 00 PUSH 0x1c(%ebx)
400030:e8 0b 10 c0 ff呼叫1040
400035:f4 hlt
400036:8b 1c 24 mov(%esp),%ebx
400039:c3 ret
40003a:66 90 xchg%ax,%ax
40003c:66 90 xchg%ax,%ax
40003e:66 90 xchg%ax,%ax
...
00400139 :
400139:8b 14 24 mov(%esp),%edx
40013c:c3 ret
0040013d:
40013d:eb-fe jmp 40013d
40013f:e9 76 b9 c0 ff jmp baba
400144:cf-iret
400145:01 d8添加%ebx,%eax
00400147 :
400147:b8 01 00 mov$0x1,%eax
40014c:83 f8 01 cmp$0x1,%eax
40014f:ea 9f 11 00 00 08 00 ljmp$0x8$0x119f
00400156 :
400156:cf-iret
400157:ea 9d 11 00 ljmp$0x0,$0x119d
40015e:66 90 xchg%ax,%ax
...
我还观察到一件事: 在我调查的某一点上,objdump输出确实匹配了
hextump
xp
字节(我不确定我现在做了什么来防止这种情况发生:()并且我观察到,在将二进制加载到0x2000000之后,位于_main的0x2000000+偏移量_处的指令(在本例中为0x13d)最终将是:

i、
jmp 0x2000000+u main的偏移量
如果我使用
jmp$
(我希望它是
jmp main的偏移量

ii.
jmp 0x2000000+任意_常量
如果我使用
jmp任意_常量
例如
jmp 0x13d
当我运行
xp/16 0x200013d
时,它将显示为
jmp 0x13d


(我认为这是相关的,因为我假设Qemu通过xp报告的内容正是CPU如何看待指令的。)

有很多代码需要处理,这不是一个简单的问题。我建议您将整个项目放在Github之类的东西中,包括一个生成文件或脚本来构建所有内容,然后再问这个问题。但有一个特点(我认为(就是您可以做
\u asm\u(“movw%%es,%0\n”:“=m”(tss->es\u l16b):);
设置用户模式TSS,但您正在将内核(环0)ES、DS、ES等段值移动到TSS。您需要为用户模式创建GDT描述符(我猜是环3)并将它们放入TSS。您似乎也从内核复制了ESP,但用户进程应该有自己的堆栈,因此您应该将其设置到用户空间中的某个内存位置。我建议BOCHS(可能更适合这种情况)或通过GDB调试器使用QEMU来逐步检查代码,并了解为什么它会失败
    .globl main
    .intel_syntax noprefix

main:
    jmp $
    jmp 0xbaba
    iret
    add eax, ebx
loop:
    mov eax, 0x1
    cmp eax, 0x1
    jmp 0x8: 0x119f
exit:
    iret
    jmp 0x0:0x119d

gcc -o app.bin -m32 -fno-pic -fno-pie -flinker-output=exec -Ttext=0x400000 -Wl,-emain app.s
app.bin:     file format elf32-i386


Disassembly of section .text:

00400000 <_start>:
  400000:   f3 0f 1e fb             endbr32 
  400004:   31 ed                   xor    %ebp,%ebp
  400006:   5e                      pop    %esi
  400007:   89 e1                   mov    %esp,%ecx
  400009:   83 e4 f0                and    $0xfffffff0,%esp
  40000c:   50                      push   %eax
  40000d:   54                      push   %esp
  40000e:   52                      push   %edx
  40000f:   e8 22 00 00 00          call   400036 <_start+0x36>
  400014:   81 c3 c8 2f 00 00       add    $0x2fc8,%ebx
  40001a:   8d 83 f4 d1 ff ff       lea    -0x2e0c(%ebx),%eax
  400020:   50                      push   %eax
  400021:   8d 83 84 d1 ff ff       lea    -0x2e7c(%ebx),%eax
  400027:   50                      push   %eax
  400028:   51                      push   %ecx
  400029:   56                      push   %esi
  40002a:   ff b3 1c 00 00 00       pushl  0x1c(%ebx)
  400030:   e8 0b 10 c0 ff          call   1040 <__libc_start_main@plt>
  400035:   f4                      hlt    
  400036:   8b 1c 24                mov    (%esp),%ebx
  400039:   c3                      ret    
  40003a:   66 90                   xchg   %ax,%ax
  40003c:   66 90                   xchg   %ax,%ax
  40003e:   66 90                   xchg   %ax,%ax

...


00400139 <__x86.get_pc_thunk.dx>:
  400139:   8b 14 24                mov    (%esp),%edx
  40013c:   c3                      ret    

0040013d <main>:
  40013d:   eb fe                   jmp    40013d <main>
  40013f:   e9 76 b9 c0 ff          jmp    baba <__cxa_finalize@plt+0xaa6a>
  400144:   cf                      iret   
  400145:   01 d8                   add    %ebx,%eax

00400147 <loop>:
  400147:   b8 01 00 00 00          mov    $0x1,%eax
  40014c:   83 f8 01                cmp    $0x1,%eax
  40014f:   ea 9f 11 00 00 08 00    ljmp   $0x8,$0x119f

00400156 <exit>:
  400156:   cf                      iret   
  400157:   ea 9d 11 00 00 00 00    ljmp   $0x0,$0x119d
  40015e:   66 90                   xchg   %ax,%ax
...