How to know how many CPU is running?
-
- Member
- Posts: 102
- Joined: Wed Oct 20, 2021 6:00 pm
- Location: Paraguay
How to know how many CPU is running?
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?
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?
-
- Member
- Posts: 5563
- Joined: Mon Mar 25, 2013 7:01 pm
Re: How to know how many CPU is running?
Why don't you know how many CPUs are running? You are in charge of starting and stopping the CPUs.
-
- Member
- Posts: 102
- Joined: Wed Oct 20, 2021 6:00 pm
- Location: Paraguay
Re: How to know how many CPU is running?
I use this code, which I found on Github, to enable all CPUs:Octocontrabass wrote:Why don't you know how many CPUs are running? You are in charge of starting and stopping the 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");
}
-
- Member
- Posts: 5563
- Joined: Mon Mar 25, 2013 7:01 pm
Re: How to know how many CPU is running?
You shouldn't copy code you don't understand.
I would guess each AP increments g_ActiveCPUCount when it starts.
I would guess each AP increments g_ActiveCPUCount when it starts.
-
- Member
- Posts: 102
- Joined: Wed Oct 20, 2021 6:00 pm
- Location: Paraguay
Re: How to know how many CPU is running?
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"?Octocontrabass wrote:You shouldn't copy code you don't understand.
I would guess each AP increments g_ActiveCPUCount when it starts.
-
- Member
- Posts: 5563
- Joined: Mon Mar 25, 2013 7:01 pm
Re: How to know how many CPU is running?
The AP starts when you send the STARTUP IPI to it.NeonLightions wrote:I don't know when it started
Application Processor, as defined by Intel's Multiprocessor Specification.NeonLightions wrote:And what is "AP"?
-
- Member
- Posts: 102
- Joined: Wed Oct 20, 2021 6:00 pm
- Location: Paraguay
Re: How to know how many CPU is running?
Hi,Octocontrabass wrote:The AP starts when you send the STARTUP IPI to it.NeonLightions wrote:I don't know when it started
Application Processor, as defined by Intel's Multiprocessor Specification.NeonLightions wrote:And what is "AP"?
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?
-
- Member
- Posts: 5563
- Joined: Mon Mar 25, 2013 7:01 pm
Re: How to know how many CPU is running?
What code are you telling the AP to run?
-
- Member
- Posts: 102
- Joined: Wed Oct 20, 2021 6:00 pm
- Location: Paraguay
Re: How to know how many CPU is running?
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.Octocontrabass wrote:What code are you telling the AP to run?
Re: How to know how many CPU is running?
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?
-
- Member
- Posts: 102
- Joined: Wed Oct 20, 2021 6:00 pm
- Location: Paraguay
Re: How to know how many CPU is running?
Hi,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?
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.
-
- Member
- Posts: 5563
- Joined: Mon Mar 25, 2013 7:01 pm
Re: How to know how many CPU is running?
Where is the code to put the AP into an idle state?
Re: How to know how many CPU is running?
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?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.
-
- Member
- Posts: 102
- Joined: Wed Oct 20, 2021 6:00 pm
- Location: Paraguay
Re: How to know how many CPU is running?
Not write yet (cause lack of knownledge)Octocontrabass wrote:Where is the code to put the AP into an idle state?
-
- Member
- Posts: 102
- Joined: Wed Oct 20, 2021 6:00 pm
- Location: Paraguay
Re: How to know how many CPU is running?
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.klange wrote: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?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.