Page 1 of 1

Got stuck when mapping kernel to lower-half

Posted: Thu Mar 10, 2022 9:04 am
by NeonLightions
Hi,
Like the title, at Core/Process/process.c, function 'initproc_init', line 101. After the CPU jumps to paging_walk_pgdir(proc->pgdir, vaddr, true). It gots stuck! But in 'aos_main', it continues excuting scheduler() function. I don't know how to debug this one, can anyone help me?
Link to my repo: https://github.com/NeonLightions/Amore-OS

Re: Got stuck when mapping kernel to lower-half

Posted: Thu Mar 10, 2022 11:24 am
by Octocontrabass
How exactly does it get stuck?

Why aren't you using the memory map provided by your bootloader?

Re: Got stuck when mapping kernel to lower-half

Posted: Thu Mar 10, 2022 7:41 pm
by NeonLightions
Octocontrabass wrote:How exactly does it get stuck?

Why aren't you using the memory map provided by your bootloader?
It got stuck when allocating page table in 'Core/Memory/paging.c', function paging_walk_pgdir, line 59. I don't know why it got stuck there, maybe because the salloc() function?

What is memory map?

Re: Got stuck when mapping kernel to lower-half

Posted: Thu Mar 10, 2022 8:03 pm
by Octocontrabass
NeonLightions wrote:I don't know why it got stuck there, maybe because the salloc() function?
Have you tried switching to the assembly view and stepping individual instructions instead of source code lines?
NeonLightions wrote:What is memory map?
The memory map tells you where memory is located. Memory is not located at the same addresses in every PC.

Re: Got stuck when mapping kernel to lower-half

Posted: Thu Mar 10, 2022 8:53 pm
by NeonLightions
Octocontrabass wrote:
NeonLightions wrote:I don't know why it got stuck there, maybe because the salloc() function?
Have you tried switching to the assembly view and stepping individual instructions instead of source code lines?
NeonLightions wrote:What is memory map?
The memory map tells you where memory is located. Memory is not located at the same addresses in every PC.
It causes another problem: It causes triple fault when finish mapping page table to physical frame, this:

Code: Select all

while (addr < PHYS_MAX)
    {
        page_table_entry_t *pte = paging_walk_pgdir_at_boot(kernel_pgdir, addr, true);
        info("Paging: mapping [%p] to [%p]", addr, pte);
        assert(pte != NULL);

        pte->present = 1;
        pte->rw = 0;
        pte->user = 0;
        pte->frame = ADDR_PAGE_NUMBER(addr);

        addr += PAGE_SIZE;
    }
After it gets out of the loop, triple fault fired.

Re: Got stuck when mapping kernel to lower-half

Posted: Thu Mar 10, 2022 9:09 pm
by Octocontrabass
Which exception occurs that causes the triple fault? What is the CPU state when this exception occurs? (Add "-d int" to your QEMU command line to get this information.)

Re: Got stuck when mapping kernel to lower-half

Posted: Fri Mar 11, 2022 1:28 am
by NeonLightions
Octocontrabass wrote:Which exception occurs that causes the triple fault? What is the CPU state when this exception occurs? (Add "-d int" to your QEMU command line to get this information.)
This is the CPU state (quite long):

Code: Select all

