Home arrow Forums
OSDEV Forums  


anonymous
Visitor

Fresh Osdever
Posts: 0
graphgraph
Karma: 0  
Memory map and GDT - 2006/01/18 20:24 Hi, I started writing an OS and need help with setting the GDT.

Got a working bootloader, which switches to p-mode, loads and jumps to the kernel. At the moment i have just set my GDT to use ring0 and the entire address space. And then continued writing my OS with this configuration... However I would like to know how to set up the GDT in a proper way, so exceptions are thrown when an user application tries to access kernel space.

- Which areas are typically used for kernel / user space ?

- Where can I find a complete memory map, so I can see which areas that are reserved?

- As I write on the kernel its size grows. Is it possible to set the size of the kernel space (ring0) in the GDT from the linker script or do I manually have to change this?

Thanks alot
  | | The administrator has disabled public write access.
OSDEV
Community
Advertisement
   
gaf
User

Platinum Osdever
Posts: 153
graph
Karma: 10  
Re: Memory map and GDT - 2006/01/20 05:51 Hello,
and welcome to the message board..

Which areas are typically used for kernel / user space ?
Almost all modern operating systems use paging for memory management and assign the first 2gb (sometimes 3gb) of virtual address-space to the current user-task while the kernel resides in the upper 2gb.

Where can I find a complete memory map, so I can see which areas that are reserved?
If you want to know what's going on in the very first mb of memory you should have a look at ralf brown's interrupt list. To get an idea which parts of the memory area above the one megabyte mark may be used you can ask the BIOS for a memory map (here).

As I write on the kernel its size grows. Is it possible to set the size of the kernel space (ring0) in the GDT from the linker script or do I manually have to change this?
This could be done by declaring symbols in your ld-script, which might then be used in you code, but I discourage you from doing so. If you really have to change a descriptor's size you might aswell do so at run-time by simply writing a procedure that replaces the bits in the gdt and reloads it. In conjuction with paging one normally uses a very high limit for the descriptors and maps physical memory as needed.

regards,
gaf
  | | The administrator has disabled public write access.
conner
User

Fresh Osdever
Posts: 1
graphgraph
Karma: 0  
Re: Memory map and GDT - 2006/02/04 19:01 Thanks gaf!

Decided to load my kernel to 16mb physical address to avoid conflicts with DMA and put system stack and GDT in parts of the first MB of physical RAM, that are not used. Going to split virtual space for user/kernel to 3gb/1gb. Also I decided to go for Intel's sysenter/sysexit for performance reasons.

However another problem have arisen. I had the idea that you could protect user processes(ring3) from accessing each others memory by adding an entry for each process to the GDT. However sysenter/sysexit requires the GDT to have to form:

Level0 Code Segment Descriptor
Level0 Stack Segment Descriptor
Level3 Code Segment Descriptor
Level3 Stack Segment Descriptor

Does this means that I have to use LDTs for protecting user processes from each other?
  | | The administrator has disabled public write access.
gaf
User

Platinum Osdever
Posts: 153
graph
Karma: 10  
Re: Memory map and GDT - 2006/02/05 09:25 Hello,
If you go for a flat-mode kernel, protection is not provided by segmentation (descriptors in the gdt/ldt) but only by the paging mechanism. The values defined in the gdt entries are virtual addresses and every time a memory reference is made by a task, the cpu looks up the address in the page-directory/page-table to get the corresponding physical address. To make sure that a task can't access memory hold by another task, all you have to do is to use one page-directory per process, and make sure that no two tasks have the same physical address mapped to their virtual address-space.

Code:
page-directory task[0]:

v-addr p-addr
0x00000000 0x01000000 (physical = 16M
0x00001000 0x01001000 (next page = page+4096)
....
0x000FC000 0x010FC000 (last page = 17M

page-directory task[1]:
v-addr p-addr
0x00000000 0x01100000 (physical = 17M
0x00001000 0x01101000 (next page = page+4096)
....
0x000FC000 0x011FC000 (last page = 18M

task[0]: mov [0x00000100] -> phys 0x01000100
task[1]: mov [0x00000100] -> phys 0x01100100


regards,
gaf
  | | The administrator has disabled public write access.

A WebArticles site. Sponsored by Evoleto. Motorola V525 / Business Directory / Delaware Incorporation / Home Made Bazaar