Why I can't access to Tss variable in this C code Qnd How to fix this? and why I get Page Fault exception while I try to write to Tss variable from C code? in protected mode I declare gdt64 and Tss in boot32.S:
.align 16
gdt64:
.quad 0x0000000000000000 // 0x00 NULL
.quad 0x0020980000000000 // 0x08 KCODE64
.quad 0x0020f80000000000
.quad 0x0000f20000000000
TssDesc:
.word TssLen-1
.word 0
.byte 0
.byte 0x89
.byte 0
.byte 0
.quad 0
gdt64_end:
.align 16
.global init_gdt64_ptr_baseaddr
.global init_gdt64_ptr
init_gdt64_ptr:
.word gdt64_end - gdt64 - 1
init_gdt64_ptr_baseaddr:
.quad gdt64 # Change to QUAD from LONG
.global Tss
.global TssDesc
Tss:
.long 0
.quad 0xffff800000190000
.fill 88
.long TssLen
.equ TssLen, . - Tss
boot64.S:
.extern Tss
.extern TssDesc
...
SetTss:
lea Tss, %rax
lea TssDesc, %rbx
mov %ax, 2(%rbx)
shr $16,%rax
mov %al,4(%rbx)
shr $8,%rax
mov %al,7(%rbx)
shr $8,%rax
mov %eax,8(%rbx)
mov $0x20,%ax
ltr %ax
ret
...
.global _start64h
_start64h:
mov $KERNEL_VMA, %rax
add %rax, init_gdt64_ptr_baseaddr
lgdt init_gdt64_ptr(%rax)
add %rax, %rsp
call SetTss
But later in long mode and C code in proc.c:
extern struct TSS Tss;
static void set_tss(struct Process *proc)
{
Tss.rsp0 = proc->stack + STACK_SIZE;
}
And I get Page Fault (14)exception on this line when write to Tss.rsp0.
In gdb if I try to read Tss by "p/x Tss" got
this:
Cannot access memory at address 0x20103a
Qemu output: this line of code in exception handler :
printk("[Error %d at ring %d] %d:%x %x", tf->trapno, (tf->cs & 3), tf->errorcode, read_cr2(), tf->rip);
outputs this:
[Error 14 at ring 0] 2:20103EH FFFF800000209AC8H
link to this project on github: https://github.com/JustVic/kernel_multitasking
kernel development is so hard process :(...