SMM: enter
EAX=00000001 EBX=00000000 ECX=02000000 EDX=02000628
ESI=0000000b EDI=02000000 EBP=000f2c72 ESP=00006d98
EIP=000ebaef EFL=00000002 [-------] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =0010 00000000 ffffffff 00cf9300 DPL=0 DS   [-WA]
CS =0008 00000000 ffffffff 00cf9b00 DPL=0 CS32 [-RA]
SS =0010 00000000 ffffffff 00cf9300 DPL=0 DS   [-WA]
DS =0010 00000000 ffffffff 00cf9300 DPL=0 DS   [-WA]
FS =0010 00000000 ffffffff 00cf9300 DPL=0 DS   [-WA]
GS =0010 00000000 ffffffff 00cf9300 DPL=0 DS   [-WA]
LDT=0000 00000000 0000ffff 00008200 DPL=0 LDT
TR =0000 00000000 0000ffff 00008b00 DPL=0 TSS32-busy
GDT=     000f6280 00000037
IDT=     000f62be 00000000
CR0=00000011 CR2=00000000 CR3=00000000 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000 
DR6=ffff0ff0 DR7=00000400
CCS=00000080 CCD=00000001 CCO=LOGICB  
EFER=0000000000000000
SMM: after RSM
EAX=00000001 EBX=00000000 ECX=02000000 EDX=02000628
ESI=0000000b EDI=02000000 EBP=000f2c72 ESP=00006d98
EIP=000ebaef EFL=00000002 [-------] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
CS =0008 00000000 ffffffff 00c09b00 DPL=0 CS32 [-RA]
SS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
DS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
FS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
GS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
LDT=0000 00000000 0000ffff 00008200 DPL=0 LDT
TR =0000 00000000 0000ffff 00008b00 DPL=0 TSS32-busy
GDT=     000f6280 00000037
IDT=     000f62be 00000000
CR0=00000011 CR2=00000000 CR3=00000000 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000 
DR6=ffff0ff0 DR7=00000400
CCS=00000000 CCD=00000000 CCO=EFLAGS  
EFER=0000000000000000
SMM: enter
EAX=000000b5 EBX=000f7bfc ECX=00001234 EDX=00006dff
ESI=00006d3c EDI=3ffbedc5 EBP=00006cfc ESP=00006cfc
EIP=00007bfb EFL=00000006 [-----P-] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =db80 000db800 ffffffff 008f9300
CS =f000 000f0000 ffffffff 008f9b00
SS =0000 00000000 ffffffff 008f9300
DS =0000 00000000 ffffffff 008f9300
FS =0000 00000000 ffffffff 008f9300
GS =0000 00000000 ffffffff 008f9300
LDT=0000 00000000 0000ffff 00008200
TR =0000 00000000 0000ffff 00008b00
GDT=     00000000 00000000
IDT=     00000000 000003ff
CR0=00000010 CR2=00000000 CR3=00000000 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000 
DR6=ffff0ff0 DR7=00000400
CCS=00000004 CCD=00006cfc CCO=EFLAGS  
EFER=0000000000000000
SMM: after RSM
EAX=000000b5 EBX=000f7bfc ECX=00001234 EDX=00006dff
ESI=00006d3c EDI=3ffbedc5 EBP=00006cfc ESP=00006cfc
EIP=000f7bfc EFL=00000002 [-------] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
CS =0008 00000000 ffffffff 00c09b00 DPL=0 CS32 [-RA]
SS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
DS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
FS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
GS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
LDT=0000 00000000 0000ffff 00008200 DPL=0 LDT
TR =0000 00000000 0000ffff 00008b00 DPL=0 TSS32-busy
GDT=     000f6280 00000037
IDT=     000f62be 00000000
CR0=00000011 CR2=00000000 CR3=00000000 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000 
DR6=ffff0ff0 DR7=00000400
CCS=00000000 CCD=00000000 CCO=EFLAGS  
EFER=0000000000000000
SMM: enter
EAX=000000b5 EBX=00007c16 ECX=00005678 EDX=3ffabb00
ESI=000ea600 EDI=3ffbedc5 EBP=00006cfc ESP=00006cfc
EIP=000f7c15 EFL=00000016 [----AP-] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
CS =0008 00000000 ffffffff 00c09b00 DPL=0 CS32 [-RA]
SS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
DS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
FS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
GS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
LDT=0000 00000000 0000ffff 00008200 DPL=0 LDT
TR =0000 00000000 0000ffff 00008b00 DPL=0 TSS32-busy
GDT=     000f6280 00000037
IDT=     000f62be 00000000
CR0=00000011 CR2=00000000 CR3=00000000 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000 
DR6=ffff0ff0 DR7=00000400
CCS=00000014 CCD=00006ce8 CCO=EFLAGS  
EFER=0000000000000000
SMM: after RSM
EAX=000000b5 EBX=00007c16 ECX=00005678 EDX=3ffabb00
ESI=000ea600 EDI=3ffbedc5 EBP=00006cfc ESP=00006cfc
EIP=00007c16 EFL=00000006 [-----P-] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =db80 000db800 ffffffff 00809300
CS =f000 000f0000 ffffffff 00809b00
SS =0000 00000000 ffffffff 00809300
DS =0000 00000000 ffffffff 00809300
FS =0000 00000000 ffffffff 00809300
GS =0000 00000000 ffffffff 00809300
LDT=0000 00000000 0000ffff 00008200
TR =0000 00000000 0000ffff 00008b00
GDT=     00000000 00000000
IDT=     00000000 000003ff
CR0=00000010 CR2=00000000 CR3=00000000 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000 
DR6=ffff0ff0 DR7=00000400
CCS=00000004 CCD=00000001 CCO=EFLAGS  
EFER=0000000000000000
SMM: enter
EAX=000000b5 EBX=000f7bfc ECX=00001234 EDX=00006aff
ESI=00006a22 EDI=3ffbedc5 EBP=000069e2 ESP=000069e2
EIP=00007bfb EFL=00000006 [-----P-] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =db80 000db800 ffffffff 008f9300
CS =f000 000f0000 ffffffff 008f9b00
SS =0000 00000000 ffffffff 008f9300
DS =0000 00000000 ffffffff 008f9300
FS =0000 00000000 ffffffff 008f9300
GS =ca00 000ca000 ffffffff 008f9300
LDT=0000 00000000 0000ffff 00008200
TR =0000 00000000 0000ffff 00008b00
GDT=     00000000 00000000
IDT=     00000000 000003ff
CR0=00000010 CR2=00000000 CR3=00000000 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000 
DR6=ffff0ff0 DR7=00000400
CCS=00000004 CCD=000069e2 CCO=EFLAGS  
EFER=0000000000000000
SMM: after RSM
EAX=000000b5 EBX=000f7bfc ECX=00001234 EDX=00006aff
ESI=00006a22 EDI=3ffbedc5 EBP=000069e2 ESP=000069e2
EIP=000f7bfc EFL=00000002 [-------] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
CS =0008 00000000 ffffffff 00c09b00 DPL=0 CS32 [-RA]
SS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
DS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
FS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
GS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
LDT=0000 00000000 0000ffff 00008200 DPL=0 LDT
TR =0000 00000000 0000ffff 00008b00 DPL=0 TSS32-busy
GDT=     000f6280 00000037
IDT=     000f62be 00000000
CR0=00000011 CR2=00000000 CR3=00000000 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000 
DR6=ffff0ff0 DR7=00000400
CCS=00000000 CCD=00000000 CCO=EFLAGS  
EFER=0000000000000000
SMM: enter
EAX=000000b5 EBX=00007c16 ECX=00005678 EDX=00000005
ESI=00000000 EDI=3ffbedc5 EBP=000069e2 ESP=000069e2
EIP=000f7c15 EFL=00000002 [-------] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
CS =0008 00000000 ffffffff 00c09b00 DPL=0 CS32 [-RA]
SS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
DS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
FS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
GS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
LDT=0000 00000000 0000ffff 00008200 DPL=0 LDT
TR =0000 00000000 0000ffff 00008b00 DPL=0 TSS32-busy
GDT=     000f6280 00000037
IDT=     000f62be 00000000
CR0=00000011 CR2=00000000 CR3=00000000 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000 
DR6=ffff0ff0 DR7=00000400
CCS=00000000 CCD=000069ce CCO=EFLAGS  
EFER=0000000000000000
SMM: after RSM
EAX=000000b5 EBX=00007c16 ECX=00005678 EDX=00000005
ESI=00000000 EDI=3ffbedc5 EBP=000069e2 ESP=000069e2
EIP=00007c16 EFL=00000006 [-----P-] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =db80 000db800 ffffffff 00809300
CS =f000 000f0000 ffffffff 00809b00
SS =0000 00000000 ffffffff 00809300
DS =0000 00000000 ffffffff 00809300
FS =0000 00000000 ffffffff 00809300
GS =ca00 000ca000 ffffffff 00809300
LDT=0000 00000000 0000ffff 00008200
TR =0000 00000000 0000ffff 00008b00
GDT=     00000000 00000000
IDT=     00000000 000003ff
CR0=00000010 CR2=00000000 CR3=00000000 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000 
DR6=ffff0ff0 DR7=00000400
CCS=00000004 CCD=00000001 CCO=EFLAGS  
EFER=0000000000000000
SMM: enter
EAX=000000b5 EBX=000f7bfc ECX=00001234 EDX=00006aff
ESI=00006a1c EDI=3ffbedc5 EBP=000069dc ESP=000069dc
EIP=00007bfb EFL=00000002 [-------] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =db80 000db800 ffffffff 00809300
CS =f000 000f0000 ffffffff 00809b00
SS =0000 00000000 ffffffff 00809300
DS =0000 00000000 ffffffff 00809300
FS =0000 00000000 ffffffff 00809300
GS =ca00 000ca000 ffffffff 00809300
LDT=0000 00000000 0000ffff 00008200
TR =0000 00000000 0000ffff 00008b00
GDT=     00000000 00000000
IDT=     00000000 000003ff
CR0=00000010 CR2=00000000 CR3=00000000 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000 
DR6=ffff0ff0 DR7=00000400
CCS=00000000 CCD=000069dc CCO=EFLAGS  
EFER=0000000000000000
SMM: after RSM
EAX=000000b5 EBX=000f7bfc ECX=00001234 EDX=00006aff
ESI=00006a1c EDI=3ffbedc5 EBP=000069dc ESP=000069dc
EIP=000f7bfc EFL=00000002 [-------] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
CS =0008 00000000 ffffffff 00c09b00 DPL=0 CS32 [-RA]
SS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
DS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
FS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
GS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
LDT=0000 00000000 0000ffff 00008200 DPL=0 LDT
TR =0000 00000000 0000ffff 00008b00 DPL=0 TSS32-busy
GDT=     000f6280 00000037
IDT=     000f62be 00000000
CR0=00000011 CR2=00000000 CR3=00000000 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000 
DR6=ffff0ff0 DR7=00000400
CCS=00000000 CCD=00000000 CCO=EFLAGS  
EFER=0000000000000000
SMM: enter
EAX=000000b5 EBX=00007c16 ECX=00005678 EDX=00000003
ESI=3ff8cb00 EDI=3ffbedc5 EBP=000069dc ESP=000069dc
EIP=000f7c15 EFL=00000012 [----A--] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
CS =0008 00000000 ffffffff 00c09b00 DPL=0 CS32 [-RA]
SS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
DS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
FS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
GS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
LDT=0000 00000000 0000ffff 00008200 DPL=0 LDT
TR =0000 00000000 0000ffff 00008b00 DPL=0 TSS32-busy
GDT=     000f6280 00000037
IDT=     000f62be 00000000
CR0=00000011 CR2=00000000 CR3=00000000 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000 
DR6=ffff0ff0 DR7=00000400
CCS=00000010 CCD=000069c8 CCO=EFLAGS  
EFER=0000000000000000
SMM: after RSM
EAX=000000b5 EBX=00007c16 ECX=00005678 EDX=00000003
ESI=3ff8cb00 EDI=3ffbedc5 EBP=000069dc ESP=000069dc
EIP=00007c16 EFL=00000002 [-------] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =db80 000db800 ffffffff 00809300
CS =f000 000f0000 ffffffff 00809b00
SS =0000 00000000 ffffffff 00809300
DS =0000 00000000 ffffffff 00809300
FS =0000 00000000 ffffffff 00809300
GS =ca00 000ca000 ffffffff 00809300
LDT=0000 00000000 0000ffff 00008200
TR =0000 00000000 0000ffff 00008b00
GDT=     00000000 00000000
IDT=     00000000 000003ff
CR0=00000010 CR2=00000000 CR3=00000000 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000 
DR6=ffff0ff0 DR7=00000400
CCS=00000000 CCD=00000001 CCO=EFLAGS  
EFER=0000000000000000
SMM: enter
EAX=000000b5 EBX=000f7bfc ECX=00001234 EDX=00006aff
ESI=00006a22 EDI=3ffbedc5 EBP=000069e2 ESP=000069e2
EIP=00007bfb EFL=00000006 [-----P-] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =db80 000db800 ffffffff 00809300
CS =f000 000f0000 ffffffff 00809b00
SS =0000 00000000 ffffffff 00809300
DS =0000 00000000 ffffffff 00809300
FS =0000 00000000 ffffffff 00809300
GS =ca00 000ca000 ffffffff 00809300
LDT=0000 00000000 0000ffff 00008200
TR =0000 00000000 0000ffff 00008b00
GDT=     00000000 00000000
IDT=     00000000 000003ff
CR0=00000010 CR2=00000000 CR3=00000000 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000 
DR6=ffff0ff0 DR7=00000400
CCS=00000004 CCD=000069e2 CCO=EFLAGS  
EFER=0000000000000000
SMM: after RSM
EAX=000000b5 EBX=000f7bfc ECX=00001234 EDX=00006aff
ESI=00006a22 EDI=3ffbedc5 EBP=000069e2 ESP=000069e2
EIP=000f7bfc EFL=00000002 [-------] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
CS =0008 00000000 ffffffff 00c09b00 DPL=0 CS32 [-RA]
SS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
DS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
FS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
GS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
LDT=0000 00000000 0000ffff 00008200 DPL=0 LDT
TR =0000 00000000 0000ffff 00008b00 DPL=0 TSS32-busy
GDT=     000f6280 00000037
IDT=     000f62be 00000000
CR0=00000011 CR2=00000000 CR3=00000000 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000 
DR6=ffff0ff0 DR7=00000400
CCS=00000000 CCD=00000000 CCO=EFLAGS  
EFER=0000000000000000
SMM: enter
EAX=000000b5 EBX=00007c16 ECX=00005678 EDX=00000005
ESI=00000000 EDI=3ffbedc5 EBP=000069e2 ESP=000069e2
EIP=000f7c15 EFL=00000002 [-------] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
CS =0008 00000000 ffffffff 00c09b00 DPL=0 CS32 [-RA]
SS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
DS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
FS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
GS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
LDT=0000 00000000 0000ffff 00008200 DPL=0 LDT
TR =0000 00000000 0000ffff 00008b00 DPL=0 TSS32-busy
GDT=     000f6280 00000037
IDT=     000f62be 00000000
CR0=00000011 CR2=00000000 CR3=00000000 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000 
DR6=ffff0ff0 DR7=00000400
CCS=00000000 CCD=000069ce CCO=EFLAGS  
EFER=0000000000000000
SMM: after RSM
EAX=000000b5 EBX=00007c16 ECX=00005678 EDX=00000005
ESI=00000000 EDI=3ffbedc5 EBP=000069e2 ESP=000069e2
EIP=00007c16 EFL=00000006 [-----P-] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =db80 000db800 ffffffff 00809300
CS =f000 000f0000 ffffffff 00809b00
SS =0000 00000000 ffffffff 00809300
DS =0000 00000000 ffffffff 00809300
FS =0000 00000000 ffffffff 00809300
GS =ca00 000ca000 ffffffff 00809300
LDT=0000 00000000 0000ffff 00008200
TR =0000 00000000 0000ffff 00008b00
GDT=     00000000 00000000
IDT=     00000000 000003ff
CR0=00000010 CR2=00000000 CR3=00000000 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000 
DR6=ffff0ff0 DR7=00000400
CCS=00000004 CCD=00000001 CCO=EFLAGS  
EFER=0000000000000000
SMM: enter
EAX=000000b5 EBX=000f7bfc ECX=00001234 EDX=00006aff
ESI=00006a1c EDI=3ffbedc5 EBP=000069dc ESP=000069dc
EIP=00007bfb EFL=00000002 [-------] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =db80 000db800 ffffffff 00809300
CS =f000 000f0000 ffffffff 00809b00
SS =0000 00000000 ffffffff 00809300
DS =0000 00000000 ffffffff 00809300
FS =0000 00000000 ffffffff 00809300
GS =ca00 000ca000 ffffffff 00809300
LDT=0000 00000000 0000ffff 00008200
TR =0000 00000000 0000ffff 00008b00
GDT=     00000000 00000000
IDT=     00000000 000003ff
CR0=00000010 CR2=00000000 CR3=00000000 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000 
DR6=ffff0ff0 DR7=00000400
CCS=00000000 CCD=000069dc CCO=EFLAGS  
EFER=0000000000000000
SMM: after RSM
EAX=000000b5 EBX=000f7bfc ECX=00001234 EDX=00006aff
ESI=00006a1c EDI=3ffbedc5 EBP=000069dc ESP=000069dc
EIP=000f7bfc EFL=00000002 [-------] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
CS =0008 00000000 ffffffff 00c09b00 DPL=0 CS32 [-RA]
SS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
DS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
FS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
GS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
LDT=0000 00000000 0000ffff 00008200 DPL=0 LDT
TR =0000 00000000 0000ffff 00008b00 DPL=0 TSS32-busy
GDT=     000f6280 00000037
IDT=     000f62be 00000000
CR0=00000011 CR2=00000000 CR3=00000000 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000 
DR6=ffff0ff0 DR7=00000400
CCS=00000000 CCD=00000000 CCO=EFLAGS  
EFER=0000000000000000
SMM: enter
EAX=000000b5 EBX=00007c16 ECX=00005678 EDX=00000003
ESI=3feccb00 EDI=3ffbedc5 EBP=000069dc ESP=000069dc
EIP=000f7c15 EFL=00000012 [----A--] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
CS =0008 00000000 ffffffff 00c09b00 DPL=0 CS32 [-RA]
SS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
DS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
FS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
GS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
LDT=0000 00000000 0000ffff 00008200 DPL=0 LDT
TR =0000 00000000 0000ffff 00008b00 DPL=0 TSS32-busy
GDT=     000f6280 00000037
IDT=     000f62be 00000000
CR0=00000011 CR2=00000000 CR3=00000000 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000 
DR6=ffff0ff0 DR7=00000400
CCS=00000010 CCD=000069c8 CCO=EFLAGS  
EFER=0000000000000000
SMM: after RSM
EAX=000000b5 EBX=00007c16 ECX=00005678 EDX=00000003
ESI=3feccb00 EDI=3ffbedc5 EBP=000069dc ESP=000069dc
EIP=00007c16 EFL=00000002 [-------] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =db80 000db800 ffffffff 00809300
CS =f000 000f0000 ffffffff 00809b00
SS =0000 00000000 ffffffff 00809300
DS =0000 00000000 ffffffff 00809300
FS =0000 00000000 ffffffff 00809300
GS =ca00 000ca000 ffffffff 00809300
LDT=0000 00000000 0000ffff 00008200
TR =0000 00000000 0000ffff 00008b00
GDT=     00000000 00000000
IDT=     00000000 000003ff
CR0=00000010 CR2=00000000 CR3=00000000 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000 
DR6=ffff0ff0 DR7=00000400
CCS=00000000 CCD=00000001 CCO=EFLAGS  
EFER=0000000000000000
check_exception old: 0xffffffff new 0xe
     0: v=0e e=0000 i=0 cpl=0 IP=0008:00101c9e pc=00101c9e SP=0010:0010fc98 CR2=00101c9e
