Page 1 of 2
How to know how many CPU is running?
Posted: Sat Apr 02, 2022 8:41 pm
by NeonLightions
Hi,
Like the title, I would like to know how to know how many CPU is running. I'm writing SMP kernel and got stuck at that. Can anyone help me?
Re: How to know how many CPU is running?
Posted: Sat Apr 02, 2022 8:51 pm
by Octocontrabass
Why don't you know how many CPUs are running? You are in charge of starting and stopping the CPUs.
Re: How to know how many CPU is running?
Posted: Sat Apr 02, 2022 8:54 pm
by NeonLightions
Octocontrabass wrote:Why don't you know how many CPUs are running? You are in charge of starting and stopping the CPUs.
I use this code, which I found on Github, to enable all CPUs:
Code: Select all
void smp_init()
{
g_ActiveCPUCount = 1;
uint32_t localID = lapic_getID();
for (uint32_t i = 0; i < g_CPUCount; i++)
{
uint32_t apicID = g_CPUIDS[i];
if (apicID != localID)
{
lapic_sendInit(apicID);
}
}
pit_wait(10);
for (uint32_t i = 0; i < g_CPUCount; i++)
{
uint32_t apicID = g_CPUIDS[i];
if (apicID != localID)
{
lapic_sendStartup(apicID, 0x8);
}
}
pit_wait(1);
while (g_ActiveCPUCount != g_CPUCount)
{
pit_wait(1);
}
system("All CPUs activated!\n");
}
The problem is I don't know where is g_ActiveCPUCount from.
Re: How to know how many CPU is running?
Posted: Sat Apr 02, 2022 9:39 pm
by Octocontrabass
You shouldn't copy code you don't understand.
I would guess each AP increments g_ActiveCPUCount when it starts.
Re: How to know how many CPU is running?
Posted: Sat Apr 02, 2022 9:45 pm
by NeonLightions
Octocontrabass wrote:You shouldn't copy code you don't understand.
I would guess each AP increments g_ActiveCPUCount when it starts.
I appreciate your advice. That's the problem: I don't know when it started, does it have anything that get CPU state? And what is "AP"?
Re: How to know how many CPU is running?
Posted: Sat Apr 02, 2022 9:57 pm
by Octocontrabass
NeonLightions wrote:I don't know when it started
The AP starts when you send the STARTUP IPI to it.
NeonLightions wrote:And what is "AP"?
Application Processor, as defined by Intel's
Multiprocessor Specification.
Re: How to know how many CPU is running?
Posted: Mon Apr 04, 2022 1:49 am
by NeonLightions
Octocontrabass wrote:NeonLightions wrote:I don't know when it started
The AP starts when you send the STARTUP IPI to it.
NeonLightions wrote:And what is "AP"?
Application Processor, as defined by Intel's
Multiprocessor Specification.
Hi,
After a few tries (here is my code:
https://github.com/NeonLightions/Amore-OS), now the OS stuck when enabling other CPUs. What should I do?
Re: How to know how many CPU is running?
Posted: Mon Apr 04, 2022 10:21 am
by Octocontrabass
What code are you telling the AP to run?
Re: How to know how many CPU is running?
Posted: Mon Apr 04, 2022 11:04 pm
by NeonLightions
Octocontrabass wrote:What code are you telling the AP to run?
All of the code telling the AP to run is on src/cpu/smp.c. Some of functions that come from src/cpu/timer/pit.c and src/system/apic/apic.c.
Re: How to know how many CPU is running?
Posted: Tue Apr 05, 2022 12:00 am
by klange
When you send a SIPI to an AP,
as you do here, you are telling the AP where to start executing code from physical memory. The SIPI has a value attached - it's a 1 in your code, which means
start executing at 0100:0000. As I can not find anything in your code that puts instructions there, I'm led to believe that you're telling the APs to go and run whatever garbage happens to be at that address. What do you expect the APs to do?
Re: How to know how many CPU is running?
Posted: Tue Apr 05, 2022 12:36 am
by NeonLightions
klange wrote:When you send a SIPI to an AP,
as you do here, you are telling the AP where to start executing code from physical memory. The SIPI has a value attached - it's a 1 in your code, which means
start executing at 0100:0000. As I can not find anything in your code that puts instructions there, I'm led to believe that you're telling the APs to go and run whatever garbage happens to be at that address. What do you expect the APs to do?
Hi,
I want other APs to go to idle state when the BSP continues bootstraping the OS. After that in the scheduler, wake up other APs and give them the start address of the process that the scheduler schedules.
Re: How to know how many CPU is running?
Posted: Tue Apr 05, 2022 10:34 am
by Octocontrabass
Where is the code to put the AP into an idle state?
Re: How to know how many CPU is running?
Posted: Tue Apr 05, 2022 5:28 pm
by klange
NeonLightions wrote:I want other APs to go to idle state when the BSP continues bootstraping the OS. After that in the scheduler, wake up other APs and give them the start address of the process that the scheduler schedules.
That's what you
want to happen. I asked what you
expect to happen. What do you think is going to happen with the APs
right now, with your code as it is?
Re: How to know how many CPU is running?
Posted: Tue Apr 05, 2022 9:59 pm
by NeonLightions
Octocontrabass wrote:Where is the code to put the AP into an idle state?
Not write yet
(cause lack of knownledge)
Re: How to know how many CPU is running?
Posted: Tue Apr 05, 2022 10:02 pm
by NeonLightions
klange wrote:NeonLightions wrote:I want other APs to go to idle state when the BSP continues bootstraping the OS. After that in the scheduler, wake up other APs and give them the start address of the process that the scheduler schedules.
That's what you
want to happen. I asked what you
expect to happen. What do you think is going to happen with the APs
right now, with your code as it is?
As I found on documents, the APs will start on Real mode and begin excuting at given address. Like you said, it still excuting even there's nothing at [0100:0000]. I'm expecting that the APs will excuting GRUB's code (which I don't know how to do it) and enter the Protected mode.