Page 1 of 1

software driven paging

Posted: Sun Aug 05, 2007 6:18 am
by Ninjarider
over the past couple days i have been trying to enable paging. havn't been able to make it into paging yet successfully following the intel manuals. was wondering if anybody has has something a software version of paging in there operating system because im thing about making paging software based.

also is there any way to get segmentation and paging working together on a hardware level/

Re: software driven paging

Posted: Sun Aug 05, 2007 9:00 am
by SpooK
Ninjarider wrote:over the past couple days i have been trying to enable paging. havn't been able to make it into paging yet successfully following the intel manuals. was wondering if anybody has has something a software version of paging in there operating system because im thing about making paging software based.

also is there any way to get segmentation and paging working together on a hardware level/
Believe me, you do not want to add that kind of overhead even if you could.

Just keep reading and keep searching this site and you will get it eventually. If you are getting too frustrated, take a short break from your project and/or post the failing code here for assistance.

Posted: Sun Aug 05, 2007 12:30 pm
by JAAman
also is there any way to get segmentation and paging working together on a hardware level
it always is* (segmentation is not disabled when paging is enabled, it cannot be disabled at all) -- that is, if you had asked whether it was possible to use paging without segmentation, the answer would be no

however most people try hard to avoid using both, because it becomes needlessly complicated trying to do so

paging takes some time to understand and set up properly, but once you understand it, its really quite simple


*note: this is assuming PMode -- for LMode segmentation is effectively disabled, and cannot be re-enabled

Posted: Sun Aug 05, 2007 7:11 pm
by Gizmo
Paging isnt too hard to setup, you (assuming you are using default 4k pages) setup a page directory with 1024 entries. These entries each contain a pointer and page flags, each entry in the directory points to a page table. Page tables are almost exactly the same as a directory, except the entries point to pages, which are basically imaginary 4k blocks of physical memory.

The logical address you use when you use paging is
10 bits page table entry in directory
10 bits page entry in table
12 bits offset in page
...wich the cpu turns into another logical address used by segmentation to finally make a physical address.
If your segments use a flat address area (0-4gb addresses, 2 segments code and data) then you can pretend segments don't exist (just remeber to set ds and ss at least once).

The entires in the page tables and directory contain an address and flags.
format:

Code: Select all

bits     purpose
12-31   address
11-9 available for whatever you want to use here
8      global bit -ignored
7      page size- 0 for 4k
6      reserved leave 0
5      acessed, use to identify a page that has been unused since last time you checked
4      disables cach, leave 0
3      write through? leave 0
2      1 for user, 0 for supervisor (1 keeps userland from modifing)
1      r/w 1 for write, 0 is read only
0      present, if this is unset this page loaded your os hasnt allocated a place in physical ram and mapped it to this page yet- you load the page from hard drive to a free address you havent used and set the address and present parts.
When the present bit is not set you can put whatever in the rest of the entry- its available for programmer use (store offset in page file that contains the data for this page etc).

Here is some code, its not complete, it just sets pages for every page in a 4gb address space flat with no page file(all pages are loaded- if you access memory thats not installed the data stored there is always 0).

Code: Select all

       ;page directory list tables 1024 entries * 4 bytes = 4096 bytes 1000h
	     ;137000-138000
	     K_PAGE_DIRS	 equ 137000h
	     K_PAGE_DIRS_MAX	 equ 1024
	     K_PAGE_DIRS_END	 equ 138000h
       ;page tables 1024 tables * 1024 entries * 4 bytes = 4,194,304 bytes  40000h
	     ;138000-178000
	     K_PAGE_TABLES	 equ 138000h
	     K_PAGE_TABLES_END	 equ 178000h
	     K_PAGE_TABLES_MAX	 equ 1024

use32

	;setup pages
	       call MAKE_KERNEL_PAGES
	;enabling paging
	       mov eax, K_PAGE_DIRS
	       mov cr3, eax
	       mov eax, cr0
	       or  eax, 0x80000000
	       mov cr0, eax


