2626#include " kernel/kernel.hpp"
2727#include " shared/memory/constants.hpp"
2828#include " shared/logger/logger.hpp"
29+ #include " kernel/memory/paging.hpp"
2930
3031bool smpInitialized = false ;
3132
3233void smpInitialize (g_physical_address initialPageDirectoryPhysical)
3334{
35+ // TODO: For all physical allocations below we must make sure that the memory is in 32 bit address range
36+
37+ // Identity-map lower memory
38+ for (g_address phys = 0 ; phys < G_SMP_STARTUP_AREA_END; phys += G_PAGE_SIZE)
39+ {
40+ pagingMapPage (phys, phys, G_PAGE_TABLE_KERNEL_DEFAULT, G_PAGE_KERNEL_DEFAULT);
41+ logInfo (" Identitiy map %x to %x: %x" , phys, phys, pagingVirtualToPhysical (phys));
42+ }
43+
44+ // Map it so we can write it here
45+ g_virtual_address mappedLower = addressRangePoolAllocate (memoryVirtualRangePool,
46+ G_PAGE_ALIGN_UP (G_SMP_STARTUP_AREA_END) / G_PAGE_SIZE);
47+ for (g_address phys = 0 ; phys < G_SMP_STARTUP_AREA_END; phys += G_PAGE_SIZE)
48+ {
49+ pagingMapPage (mappedLower + phys, phys, G_PAGE_TABLE_KERNEL_DEFAULT, G_PAGE_KERNEL_DEFAULT);
50+ }
51+
3452 // Write values to lower memory for use within startup code
35- *((uint32_t *) G_SMP_STARTUP_AREA_PAGEDIR) = initialPageDirectoryPhysical;
36- *((uint32_t *) G_SMP_STARTUP_AREA_AP_ENTRY) = (g_virtual_address) kernelRunApplicationCore;
37- *((uint32_t *) G_SMP_STARTUP_AREA_AP_COUNTER) = 0 ;
53+ *((g_address *) (mappedLower + G_SMP_STARTUP_AREA_PAGEDIR) ) = initialPageDirectoryPhysical;
54+ *((g_address *) (mappedLower + G_SMP_STARTUP_AREA_AP_ENTRY) ) = (g_virtual_address) kernelRunApplicationCore;
55+ *((g_size *) (mappedLower + G_SMP_STARTUP_AREA_AP_COUNTER) ) = 0 ;
3856
39- logDebug (" %! initial page directory for APs: %h" , " smp" , *((uint32_t *) G_SMP_STARTUP_AREA_PAGEDIR));
40- logDebug (" %! kernel entry point for APs: %h" , " smp" , *((uint32_t *) G_SMP_STARTUP_AREA_AP_ENTRY));
41- logDebug (" %! initial AP counter value: %i" , " smp" , *((uint32_t *) G_SMP_STARTUP_AREA_AP_COUNTER));
57+ logInfo (" %! initial page directory for APs: %h, %x" , " smp" ,
58+ *((g_address*) (mappedLower+G_SMP_STARTUP_AREA_PAGEDIR)),
59+ initialPageDirectoryPhysical);
60+ logInfo (" %! kernel entry point for APs: %h" , " smp" , *((g_address*) (mappedLower+G_SMP_STARTUP_AREA_AP_ENTRY)));
61+ logInfo (" %! initial AP counter value: %i" , " smp" , *((uint32_t *) (mappedLower+G_SMP_STARTUP_AREA_AP_COUNTER)));
4262
4363 // Create enough stacks for all APs
44- g_physical_address* stackArray = (g_physical_address*) G_SMP_STARTUP_AREA_AP_STACK_ARRAY;
64+ auto stackArray = (g_physical_address*) (mappedLower + G_SMP_STARTUP_AREA_AP_STACK_ARRAY) ;
4565 for (uint32_t i = 0 ; i < processorGetNumberOfProcessors (); i++)
4666 {
47-
4867 g_physical_address stackPhysical = memoryPhysicalAllocate ();
4968 if (stackPhysical == 0 )
5069 {
5170 logInfo (" %*%! could not allocate physical page for AP stack" , 0x0C , " smp" );
5271 return ;
5372 }
73+
5474 g_virtual_address stackVirtual = addressRangePoolAllocate (memoryVirtualRangePool, 1 );
5575 if (stackPhysical == 0 )
5676 {
@@ -63,18 +83,19 @@ void smpInitialize(g_physical_address initialPageDirectoryPhysical)
6383 g_virtual_address stackTop = (stackVirtual + G_PAGE_SIZE);
6484 stackArray[i] = stackTop;
6585
66- logDebug (" %! created AP stack (%h) placed at %h" , " smp" , stackArray[i], &stackArray[i]);
86+ logInfo (" %! created AP stack (%h -> %h) placed at %h" , " smp" , stackArray[i], stackPhysical,
87+ ((g_address)&stackArray[i]) - mappedLower);
6788 }
6889
6990 // Copy start object from ramdisk to lower memory
70- const char * ap_startup_location = " system/lib/apstartup.o" ;
71- g_ramdisk_entry* startupObject = ramdiskFindAbsolute (ap_startup_location );
72- if (startupObject == 0 )
91+ const char * apStartupPath = " system/lib/apstartup.o" ;
92+ g_ramdisk_entry* startupObject = ramdiskFindAbsolute (apStartupPath );
93+ if (startupObject == nullptr )
7394 {
74- logInfo (" %*%! could not initialize due to missing apstartup object at '%s'" , 0x0C , " smp" , ap_startup_location );
95+ logInfo (" %*%! could not initialize due to missing apstartup object at '%s'" , 0x0C , " smp" , apStartupPath );
7596 return ;
7697 }
77- memoryCopy ((uint8_t *) G_SMP_STARTUP_AREA_CODE_START, ( uint8_t *) startupObject->data , startupObject->dataSize );
98+ memoryCopy ((uint8_t *) (mappedLower + G_SMP_STARTUP_AREA_CODE_START), startupObject->data , startupObject->dataSize );
7899
79100 smpInitialized = true ;
80101
@@ -88,6 +109,8 @@ void smpInitialize(g_physical_address initialPageDirectoryPhysical)
88109 }
89110 core = core->next ;
90111 }
112+
113+ logInfo (" %! initial AP counter value: %i" , " smp" , *((uint32_t *) (mappedLower+G_SMP_STARTUP_AREA_AP_COUNTER)));
91114}
92115
93116void smpInitializeCore (g_processor* cpu)
0 commit comments