EAX=80000011 EBX=00108790 ECX=000003d5 EDX=000003d5
ESI=08000000 EDI=00000000 EBP=0010fcb0 ESP=0010fc98
EIP=00101c9e EFL=00000086 [--S--P-] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =0010 00000000 ffffffff 00cf9300 DPL=0 DS   [-WA]
CS =0008 00000000 ffffffff 00cf9a00 DPL=0 CS32 [-R-]
SS =0010 00000000 ffffffff 00cf9300 DPL=0 DS   [-WA]
DS =0010 00000000 ffffffff 00cf9300 DPL=0 DS   [-WA]
FS =0010 00000000 ffffffff 00cf9300 DPL=0 DS   [-WA]
GS =0010 00000000 ffffffff 00cf9300 DPL=0 DS   [-WA]
LDT=0000 00000000 0000ffff 00008200 DPL=0 LDT
TR =0000 00000000 0000ffff 00008b00 DPL=0 TSS32-busy
GDT=     0010bc60 0000002f
IDT=     0010b040 000007ff
CR0=80000011 CR2=00101c9e CR3=00114000 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000 
DR6=ffff0ff0 DR7=00000400
CCS=00000010 CCD=80000011 CCO=LOGICL  
EFER=0000000000000000
check_exception old: 0xe new 0xe
     1: v=08 e=0000 i=0 cpl=0 IP=0008:00101c9e pc=00101c9e SP=0010:0010fc98 env->regs[R_EAX]=80000011