Code: Select all

     ;creates the kernel page directory and tables
	 use32
	 MAKE_KERNEL_PAGES:
	   ;save all regs
		pusha
	   ;set start addresses
		mov eax, K_PAGE_DIRS
		mov ebx, K_PAGE_TABLES
		mov edx, 0
	   ;create directory entries
	   	.kpage_loopdir:
	       ;save table base pointer
			push ebx
			or ebx, 3
			mov [ds:eax], ebx
			pop ebx
	     ;create table entries
			mov ecx, 0
		.kpage_looptable:
		;store directory entry pointer
			 push eax
		;get table entry pointer
			mov eax, ecx
			add eax, ebx
		;save entry
			 push edx
			 or edx, 3
			 mov [ds:eax], edx
			 pop edx
		;restore directory entry pointer
			pop eax
		;increment table entry pointer
			add ecx, 4
		;increment physical address
			 add edx, 4096
		;if not at end of table continue
			 cmp ecx, 4096
			 jl .kpage_looptable
	      ;increment directory entry pointer
		    add eax, 4
	      ;increment page table pointer
		    add ebx, 4096
	      ;if not at end of directory, continue
		    cmp eax, K_PAGE_DIRS_END
		    jl .kpage_loopdir
	   ;restore regs and return to caller
		popa
		ret
That will get you started but you would need to avoid just setting them all to present, and use a method for showing where you have and havent allocated memory yet. I use a "bitmap" approach with 4mb granularity, each of my blocks (units of allocation) is 1000 pages large. Its so I can use all page size extensions (4k, 2m, and 4m) without using separate allocation structures. Its not perfect, as I am still a os dev newb. But that should get you started with atleast a working example.

ps: oh yea, make sure your page directory and tables are on 4kb boundries- ie the addresses end in xxx000h (4096 = 1000h)

Re: software driven paging

Posted: Mon Aug 06, 2007 5:17 am
by AndrewAPrice
Ninjarider wrote:over the past couple days i have been trying to enable paging. havn't been able to make it into paging yet successfully following the intel manuals. was wondering if anybody has has something a software version of paging in there operating system because im thing about making paging software based.

also is there any way to get segmentation and paging working together on a hardware level/
Software segmentation. Or the second best thing: position independent code.

Posted: Mon Aug 06, 2007 7:44 am
by Ninjarider
thnx all
thnx especially for the asm code.

i was thinking about doing something simular to a basic bitmap of it. but i was also thinking about also having information about the process or thread the required the memory so that the system can cleanup memory. - any thoughts about this

Posted: Mon Aug 06, 2007 12:25 pm
by Gizmo
Ninjarider wrote:thnx all
thnx especially for the asm code.

i was thinking about doing something simular to a basic bitmap of it. but i was also thinking about also having information about the process or thread the required the memory so that the system can cleanup memory. - any thoughts about this
Your welcome.

You can make a byte or word map, where each byte or byte pair contains a task id. If task id is 0 then its free, if not, then the task id must be a task that is currently running- if not then your os should free it (set to 0 since the task has already been killed by your os).

Another popular method is creating a stack of blocks of allocation. You maintain free blocks in a stack so when a task request allocation, you remove free blocks from the stack and they wont be used. Instead of storing a task id you would store a physical address. Basically, the amount of memory required to hold the stack decreases as memory use increases, but if the task doesn't free what it uses and you don't keep a stack per task of blocks in use you could get system wide memory leaks.

So basically you have to have a page directory and 1024 tables as well as structures used by your os to know where pages havent been assigned to yet. Your blocks of allocation should be in order (0-4gb) but the pages can point to addresses in any order (fragmentation- first come first serve). Your units of allocation can be any size, but you should use multiples of your page sizes.

Posted: Sat Feb 09, 2008 3:20 pm
by packet50071
I know its kind of Old post but still can you point me to that Intel manual pls

Posted: Sat Feb 09, 2008 3:51 pm
by bloodhound23
check the resources in the Wiki. I put the AMD manuals for x86-64 and one of the links go to the Intel Manuals.

Posted: Sat Feb 09, 2008 5:03 pm
by packet50071
[ edit : I found the resource page ] but
i can find only 64 no 32 :(

Posted: Sat Feb 09, 2008 5:05 pm
by Combuster
packet50071 wrote:and where is that resource page ??
Have you actually looked? Resources

Posted: Sat Feb 09, 2008 5:20 pm
by bloodhound23
It's an all in one. Look at the intel manuals for 32 and some on 64 and AMD for 64 and some on 32