How to know how many CPU is running?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
NeonLightions
Member
Member
Posts: 102
Joined: Wed Oct 20, 2021 6:00 pm
Location: Paraguay

How to know how many CPU is running?

Post 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?
Octocontrabass
Member
Member
Posts: 5563
Joined: Mon Mar 25, 2013 7:01 pm

Re: How to know how many CPU is running?

Post by Octocontrabass »

Why don't you know how many CPUs are running? You are in charge of starting and stopping the CPUs.
NeonLightions
Member
Member
Posts: 102
Joined: Wed Oct 20, 2021 6:00 pm
Location: Paraguay

Re: How to know how many CPU is running?

Post 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.
Octocontrabass
Member
Member
Posts: 5563
Joined: Mon Mar 25, 2013 7:01 pm

Re: How to know how many CPU is running?

Post by Octocontrabass »

You shouldn't copy code you don't understand.

I would guess each AP increments g_ActiveCPUCount when it starts.
NeonLightions
Member
Member
Posts: 102
Joined: Wed Oct 20, 2021 6:00 pm
Location: Paraguay

Re: How to know how many CPU is running?

Post 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"?
Octocontrabass
Member
Member
Posts: 5563
Joined: Mon Mar 25, 2013 7:01 pm

Re: How to know how many CPU is running?

Post 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.
NeonLightions
Member
Member
Posts: 102
Joined: Wed Oct 20, 2021 6:00 pm
Location: Paraguay

Re: How to know how many CPU is running?

Post 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?
Octocontrabass
Member
Member
Posts: 5563
Joined: Mon Mar 25, 2013 7:01 pm

Re: How to know how many CPU is running?

Post by Octocontrabass »

What code are you telling the AP to run?
NeonLightions
Member
Member
Posts: 102
Joined: Wed Oct 20, 2021 6:00 pm
Location: Paraguay

Re: How to know how many CPU is running?

Post 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.
klange
Member
Member
Posts: 679
Joined: Wed Mar 30, 2011 12:31 am
Libera.chat IRC: klange
Discord: klange

Re: How to know how many CPU is running?

Post 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?
NeonLightions
Member
Member
Posts: 102
Joined: Wed Oct 20, 2021 6:00 pm
Location: Paraguay

Re: How to know how many CPU is running?

Post 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.
Octocontrabass
Member
Member
Posts: 5563
Joined: Mon Mar 25, 2013 7:01 pm

Re: How to know how many CPU is running?

Post by Octocontrabass »

Where is the code to put the AP into an idle state?
klange
Member
Member
Posts: 679
Joined: Wed Mar 30, 2011 12:31 am
Libera.chat IRC: klange
Discord: klange

Re: How to know how many CPU is running?

Post 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?
NeonLightions
Member
Member
Posts: 102
Joined: Wed Oct 20, 2021 6:00 pm
Location: Paraguay

Re: How to know how many CPU is running?

Post by NeonLightions »

Octocontrabass wrote:Where is the code to put the AP into an idle state?
Not write yet :) (cause lack of knownledge)
NeonLightions
Member
Member
Posts: 102
Joined: Wed Oct 20, 2021 6:00 pm
Location: Paraguay

Re: How to know how many CPU is running?

Post 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.
Post Reply