EAX=80000011 EBX=00108790 ECX=000003d5 EDX=000003d5
ESI=08000000 EDI=00000000 EBP=0010fcb0 ESP=0010fc98
EIP=00101c9e EFL=00000086 [--S--P-] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =0010 00000000 ffffffff 00cf9300 DPL=0 DS   [-WA]
CS =0008 00000000 ffffffff 00cf9a00 DPL=0 CS32 [-R-]
SS =0010 00000000 ffffffff 00cf9300 DPL=0 DS   [-WA]
DS =0010 00000000 ffffffff 00cf9300 DPL=0 DS   [-WA]
FS =0010 00000000 ffffffff 00cf9300 DPL=0 DS   [-WA]
GS =0010 00000000 ffffffff 00cf9300 DPL=0 DS   [-WA]
LDT=0000 00000000 0000ffff 00008200 DPL=0 LDT
TR =0000 00000000 0000ffff 00008b00 DPL=0 TSS32-busy
GDT=     0010bc60 0000002f
IDT=     0010b040 000007ff
CR0=80000011 CR2=0010b0b0 CR3=00114000 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000 
DR6=ffff0ff0 DR7=00000400
CCS=00000010 CCD=80000011 CCO=LOGICL  
EFER=0000000000000000
check_exception old: 0x8 new 0xe
I have realized that it causes triple fault when setting bit 31 in CR0.

