I have read various post regarding how to load a new gdt in long mode when in kernel but I am confused about how the EFLAG should be set. According to this post (the one by StephanvanSchaik) we should restore the old flag, unset a few bits in the old flag and save that as the current flag then push the old flag back with execution of iretq (that's how I understand the code below). I am not quite understanding the reasoning behind that (why not just use old flag values?). On top of that I have found a few other code sample where it just sets EFLAG to value 2 or some other random value and it seems to work at least on my kernel. Could someone enlighten me on how the EFLAG should be set when I am using iretq? Below is the a repost of the code StephanvanSchaik wrote:
Code: Select all
mov rsp, 0x90000
lgdt [GDT.Pointer]
push QWORD GDT.KData
push QWORD 0x90000
pushfq
pushfq
pop rax
and rax, 1111111111111111111111111111111111111111111111101011111011111111b
push rax
popfq
push QWORD GDT.KCode
push QWORD .Flush
iretq
.Flush:
mov ax, GDT.KData
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax