@@ -425,35 +425,52 @@ bool kpg_doesMappingExists (PageDirectory pd, PTR va, Physical* pa)
425425 * @Input flags Flags that determine the setup of the Page directory.
426426 * @return True if success, false otherwise.
427427 **************************************************************************************************/
428- bool kpg_createNewPageDirectory (Physical * newPD , PagingOperationFlags flags )
428+ bool kpg_setupPageDirectory (Physical * const pd , PagingOperationFlags flags ,
429+ Physical const * const kernelPD )
429430{
430- FUNC_ENTRY ("newPD return addr: %px, Flags: %x" , newPD , flags );
431+ FUNC_ENTRY ("PD addr: %px, Flags: %x, kernel PD: %px " , pd , flags , kernelPD );
431432
432- if (kpmm_alloc (newPD , 1 , PMM_REGION_ANY ) == false) {
433- RETURN_ERROR (ERROR_PASSTHROUGH , false); // PMM alloc failure
434- }
433+ k_assert (pd != NULL , "Invalid address." );
435434
436- INFO ("New PD physical location: %px" , newPD -> val );
435+ PageDirectory l_pd = NULL ;
436+ // ---------------------------------------------------------------
437+ // Create a new, empty Page Directory.
438+ // ---------------------------------------------------------------
439+ if (BIT_ISSET (flags , PG_NEWPD_FLAG_CREATE_NEW )) {
440+ if (kpmm_alloc (pd , 1 , PMM_REGION_ANY ) == false) {
441+ RETURN_ERROR (ERROR_PASSTHROUGH , false); // PMM alloc failure
442+ }
443+ INFO ("New PD physical location: %px" , pd -> val );
437444
438- PageDirectory pd = s_internal_temporaryMap (* newPD );
439- k_memset (pd , 0 , CONFIG_PAGE_FRAME_SIZE_BYTES );
445+ l_pd = s_internal_temporaryMap (* pd );
446+ k_memset (l_pd , 0 , CONFIG_PAGE_FRAME_SIZE_BYTES );
447+ } else {
448+ l_pd = s_internal_temporaryMap (* pd );
449+ }
440450
441- // Temporary map this PD and copy kernel page table entries
451+ // ---------------------------------------------------------------
452+ // Copy kernel pages tables to the provided PD.
453+ // ---------------------------------------------------------------
442454 if (BIT_ISSET (flags , PG_NEWPD_FLAG_COPY_KERNEL_PAGES )) {
443- PageDirectory currentPD = kpg_getcurrentpd ();
455+ k_assert (kernelPD != NULL , "Invalid address." );
456+
457+ PageDirectory l_kernelPD = kpg_temporaryMap (* kernelPD );
444458 for (UINT pdi = KERNEL_PDE_INDEX ; pdi < 1024 ; pdi ++ ) {
445- pd [pdi ] = currentPD [pdi ];
459+ l_pd [pdi ] = l_kernelPD [pdi ];
446460 }
461+ kpg_temporaryUnmap ();
462+ }
447463
448- if (BIT_ISSET (flags , PG_NEWPD_FLAG_RECURSIVE_MAP )) {
449- pd [RECURSIVE_PDE_INDEX ].pageTableFrame = PHYSICAL_TO_PAGEFRAME (newPD -> val );
450- }
464+ // ---------------------------------------------------------------
465+ // Recursive map
466+ // ---------------------------------------------------------------
467+ if (BIT_ISSET (flags , PG_NEWPD_FLAG_RECURSIVE_MAP )) {
468+ l_pd [RECURSIVE_PDE_INDEX ].pageTableFrame = PHYSICAL_TO_PAGEFRAME (pd -> val );
451469 }
452- s_internal_temporaryUnmap ();
453470
454- return true; // Success
471+ s_internal_temporaryUnmap ();
472+ return true;
455473}
456-
457474/***************************************************************************************************
458475 * Deletes a Page Directory and its Page tables.
459476 *
0 commit comments