Re: Got stuck when mapping kernel to lower-half

Posted: Fri Mar 11, 2022 9:39 am
by thewrongchristian
NeonLightions wrote:
Octocontrabass wrote:Which exception occurs that causes the triple fault? What is the CPU state when this exception occurs? (Add "-d int" to your QEMU command line to get this information.)
This is the CPU state (quite long):

Code: Select all

...
check_exception old: 0xffffffff new 0xe
     0: v=0e e=0000 i=0 cpl=0 IP=0008:00101c9e pc=00101c9e SP=0010:0010fc98 CR2=00101c9e
EAX=80000011 EBX=00108790 ECX=000003d5 EDX=000003d5
ESI=08000000 EDI=00000000 EBP=0010fcb0 ESP=0010fc98
EIP=00101c9e EFL=00000086 [--S--P-] CPL=0 II=0 A20=1 SMM=0 HLT=0
...
CR0=80000011 CR2=00101c9e CR3=00114000 CR4=00000000
I have realized that it causes triple fault when setting bit 31 in CR0.
Exception 0xe is a page fault.

CR2=00101c9e matches EIP=00101c9e, so your code is not mapped in the page table, and when you turn paging on, you can no longer read your instruction stream.

Before enabling paging, you need to identity map the region of code executing.

If you're triple faulting, you probably haven't mapped your stack either.

Re: Got stuck when mapping kernel to lower-half

Posted: Fri Mar 11, 2022 8:09 pm
by NeonLightions
thewrongchristian wrote:
NeonLightions wrote:
Octocontrabass wrote:Which exception occurs that causes the triple fault? What is the CPU state when this exception occurs? (Add "-d int" to your QEMU command line to get this information.)
This is the CPU state (quite long):

Code: Select all

...
check_exception old: 0xffffffff new 0xe
     0: v=0e e=0000 i=0 cpl=0 IP=0008:00101c9e pc=00101c9e SP=0010:0010fc98 CR2=00101c9e
EAX=80000011 EBX=00108790 ECX=000003d5 EDX=000003d5
ESI=08000000 EDI=00000000 EBP=0010fcb0 ESP=0010fc98
EIP=00101c9e EFL=00000086 [--S--P-] CPL=0 II=0 A20=1 SMM=0 HLT=0
...
CR0=80000011 CR2=00101c9e CR3=00114000 CR4=00000000
I have realized that it causes triple fault when setting bit 31 in CR0.
Exception 0xe is a page fault.

CR2=00101c9e matches EIP=00101c9e, so your code is not mapped in the page table, and when you turn paging on, you can no longer read your instruction stream.

Before enabling paging, you need to identity map the region of code executing.

