UbixOS  2.0
paging.c
Go to the documentation of this file.
1 /*-
2  * Copyright (c) 2002-2018 The UbixOS Project.
3  * All rights reserved.
4  *
5  * This was developed by Christopher W. Olsen for the UbixOS Project.
6  *
7  * Redistribution and use in source and binary forms, with or without modification, are permitted
8  * provided that the following conditions are met:
9  *
10  * 1) Redistributions of source code must retain the above copyright notice, this list of
11  * conditions, the following disclaimer and the list of authors.
12  * 2) Redistributions in binary form must reproduce the above copyright notice, this list of
13  * conditions, the following disclaimer and the list of authors in the documentation and/or
14  * other materials provided with the distribution.
15  * 3) Neither the name of the UbixOS Project nor the names of its contributors may be used to
16  * endorse or promote products derived from this software without specific prior written
17  * permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
20  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/types.h>
30 #include <vmm/vmm.h>
31 #include <lib/kprintf.h>
32 #include <lib/kmalloc.h>
33 #include <ubixos/kpanic.h>
34 #include <ubixos/sched.h>
35 #include <ubixos/spinlock.h>
36 #include <string.h>
37 #include <assert.h>
38 #include <sys/descrip.h>
39 #include <ubixos/vitals.h>
40 
41 uint32_t *kernelPageDirectory = 0x0; // Pointer To Kernel Page Directory
42 
43 static struct spinLock fkpSpinLock = SPIN_LOCK_INITIALIZER;
44 
45 static struct spinLock rmpSpinLock = SPIN_LOCK_INITIALIZER;
46 
47 /*****************************************************************************************
48  Function: int vmm_pagingInit();
49 
50  Description: This Function Will Initialize The Operating Systems Paging
51  Abilities.
52 
53  Notes:
54  02/20/2004 - Looked Over Code And Have Approved Its Quality
55  07/28/3004 - All pages are set for ring-0 only no more user accessable
56 
57  *****************************************************************************************/
58 
59 int vmm_pagingInit() {
60  uint32_t i = 0x0;
61  uint32_t *pageTable = 0x0;
62 
63  /* Allocate A Page Of Memory For Kernels Page Directory */
65 
66  if (kernelPageDirectory == 0x0) {
67  K_PANIC("Error: vmm_findFreePage Failed");
68  } /* end if */
69 
70  /* Clear The Memory To Ensure There Is No Garbage */
72 
73  /* Allocate a page for the first 4MB of memory */
74  if ((pageTable = (uint32_t *) vmm_findFreePage( sysID)) == 0x0)
75  K_PANIC("Error: vmm_findFreePage Failed");
76 
77  /* Make Sure The Page Table Is Clean */
78  bzero(pageTable, PAGE_SIZE);
79 
80  kernelPageDirectory[0] = (uint32_t) ((uint32_t) (pageTable) | PAGE_DEFAULT);
81 
82  /*
83  * Map the first 1MB of Memory to the kernel MM space because our kernel starts
84  * at 0x30000
85  * Do not map page at address 0x0 this is reserved for null...
86  */
87 
88  /* MrOlsen (2016-01-15) NOTE: I'm Mapping It For Now Until I Can Figure Out Why FS:0x0 */
89  for (i = 0x0; i < (PD_ENTRIES / 0x4); i++) {
90  pageTable[i] = (uint32_t) ((i * 0x1000) | PAGE_DEFAULT); //FIXME: This is temp becauseo f bios callKERNEL_PAGE_DEFAULT); //MrOlsen 2018-01-14 PAGE_DEFAULT
91  } /* end for */
92 
93  /* Allocate a page for the second 4MB of memory */
94  if ((pageTable = (uint32_t *) vmm_findFreePage( sysID)) == 0x0)
95  K_PANIC("Error: vmm_findFreePage Failed");
96 
97  /* Make Sure The Page Table Is Clean */
98  bzero(pageTable, PAGE_SIZE);
99 
100  kernelPageDirectory[1] = (uint32_t) ((uint32_t) (pageTable) | PAGE_DEFAULT);
101 
102  /*
103  * Create page tables for the top 1GB of VM space. This space is set aside
104  * for kernel space and will be shared with each process
105  */
106 
107  for (i = PD_INDEX(VMM_KERN_START); i <= PD_INDEX(VMM_KERN_END); i++) {
108  if ((pageTable = (uint32_t *) vmm_findFreePage( sysID)) == 0x0)
109  K_PANIC("Error: vmm_findFreePage Failed");
110 
111  /* Make Sure The Page Table Is Clean */
112  bzero(pageTable, PAGE_SIZE);
113 
114  /* Map In The Page Directory */
116  } /* end for */
117 
118  kernelPageDirectory[1023] = (uint32_t) ((uint32_t) (pageTable) | KERNEL_PAGE_DEFAULT);
119  pageTable = (uint32_t *) (kernelPageDirectory[1023] & 0xFFFFF000);
120 
121  pageTable[1023] = (vmm_findFreePage(sysID) | KERNEL_PAGE_DEFAULT | PAGE_STACK);
122  pageTable[1022] = (vmm_findFreePage(sysID) | KERNEL_PAGE_DEFAULT | PAGE_STACK);
123 
124  /*
125  * Map Page Tables Into VM Space
126  * The First Page Table (4MB) Maps To All Page Directories
127  */
129  if ((pageTable = (uint32_t *) vmm_findFreePage( sysID)) == 0x0)
130  K_PANIC("Error: vmm_findFreePage Failed");
131 
132  bzero(pageTable, PAGE_SIZE);
133 
135  }
136 
137  pageTable = (uint32_t *) (kernelPageDirectory[PD_INDEX(PT_BASE_ADDR)] & 0xFFFFF000);
138 
139  for (i = 0; i < PD_ENTRIES; i++) {
140  pageTable[i] = kernelPageDirectory[i];
141  } /* end for */
142 
143  /*
144  * Map Page Directory Into VM Space
145  * First Page After Page Tables
146  */
148  if ((pageTable = (uint32_t *) vmm_findFreePage( sysID)) == 0x0)
149  K_PANIC("Error: vmm_findFreePage Failed");
150 
151  bzero(pageTable, PAGE_SIZE);
152 
154  }
155 
156  pageTable = (uint32_t *) (kernelPageDirectory[PD_INDEX(PD_BASE_ADDR)] & 0xFFFFF000);
157  pageTable[0] = (uint32_t) ((uint32_t) (kernelPageDirectory) | KERNEL_PAGE_DEFAULT);
158 
159  /* Allocate New Stack Space */
160  if ((pageTable = (uint32_t *) vmm_findFreePage(sysID)) == 0x0)
161  K_PANIC("ERROR: vmm_findFreePage Failed");
162 
163  /* Now Lets Turn On Paging With This Initial Page Table */
164  asm volatile(
165  "movl %0,%%eax \n"
166  "movl %%eax,%%cr3 \n"
167  "movl %%cr0,%%eax \n"
168  "orl $0x80010000,%%eax \n" /* Turn on memory protection */
169  "movl %%eax,%%cr0 \n"
170  :
171  : "d"((uint32_t *) (kernelPageDirectory))
172  );
173 
174  /* Remap The Memory List */
175  for (i = VMM_MMAP_ADDR_RMODE; i <= (0x101000 + (numPages * sizeof(mMap))); i += 0x1000) {
176  if ((vmm_remapPage(i, (VMM_MMAP_ADDR_PMODE + (i - 0x101000)), PAGE_DEFAULT, sysID, 0)) == 0x0)
177  K_PANIC("vmmRemapPage failed\n");
178  }
179 
180  /* Set New Address For Memory Map Since Its Relocation */
182 
183  /* Print information on paging */
184  kprintf("paging0 - Address: [0x%X], PagingISR Address: [0x%X]\n", kernelPageDirectory, &_vmm_pageFault);
185 
186  /* Return so we know everything went well */
187  return (0x0);
188 } /* END */
189 
190 /*****************************************************************************************
191  Function: int vmmRemapPage(Physical Source,Virtual Destination)
192 
193  Description: This Function Will Remap A Physical Page Into Virtual Space
194 
195  Notes:
196  07/29/02 - Rewrote This To Work With Our New Paging System
197  07/30/02 - Changed Address Of Page Tables And Page Directory
198  07/28/04 - If perms == 0x0 set to PAGE_DEFAULT
199 
200  *****************************************************************************************/
201 int vmm_remapPage(uint32_t source, uint32_t dest, uint16_t perms, pidType pid, int haveLock) {
202 
203  uint16_t destPageDirectoryIndex = 0x0, destPageTableIndex = 0x0;
204  uint32_t *pageDir = 0x0, *pageTable = 0x0;
205 
206  short i = 0x0;
207 
208  if (pid < sysID)
209  kpanic("Invalid PID %i", pid);
210 
211  if (source == 0x0)
212  K_PANIC("source == 0x0");
213 
214  if (dest == 0x0)
215  K_PANIC("dest == 0x0");
216 
217  if (haveLock == 0) {
218  if (dest >= VMM_USER_START && dest <= VMM_USER_END)
219  spinLock(&rmpSpinLock);
220  else
222  }
223 
224  if (perms == 0x0)
225  perms = KERNEL_PAGE_DEFAULT;
226 
227  /* Set Pointer pageDirectory To Point To The Virtual Mapping Of The Page Directory */
228  pageDir = (uint32_t *) PD_BASE_ADDR;
229 
230  /* Get Index Into The Page Directory */
231  destPageDirectoryIndex = PD_INDEX(dest);
232 
233  if ((pageDir[destPageDirectoryIndex] & PAGE_PRESENT) != PAGE_PRESENT) {
234  //kprintf("[NpdI:0x%X]", destPageDirectoryIndex);
235  vmm_allocPageTable(destPageDirectoryIndex, pid);
236  }
237 
238  /* Set Address To Page Table */
239  pageTable = (uint32_t *) (PT_BASE_ADDR + (PAGE_SIZE * destPageDirectoryIndex));
240 
241  /* Get The Index To The Page Table */
242  destPageTableIndex = PT_INDEX(dest);
243 
244  /* If The Page Is Mapped In Free It Before We Remap */
245  if ((pageTable[destPageTableIndex] & PAGE_PRESENT) == PAGE_PRESENT) {
246  kpanic("A Page Is Already Mapped Here: 0x%X:0x%X", dest, destPageTableIndex);
247 
248  if ((pageTable[destPageTableIndex] & PAGE_STACK) == PAGE_STACK)
249  kprintf("Stack Page: [0x%X]\n", dest);
250 
251  if ((pageTable[destPageTableIndex] & PAGE_COW) != PAGE_COW) {
252  kprintf("Page NOT COW\n");
253  kprintf("Page Present: [0x%X][0x%X]", dest, pageTable[destPageTableIndex]);
254  source = 0x0;
255  goto rmDone;
256  }
257 
258  /* Clear The Page First For Security Reasons */
259  freePage(((uint32_t) pageTable[destPageTableIndex] & 0xFFFFF000));
260 
261  }
262 
263  /* Set The Source Address In The Destination */
264  pageTable[destPageTableIndex] = (uint32_t) (source | perms);
265 
266  /* Reload The Page Table; */
267  rmDone: asm volatile(
268  "push %eax \n"
269  "movl %cr3,%eax\n"
270  "movl %eax,%cr3\n"
271  "pop %eax \n"
272  );
273 
274  /* Return */
275  if (haveLock == 0x0) {
276  if (dest >= VMM_USER_START && dest <= VMM_USER_END)
277  spinUnlock(&rmpSpinLock);
278  else
280  }
281 
282  return (source);
283 }
284 
285 /************************************************************************
286 
287  Function: void *vmm_getFreeKernelPage(pidType pid, uint16_t count);
288  Description: Returns A Free Page Mapped To The VM Space
289  Notes:
290 
291  07/30/02 - This Returns A Free Page In The Kernel Space
292 
293  ***********************************************************************/
294 void *vmm_getFreeKernelPage(pidType pid, uint16_t count) {
295  int pdI = 0x0, ptI = 0x0, c = 0, lc = 0;
296 
297  uint32_t *pageDirectory = PD_BASE_ADDR;
298 
299  uint32_t *pageTable = 0x0;
300 
301  uint32_t startAddress = 0x0;
302 
303  /* Lock The Page Dir & Tables For All Since Kernel Space Is Shared */
305 
306  /* Lets Search For A Free Page */
307  for (pdI = PD_INDEX(VMM_KERN_START); pdI <= PD_INDEX(VMM_KERN_END); pdI++) {
308 
309  if ((pageDirectory[pdI] & PAGE_PRESENT) != PAGE_PRESENT)
310  if (vmm_allocPageTable(pdI, pid) == -1)
311  kpanic("Failed To Allocate Page Dir: 0x%X", pdI);
312 
313  /* Set Page Table Address */
314  pageTable = (uint32_t *) (PT_BASE_ADDR + (PAGE_SIZE * pdI));
315 
316  /* Loop Through The Page Table For A Free Page */
317  for (ptI = 0; ptI < PT_ENTRIES; ptI++) {
318 
319  /* Check For Unalocated Page */
320  if ((pageTable[ptI] & PAGE_PRESENT) != PAGE_PRESENT) {
321  if (startAddress == 0x0)
322  startAddress = ((pdI * (PD_ENTRIES * PAGE_SIZE)) + (ptI * PAGE_SIZE));
323  c++;
324  } else {
325  startAddress = 0x0;
326  c = 0;
327  }
328 
329  if (c == count)
330  goto gotPages;
331 
332  }
333  }
334 
335  startAddress = 0x0;
336  goto noPagesAvail;
337 
338  gotPages: for (c = 0; c < count; c++) {
339  if ((vmm_remapPage((uint32_t) vmm_findFreePage(pid), (startAddress + (PAGE_SIZE * c)), KERNEL_PAGE_DEFAULT, pid, 1)) == 0x0)
340  K_PANIC("vmmRemapPage failed: gfkp-1\n");
341 
342  vmm_clearVirtualPage((uint32_t) (startAddress + (PAGE_SIZE * c)));
343 
344  }
345 
346  noPagesAvail: spinUnlock(&pdSpinLock);
347 
348  return (startAddress);
349 }
350 
351 /************************************************************************
352 
353  Function: void vmm_clearVirtualPage(uint32_t pageAddr);
354 
355  Description: This Will Null Out A Page Of Memory
356 
357  Notes:
358 
359  ************************************************************************/
360 int vmm_clearVirtualPage(uint32_t pageAddr) {
361  uint32_t *src = 0x0;
362  int counter = 0x0;
363 
364  /* Set Source Pointer To Virtual Page Address */
365  src = (uint32_t *) pageAddr;
366 
367  /* Clear Out The Page */
368  for (counter = 0x0; counter < PD_ENTRIES; counter++) {
369  src[counter] = (uint32_t) 0x0;
370  }
371 
372  /* Return */
373  return (0x0);
374 }
375 
376 void *vmm_mapFromTask(pidType pid, void *ptr, uint32_t size) {
377  kTask_t *child = 0x0;
378  uint32_t i = 0x0, x = 0x0, y = 0x0, count = ((size + 4095) / 0x1000), c = 0x0;
379  uInt32 dI = 0x0, tI = 0x0;
380  uint32_t baseAddr = 0x0, offset = 0x0;
381  uint32_t *childPageDir = (uint32_t *) 0x5A00000;
382  uint32_t *childPageTable = 0x0;
383  uint32_t *pageTableSrc = 0x0;
384  offset = (uint32_t) ptr & 0xFFF;
385  baseAddr = (uint32_t) ptr & 0xFFFFF000;
386  child = schedFindTask(pid);
387  //Calculate The Page Table Index And Page Directory Index
388  dI = (baseAddr / (1024 * 4096));
389  tI = ((baseAddr - (dI * (1024 * 4096))) / 4096);
390 
391  kprintf("cr3: 0x%X\n", child->tss.cr3);
392  if (vmm_remapPage(child->tss.cr3, 0x5A00000, KERNEL_PAGE_DEFAULT, _current->id, 0) == 0x0)
393  K_PANIC("vmm_remapPage: Failed");
394 
395  for (i = 0; i < PD_ENTRIES; i++) {
396 
397  if ((childPageDir[i] & PAGE_PRESENT) == PAGE_PRESENT) {
398  //kprintf("mapping: 0x%X\n", i);
399  if (vmm_remapPage(childPageDir[i], 0x5A01000 + (i * 0x1000), KERNEL_PAGE_DEFAULT, _current->id, 0) == 0x0)
400  K_PANIC("Returned NULL");
401  }
402  }
403  kprintf("mapping completed\n");
404  //for (x = (_current->oInfo.vmStart / (1024 * 4096)); x < 1024; x++) {
405  for (x = PD_INDEX(VMM_KERN_START); x < PD_INDEX(VMM_KERN_END); x++) {
406  //kpanic("v_mFT");
407  pageTableSrc = (uint32_t *) (PT_BASE_ADDR + (4096 * x));
408 
409  for (y = 0; y < 1024; y++) {
410 
411  //Loop Through The Page Table Find An UnAllocated Page
412  if ((uint32_t) pageTableSrc[y] == (uint32_t) 0x0) {
413 
414  if (count > 1) {
415 
416  for (c = 0; ((c < count) && (y + c < 1024)); c++) {
417 
418  if ((uint32_t) pageTableSrc[y + c] != (uint32_t) 0x0) {
419 
420  c = -1;
421  break;
422 
423  }
424 
425  }
426 
427  if (c != -1) {
428 
429  for (c = 0; c < count; c++) {
430 
431  if ((tI + c) >= 0x1000) {
432 
433  dI++;
434  tI = 0 - c;
435 
436  }
437 
438  if ((childPageDir[dI] & PAGE_PRESENT) == PAGE_PRESENT) {
439  //kprintf("dI: 0x%X\n", dI);
440  childPageTable = (uint32_t *) (0x5A01000 + (0x1000 * dI));
441 
442  if ((childPageTable[tI + c] & PAGE_PRESENT) == PAGE_PRESENT) {
443  if (vmm_remapPage(childPageTable[tI + c], ((x * (1024 * 4096)) + ((y + c) * 4096)), KERNEL_PAGE_DEFAULT, _current->id, 0) == 0x0)
444  K_PANIC("remap == NULL");
445  }
446  }
447 
448  }
449 
450  vmm_unmapPage(0x5A00000, 1);
451 
452  for (i = 0; i < 0x1000; i++) {
453  vmm_unmapPage((0x5A01000 + (i * 0x1000)), 1);
454  }
455 
456  return ((void *) ((x * (1024 * 4096)) + (y * 4096) + offset));
457 
458  }
459 
460  } else {
461 
462  //Map A Physical Page To The Virtual Page
463  childPageTable = (uint32_t *) (0x5A01000 + (0x1000 * dI));
464  if ((childPageDir[dI] & PAGE_PRESENT) == PAGE_PRESENT) {
465  //kprintf("eDI: 0x%X", dI);
466  if ((childPageTable[tI] & PAGE_PRESENT) == PAGE_PRESENT) {
467  if (vmm_remapPage(childPageTable[tI], ((x * (1024 * 4096)) + (y * 4096)), KERNEL_PAGE_DEFAULT, _current->id, 0) == 0x0)
468  K_PANIC("remap Failed");
469  }
470  }
471 
472  //Return The Address Of The Mapped In Memory
473  vmm_unmapPage(0x5A00000, 1);
474 
475  for (i = 0; i < 0x1000; i++) {
476  vmm_unmapPage((0x5A01000 + (i * 0x1000)), 1);
477  }
478 
479  return ((void *) ((x * (1024 * 4096)) + (y * 4096) + offset));
480 
481  }
482 
483  }
484 
485  }
486 
487  }
488 
489  return (0x0);
490 }
491 
492 void *vmm_getFreeMallocPage(uInt16 count) {
493  uInt16 x = 0x0, y = 0x0;
494  int c = 0x0;
495  uint32_t *pageTableSrc = 0x0;
496  uint32_t *pageDirectory = 0x0;
497 
498  pageDirectory = (uint32_t *) PD_BASE_ADDR;
499 
500  spinLock(&fkpSpinLock);
501 
502  /* Lets Search For A Free Page */
503  for (x = PD_INDEX(VMM_KERN_START); x <= PD_INDEX(VMM_KERN_END); x++) {
504  if ((pageDirectory[x] & PAGE_PRESENT) != PAGE_PRESENT) /* If Page Directory Is Not Yet Allocated Allocate It */
506 
507  pageTableSrc = (uint32_t *) (PT_BASE_ADDR + (PAGE_SIZE * x));
508 
509  for (y = 0; y < 1024; y++) {
510  /* Loop Through The Page Table Find An UnAllocated Page */
511  if ((uint32_t) pageTableSrc[y] == (uint32_t) 0x0) {
512  if (count > 1) {
513  for (c = 0; c < count; c++) {
514  if (y + c < 1024) {
515  if ((uint32_t) pageTableSrc[y + c] != (uint32_t) 0x0) {
516  c = -1;
517  break;
518  }
519  }
520  }
521  if (c != -1) {
522  for (c = 0; c < count; c++) {
523  if (vmm_remapPage((uint32_t) vmm_findFreePage( sysID), ((x * 0x400000) + ((y + c) * 0x1000)), KERNEL_PAGE_DEFAULT, sysID, 0) == 0x0)
524  K_PANIC("remap Failed");
525 
526  vmm_clearVirtualPage((uint32_t) ((x * 0x400000) + ((y + c) * 0x1000)));
527  }
528  spinUnlock(&fkpSpinLock);
529  return ((void *) ((x * (PAGE_SIZE * PD_ENTRIES)) + (y * PAGE_SIZE)));
530  }
531  } else {
532  /* Map A Physical Page To The Virtual Page */
533  if (vmm_remapPage((uint32_t) vmm_findFreePage( sysID), ((x * 0x400000) + (y * 0x1000)), KERNEL_PAGE_DEFAULT, sysID, 0) == 0x0)
534  K_PANIC("Failed");
535 
536  /* Clear This Page So No Garbage Is There */
537  vmm_clearVirtualPage((uint32_t) ((x * 0x400000) + (y * 0x1000)));
538  /* Return The Address Of The Newly Allocate Page */
539  spinUnlock(&fkpSpinLock);
540  return ((void *) ((x * (PAGE_SIZE * PD_ENTRIES)) + (y * PAGE_SIZE)));
541  }
542  }
543  }
544  }
545  /* If No Free Page Was Found Return NULL */
546  spinUnlock(&fkpSpinLock);
547  return (0x0);
548 }
549 
550 int obreak(struct thread *td, struct obreak_args *uap) {
551  uint32_t i = 0x0;
552  vm_offset_t old = 0x0;
553  vm_offset_t base = 0x0;
554  vm_offset_t new = 0x0;
555 
556  /*
557  #ifdef _VMM_DEBUG
558  */
559  kprintf("vm_offset_t: [%i]\n", sizeof(vm_offset_t));
560  kprintf("nsize: [0x%X]\n", uap->nsize);
561  kprintf("vm_daddr: [0x%X]\n", td->vm_daddr);
562  kprintf("vm_dsize: [0x%X]\n", td->vm_dsize);
563  kprintf("total: [0x%X]\n", td->vm_daddr + td->vm_dsize);
564  /*
565  #endif
566  */
567 
568  new = round_page((vm_offset_t )uap->nsize);
569 
570  base = round_page((vm_offset_t ) td->vm_daddr);
571 
572  old = base + ctob(td->vm_dsize);
573 
574  if (new < base)
575  K_PANIC("EINVAL");
576 
577  if (new > old) {
578  for (i = old; i < new; i += 0x1000) {
580  K_PANIC("remap Failed");
581  }
582  td->vm_dsize += btoc(new - old);
583  } else if (new < old) {
584  K_PANIC("new < old");
585  td->vm_dsize -= btoc(old - new);
586  }
587 
588  return (0x0);
589 }
590 
592  int x = 0x0;
593  int y = 0x0;
594 
595  uint32_t *pageTableSrc = 0x0;
596  uint32_t *pageDir = 0x0;
597 
598  pageDir = (uint32_t *) PD_BASE_ADDR;
599 
600  for (x = (addr / (PD_ENTRIES * PAGE_SIZE)); x <= PD_INDEX(VMM_USER_END); x++) {
601  if ((pageDir[x] & PAGE_PRESENT) == PAGE_PRESENT) {
602  pageTableSrc = (uint32_t *) (PT_BASE_ADDR + (PAGE_SIZE * x));
603 
604  for (y = 0; y < PT_ENTRIES; y++) {
605  if ((pageTableSrc[y] & PAGE_PRESENT) == PAGE_PRESENT) {
606  if ((pageTableSrc[y] & PAGE_COW) == PAGE_COW) {
607  adjustCowCounter(((uint32_t) pageTableSrc[y] & 0xFFFFF000), -1);
608  pageTableSrc[y] = 0x0;
609  }
610  /*
611  else if ((pageTableSrc[y] & PAGE_STACK) == PAGE_STACK) {
612  //TODO: We need to fix this so we can clean the stack!
613  //kprintf("Page Stack!: 0x%X", (x * 0x400000) + (y * 0x1000));
614  // pageTableSrc[y] = 0x0;
615  //MrOlsen (2016-01-18) NOTE: WHat should I Do Here? kprintf( "STACK: (%i:%i)", x, y );
616  }
617  */
618  else {
619  /*
620  int vmmMemoryMapIndex = ((pageTableSrc[y] & 0xFFFFF000) / 4096);
621  vmmMemoryMap[vmmMemoryMapIndex].cowCounter = 0x0;
622  vmmMemoryMap[vmmMemoryMapIndex].pid = vmmID;
623  vmmMemoryMap[vmmMemoryMapIndex].status = memAvail;
624  systemVitals->freePages++;
625  */
626  pageTableSrc[y] = 0x0;
627  }
628  }
629  }
630  }
631  }
632 
633  asm(
634  "movl %cr3,%eax\n"
635  "movl %eax,%cr3\n"
636  );
637 
638  #ifdef VMM_DEBUG
639  kprintf("Here!?");
640  #endif
641 
642  return (0x0);
643 }
taskStruct
Definition: sched.h:62
spinlock.h
kernelPageDirectory
uint32_t * kernelPageDirectory
Definition: paging.c:41
vmmMemoryMap
mMap * vmmMemoryMap
Definition: vmm_memory.c:47
uInt32
unsigned long int uInt32
Definition: objgfx30.h:49
VMM_MMAP_ADDR_PMODE
#define VMM_MMAP_ADDR_PMODE
Definition: vmm.h:50
freePage
int freePage(uint32_t pageAddr)
Definition: vmm_memory.c:262
PAGE_COW
#define PAGE_COW
Definition: paging.h:64
K_PANIC
#define K_PANIC(msg)
Definition: kpanic.h:32
string.h
uInt16
unsigned short int uInt16
Definition: objgfx30.h:48
ctob
#define ctob(x)
Definition: paging.h:74
vmm_getFreeKernelPage
void * vmm_getFreeKernelPage(pidType pid, uint16_t count)
Definition: paging.c:291
thread
Definition: thread.h:40
assert.h
vm_offset_t
__uint32_t vm_offset_t
Definition: types.h:120
spinUnlock
void spinUnlock(spinLock_t *lock)
Definition: spinlock.c:36
vmm.h
round_page
#define round_page(x)
Definition: paging.h:72
vmm_getFreeMallocPage
void * vmm_getFreeMallocPage(uInt16 count)
Definition: paging.c:488
adjustCowCounter
int adjustCowCounter(uint32_t baseAddr, int adjustment)
taskStruct::tss
struct tssStruct tss
Definition: sched.h:67
SPIN_LOCK_INITIALIZER
#define SPIN_LOCK_INITIALIZER
Definition: spinlock.h:36
sched.h
kpanic
void kpanic(const char *fmt,...)
print panic message and halt system
Definition: kpanic.c:41
types.h
bzero
#define bzero(buf, size)
Definition: gpt.h:37
VMM_KERN_START
#define VMM_KERN_START
Definition: vmm.h:64
mMap
Definition: vmm.h:102
btoc
#define btoc(x)
Definition: paging.h:75
obreak_args::nsize
char * nsize
Definition: sysproto_posix.h:419
vmm_unmapPage
void vmm_unmapPage(uint32_t, unmapFlags_t)
Definition: unmappage.c:47
spinLock
void spinLock(spinLock_t *lock)
Definition: spinlock.c:55
uint16_t
__uint16_t uint16_t
Definition: types.h:45
kpanic.h
taskStruct::id
pidType id
Definition: sched.h:63
vmm_findFreePage
uint32_t vmm_findFreePage(pidType pid)
Definition: vmm_memory.c:221
schedFindTask
kTask_t * schedFindTask(uInt32 id)
Definition: sched.c:207
PT_ENTRIES
#define PT_ENTRIES
Definition: paging.h:49
vmm_cleanVirtualSpace
int vmm_cleanVirtualSpace(uint32_t addr)
Definition: paging.c:587
kprintf.h
PAGE_GLOBAL
#define PAGE_GLOBAL
Definition: paging.h:62
vmm_remapPage
int vmm_remapPage(uint32_t source, uint32_t dest, uint16_t perms, pidType pid, int haveLock)
Definition: paging.c:199
PAGE_STACK
#define PAGE_STACK
Definition: paging.h:65
PD_INDEX
#define PD_INDEX(v_addr)
Definition: paging.h:40
vitals.h
PAGE_DEFAULT
#define PAGE_DEFAULT
Definition: paging.h:68
pdSpinLock
struct spinLock pdSpinLock
Definition: vmm_init.c:33
PD_BASE_ADDR
#define PD_BASE_ADDR
Definition: paging.h:45
uint32_t
__uint32_t uint32_t
Definition: types.h:46
obreak_args
Definition: sysproto_posix.h:417
thread::vm_dsize
u_long vm_dsize
Definition: thread.h:44
vmm_clearVirtualPage
int vmm_clearVirtualPage(uint32_t pageAddr)
Definition: paging.c:356
_current
kTask_t * _current
Definition: sched.c:50
PT_INDEX
#define PT_INDEX(v_addr)
Definition: paging.h:42
VMM_MMAP_ADDR_RMODE
#define VMM_MMAP_ADDR_RMODE
Definition: vmm.h:51
pidType
int pidType
Definition: types.h:75
thread::vm_daddr
u_long vm_daddr
Definition: thread.h:43
spinLock
Definition: spinlock.h:41
descrip.h
_vmm_pageFault
void _vmm_pageFault()
VMM_USER_START
#define VMM_USER_START
Definition: vmm.h:58
numPages
int numPages
Definition: vmm_memory.c:45
PD_ENTRIES
#define PD_ENTRIES
Definition: paging.h:48
PAGE_SIZE
#define PAGE_SIZE
Definition: paging.h:37
KERNEL_PAGE_DEFAULT
#define KERNEL_PAGE_DEFAULT
Definition: paging.h:69
kprintf
int kprintf(const char *,...)
Definition: kprintf.c:259
VMM_USER_END
#define VMM_USER_END
Definition: vmm.h:59
VMM_KERN_END
#define VMM_KERN_END
Definition: vmm.h:65
tssStruct::cr3
long cr3
Definition: tss.h:46
PT_BASE_ADDR
#define PT_BASE_ADDR
Definition: paging.h:46
vmm_mapFromTask
void * vmm_mapFromTask(pidType pid, void *ptr, uint32_t size)
Definition: paging.c:372
obreak
int obreak(struct thread *td, struct obreak_args *uap)
Definition: paging.c:546
PAGE_PRESENT
#define PAGE_PRESENT
Definition: paging.h:55
kmalloc.h
vmm_pagingInit
int vmm_pagingInit()
Definition: paging.c:58
sysID
#define sysID
Definition: kmalloc.h:38
vmm_allocPageTable
int vmm_allocPageTable(uint32_t, pidType)
Definition: vmm_allocpagetable.c:7