neon wrote:Option (1) is required if using 32 bit protected mode, optional for 64 bit. Option (2) is most used in 64 bit.
I will note here that Linux on i386 uses option (2) for the most part. It linearly maps the
start of physical memory to the 3GB line, and does so for the first 768 MB (assuming default configuration. You can change this if you really want to). So that leaves 256MB of kernel virtual memory untouched. In these first 768MB, it places the kernel image itself (which has to be loaded to the 1MB line), all of the things it has to know the physical address of without translation (so all the paging structures for all the tasks), and also everything that can't have its virtual address change after allocation. The final 256MB of virtual space are managed by an allocator, and are used for things like fixed I/O mappings (e.g. the LAPIC has its physical address just below 4GB, and must be accessed there). This block is called "high memory". When the allocator is called, it can be told whether it is OK to put an object there, and it will try to do so. But if memory runs out on high memory, it will start allocating low memory instead. This also handles the case of a PC with less than 768MB of memory, because a lot of low memory will be unused with just the things mentioned so far.
Since the mappings on the high 256MB are constantly switched out, a good balance between memory utilization and performance must be found.
In order to get started, you must decide on a scheme to write to your page tables without having a virtual address for them. With recursive mapping, you do that by giving them fixed virtual addresses, with a linear mapping you do that by giving them an address that is easily computed from the physical address. Once you have that started, I would suggest having your bootloader initialize the scheme for you, then just using it.