If you're triple faulting, you probably haven't mapped your stack either.
Hi,
I have tried like you said, but I don't know how to map the region of code excuting. Can you help me?

Re: Got stuck when mapping kernel to lower-half

Posted: Sat Mar 12, 2022 7:52 am
by thewrongchristian
NeonLightions wrote:
thewrongchristian wrote:
Exception 0xe is a page fault.

CR2=00101c9e matches EIP=00101c9e, so your code is not mapped in the page table, and when you turn paging on, you can no longer read your instruction stream.

Before enabling paging, you need to identity map the region of code executing.

If you're triple faulting, you probably haven't mapped your stack either.
Hi,
I have tried like you said, but I don't know how to map the region of code excuting. Can you help me?
Identity mapping means configuring your page table such that virtual address X maps to physical address X, so that once your bootstrap code turns on paging, the next instruction (now using virtual addresses) will have the same address as it did before paging.

Here's my code where I set up that identity page table:
https://github.com/TheWrongChristian/TWCOS/blob/trunk/arch/i386/init.c

Code: Select all

extern uint32_t pg_dir[1024];
extern uint32_t pt_00000000[1024];
extern char _kernel_offset_bootstrap[0];
extern char _kernel_offset[0];

BOOTSTRAP_CODE void bootstrap_paging_init()
{
        int i;
        uint32_t offset = _kernel_offset-_kernel_offset_bootstrap;
        pg_dir[0] = pg_dir[offset >> 22] = ((uint32_t)pt_00000000) | 0x3;
        for(i=0; i<1024; i++) {
                pt_00000000[i] = (i * 4096) | 0x3;
        }
}
Here:
  • _kernel_offset_bootstrap => Label at the start of my kernel, as loaded by the boot loader from address (physical) 0x100000 (1MB).
  • _kernel_offset => Label at the start of my kernel, but mapped into high memory.
  • offset => The difference between the two labels above, and indicates the boundary between user space and kernel space. In my kernel, the kernel maps at 0xf0100000, so offset is 0xf0000000.
  • pg_dir => Array of 1024 page directory entries. Statically allocated in my kernel image.
  • pt_00000000 => Array of 1024 page table entries. Statically allocated in my kernel image.
On kernel startup, before I turn on paging, I initialise pg_dir and pt_00000000, and reference pt_00000000 at page directory 0 (which maps 4MB from virtual address 0), and page directory 960 (which maps 4MB from virtual address 0xf0000000).

The result being, that the kernel code loaded is mapped at both physical and virtual address 0x100000, so when I turn on paging, $epc remains valid, it's just a virtual address now instead of a physical address. Additionally, my kernel is also mapped at 0xf0100000, so I can jump directly into my higher address mapped kernel, and continue bootstrapping the kernel in its final location.

As part of that final initialisation, I craft the first thread, and allocate an actual dynamic page table, leaving behind the static bootstrap page table I used above.

You could also statically initialise the static page table structures, but I prefer to initialise mine as above because:
  • The initialisation is formulaic. It's less code to write than it is to define the data. Yes, I could generate the initialisation data quite easily, but I'd have to write code to do that, so I might was well write code to initialise it directly.
  • The user/kernel boundary is defined in a single place, the linked script. All the other addresses are driven from the addresses the linker script defines. I'm not sure how I would handle that in statically defined C code, though I'm sure it's possible.