I got stuck in setting the base & limit for stack protection .
Can some one explain the idea of Expand down stack segment please? I dont get enough information from Intel's System Programming Guide 1.
I want to use expand down stack from address 0xffff,0000 to 0xfffe,0000 ( 64KB) in kernel mode. G (Granularity flag) bit is set to 1 for 4KB increments.
D/B is 1, making operation size as 32 bit pointer.
What should be the value of Segment Descriptor for Stack segment with above required information? Is it related with Real & Protected mode?
Code: Select all
void Init_MicroKernel(){ // entry function for kernel
gdt_install(); // 1st line in this function
}
void setkgdt(uint32 index, uint16 low, uint8 mid, uint8 high, uint16 limit, uint8 access, uint8 gran) {
kgdt[index].seglimit = limit;
kgdt[index].lowbase = low;
kgdt[index].midbase = mid;
kgdt[index].access = access;
kgdt[index].granularity = gran;
kgdt[index].highbase = high;
}
void gdt_install() {
desc gdtp;
setkgdt(0, 0, 0, 0, 0, 0, 0);
setkgdt(1, 0, 0, 0, 0xFFFF, 0x9B, 0xCF); // code at 0x8
setkgdt(2, 0, 0, 0, 0xFFFF, 0x93, 0xCF); // data
setkgdt(3, 0x0, 0xFF, 0xFF, 0xFFFD, 0x97, 0xCF); // stack
setkgdt(4, 0, 0, 0, 0, 0, 0);
gdtp.limit = (sizeof (gdt_entry) * MAX_GDT_ENTRIES) - 1;
gdtp.base = (uint32)kgdt;
InstallGdt((uint32)&gdtp);
}
InstallGdt:
mov 4(%esp), %eax
lgdt (%eax)
mov $DATA_SEL, %ax /* DATA_SEL equals =0x10 */
mov %ax, %ds
mov %ax, %ss
mov %ax, %es
mov %ax, %fs
mov %ax, %gs
.byte 0xEA
.long done
.word CODE_SEL /* CODE_SEL equals =0x8 */
done:
ret
Thanks