Using all available CPU's - Question on using available AP's
- IanSeyler
- Member

- Posts: 337
- Joined: Mon Jul 28, 2008 9:46 am
- Location: Ontario, Canada
- GitHub: https://github.com/ReturnInfinity
- Contact:
Using all available CPU's - Question on using available AP's
Hi everyone,
I have a question about utilizing the available AP's in the system. Currently my 2nd stage bootloader starts all CPUs/Cores and gets them into 64-bit mode. After that they just spin in a "JMP $" loop.
Is there a way to tell a certain AP to jump to a specific piece of code? Can you do something like that through the APIC?
If not how could I do this? I am trying to write the SMP functions for my kernel but this has me stumped. Will I need to write a JMP or CALL with the needed memory address to the place in memory where the AP's are executing?
Thanks,
-Ian
I have a question about utilizing the available AP's in the system. Currently my 2nd stage bootloader starts all CPUs/Cores and gets them into 64-bit mode. After that they just spin in a "JMP $" loop.
Is there a way to tell a certain AP to jump to a specific piece of code? Can you do something like that through the APIC?
If not how could I do this? I am trying to write the SMP functions for my kernel but this has me stumped. Will I need to write a JMP or CALL with the needed memory address to the place in memory where the AP's are executing?
Thanks,
-Ian
Last edited by IanSeyler on Thu Sep 10, 2009 12:24 pm, edited 1 time in total.
BareMetal OS - http://www.returninfinity.com/
Mono-tasking 64-bit OS for x86-64 based computers, written entirely in Assembly
Mono-tasking 64-bit OS for x86-64 based computers, written entirely in Assembly
- Combuster
- Member

- Posts: 9301
- Joined: Wed Oct 18, 2006 3:45 am
- Libera.chat IRC: [com]buster
- Location: On the balcony, where I can actually keep 1½m distance
- Contact:
Re: Question on using available AP's
You can wake other processors by using IPIs (similar to the way you first booted them). Basically you pretend that there has been an external interrupt that you generated. The AP will then enter the appropriate interrupt handler and from there you can tell it to do whatever you want.
Re: Question on using available AP's
you can see the CPU ID (or rather, APIC ID) of the executing processor by checking the local APIC registers (I can't think of the specific one off the top of my head).
Then you can just test the ID for whatever CPU you want and tell it to do something while the others will fail the test.
There is also a way via MSR 0x1B to tell if the CPU is the BSP or an AP.
Then you can just test the ID for whatever CPU you want and tell it to do something while the others will fail the test.
There is also a way via MSR 0x1B to tell if the CPU is the BSP or an AP.
Website: https://joscor.com
Re: Question on using available AP's
Hi,
In this case, an alternative is to use memory for synchronization. For example, the AP CPUs continually check a specific memory location and when the BSP wants one or more AP CPUs to do something it sets this specific memory location to something. For e.g. the AP CPUs could check if the dword at address "foo" is zero and keep looping while it is, but if the BSP sets "foo" to something non-zero then the AP CPUs do a "call [foo]" and execute the code the BSP wanted them to execute (which eventually returns the AP CPUs to the loop). This can be a good idea for boot code (e.g. before interrupt handling is setup properly) but isn't useful later on (when the AP CPUs may be doing useful work instead of spinning).
Cheers,
Brendan
Yes.iseyler wrote:I have a question about utilizing the available AP's in the system. Currently my 2nd stage bootloader starts all CPUs/Cores and gets them into 64-bit mode. After that they just spin in a "JMP $" loop.
Is there a way to tell a certain AP to jump to a specific piece of code? Can you do something like that through the APIC?
In this case, an alternative is to use memory for synchronization. For example, the AP CPUs continually check a specific memory location and when the BSP wants one or more AP CPUs to do something it sets this specific memory location to something. For e.g. the AP CPUs could check if the dword at address "foo" is zero and keep looping while it is, but if the BSP sets "foo" to something non-zero then the AP CPUs do a "call [foo]" and execute the code the BSP wanted them to execute (which eventually returns the AP CPUs to the loop). This can be a good idea for boot code (e.g. before interrupt handling is setup properly) but isn't useful later on (when the AP CPUs may be doing useful work instead of spinning).
Cheers,
Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
- IanSeyler
- Member

- Posts: 337
- Joined: Mon Jul 28, 2008 9:46 am
- Location: Ontario, Canada
- GitHub: https://github.com/ReturnInfinity
- Contact:
Re: Question on using available AP's
Thanks for the tips everyone. Once I have something that works it will be released with the next version of my OS.
BareMetal OS - http://www.returninfinity.com/
Mono-tasking 64-bit OS for x86-64 based computers, written entirely in Assembly
Mono-tasking 64-bit OS for x86-64 based computers, written entirely in Assembly
- IanSeyler
- Member

- Posts: 337
- Joined: Mon Jul 28, 2008 9:46 am
- Location: Ontario, Canada
- GitHub: https://github.com/ReturnInfinity
- Contact:
Re: Question on using available AP's
Hello again everyone,
After a bit of a hiatus I am working on my OS again and trying to get the AP's working.
I currently have two functions for SMP. One to send an IPI to a target CPU and one to record the APIC ID of the CPU that executes it. See the code below:
From my OS I make a call to os_smp_call to have an int 0x50 sent to CPU#1.
The code for int 0x50 is pretty simple.. just says "hello from cpu#".
The issue is that I am only able to make the AP execute the code once. Subsequent calls to os_smp_call don't seem to trigger execution on the AP. Anyone have an idea on why this may be? Is my interrupt code ok? I tried debugging it in Bochs but the latest binary cannot do 64-bit and SMP.
Thanks everyone,
-Ian
After a bit of a hiatus I am working on my OS again and trying to get the AP's working.
I currently have two functions for SMP. One to send an IPI to a target CPU and one to record the APIC ID of the CPU that executes it. See the code below:
Code: Select all
; -----------------------------------------------------------------------------
; os_smp_call -- Set a certain CPU to run a piece of code
; IN: AL = CPU #
; BL = int #
; OUT:
os_smp_call:
push rdi
push rbx
push rax
mov rdi, [os_LocalAPICAddress]
add rdi, 0x0310
shl rax, 24 ; AL holds the CPU #, shift left 24 bits to get it into 31:24, 23:0 are reserved
stosd
xor rax, rax
mov rdi, [os_LocalAPICAddress]
add rdi, 0x0300
mov al, bl ; BL holds the int #, 8:0 for the int,
stosd
pop rax
pop rbx
pop rdi
ret
; -----------------------------------------------------------------------------
; -----------------------------------------------------------------------------
; os_smp_localid -- Returns the ID of the CPU that ran this function
; IN: Nothing
; OUT: RAX = CPU ID number
os_smp_localid:
push rsi
mov rsi, [os_LocalAPICAddress]
add rsi, 0x20
lodsd
shr rax, 24 ; AL now holds the CPU's APIC ID
pop rsi
ret
; -----------------------------------------------------------------------------
The code for int 0x50 is pretty simple.. just says "hello from cpu#".
Code: Select all
; -----------------------------------------------------------------
tester:
push rsi
push rdi
push rax
mov rsi, hellofrom
call os_print_string
call os_smp_localid
mov rdi, tempstring
mov rsi, rdi
call os_int_to_string
call os_print_string
pop rax
pop rdi
pop rsi
iretq
; stackstring db 'Stack is ', 0
hellofrom db 'Hello from CPU #', 0
; -----------------------------------------------------------------
Thanks everyone,
-Ian
BareMetal OS - http://www.returninfinity.com/
Mono-tasking 64-bit OS for x86-64 based computers, written entirely in Assembly
Mono-tasking 64-bit OS for x86-64 based computers, written entirely in Assembly
- IanSeyler
- Member

- Posts: 337
- Joined: Mon Jul 28, 2008 9:46 am
- Location: Ontario, Canada
- GitHub: https://github.com/ReturnInfinity
- Contact:
Re: Question on using available AP's
I found the issue! Like any other INT/IRQ you need to acknowledge it. 
The updated code is this:
Make sure to write 0's to LocalAPICAddress+0xB0
I can now make numerous calls to os_smp_call and it works everytime. After some more cleanup I plan to make an official release. So happy to get the other processors to do stuff!
-Ian
The updated code is this:
Code: Select all
; -----------------------------------------------------------------
tester:
push rsi
push rdi
push rax
mov rsi, hellofrom
call os_print_string
call os_smp_localid
mov rdi, tempstring
mov rsi, rdi
call os_int_to_string
call os_print_string
mov rdi, [os_LocalAPICAddress]
add rdi, 0xB0
xor rax, rax
stosd
pop rax
pop rdi
pop rsi
iretq
hellofrom db 'Hello from CPU #', 0
; -----------------------------------------------------------------
I can now make numerous calls to os_smp_call and it works everytime. After some more cleanup I plan to make an official release. So happy to get the other processors to do stuff!
-Ian
BareMetal OS - http://www.returninfinity.com/
Mono-tasking 64-bit OS for x86-64 based computers, written entirely in Assembly
Mono-tasking 64-bit OS for x86-64 based computers, written entirely in Assembly
- IanSeyler
- Member

- Posts: 337
- Joined: Mon Jul 28, 2008 9:46 am
- Location: Ontario, Canada
- GitHub: https://github.com/ReturnInfinity
- Contact:
Re: Question on using available AP's
Here is the proof! Sending an IPI to all cores in the system (even to the BSP itself).

Verifed under QEMU, VMware, and physical hardware.
Now I just need to write a function that makes the interrupt modify the stack so when the core gets to the "IRETQ" it actually returns to the code I want it to execute. Is this a good way to do it?
-Ian
Verifed under QEMU, VMware, and physical hardware.
Now I just need to write a function that makes the interrupt modify the stack so when the core gets to the "IRETQ" it actually returns to the code I want it to execute. Is this a good way to do it?
-Ian
BareMetal OS - http://www.returninfinity.com/
Mono-tasking 64-bit OS for x86-64 based computers, written entirely in Assembly
Mono-tasking 64-bit OS for x86-64 based computers, written entirely in Assembly
Re: Question on using available AP's
Hi,

There's nothing wrong with storing the address of a routine somewhere, then having an IPI handler that does "call [address_of_routine]" before sending the EOI and doing IRET.
I normally setup generic code for handling IPIs. For example, I might have one function that acquires a re-entrancy lock, sets a global "address_of_routine" variable, (optionally) sets one or more extra global variables (e.g. "data1", "data2"), then sends the IPI and waits for the other CPU/s to handle the IPI, then frees the re-entrancy lock and returns.
Normally I setup several of these things for different purposes - e.g. one for sending an IPI to all CPUs (where the caller calls the routine too), one for sending an IPI to a specific CPU, one for sending an IPI to a group of CPUs, etc.
However, you shouldn't need to use IPIs for very much - TLB invalidation, MTRR updates, telling a CPU that a task with a higher priority than its currently running task was put on the CPU's "ready to run" queue, and not much else. This is partly because they're expensive (each IPI trashes the pipelines, etc on every CPU that receives the IPI), so it's a good idea to avoid IPIs and reduce the number of CPUs that receive IPIs that you can't avoid (especially if you want the OS to be good for "many-CPU" systems, which is going to be almost all computers by the time your OS is finished).
Cheers,
Brendan
Probably not...iseyler wrote:Now I just need to write a function that makes the interrupt modify the stack so when the core gets to the "IRETQ" it actually returns to the code I want it to execute. Is this a good way to do it?
There's nothing wrong with storing the address of a routine somewhere, then having an IPI handler that does "call [address_of_routine]" before sending the EOI and doing IRET.
I normally setup generic code for handling IPIs. For example, I might have one function that acquires a re-entrancy lock, sets a global "address_of_routine" variable, (optionally) sets one or more extra global variables (e.g. "data1", "data2"), then sends the IPI and waits for the other CPU/s to handle the IPI, then frees the re-entrancy lock and returns.
Normally I setup several of these things for different purposes - e.g. one for sending an IPI to all CPUs (where the caller calls the routine too), one for sending an IPI to a specific CPU, one for sending an IPI to a group of CPUs, etc.
However, you shouldn't need to use IPIs for very much - TLB invalidation, MTRR updates, telling a CPU that a task with a higher priority than its currently running task was put on the CPU's "ready to run" queue, and not much else. This is partly because they're expensive (each IPI trashes the pipelines, etc on every CPU that receives the IPI), so it's a good idea to avoid IPIs and reduce the number of CPUs that receive IPIs that you can't avoid (especially if you want the OS to be good for "many-CPU" systems, which is going to be almost all computers by the time your OS is finished).
Cheers,
Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
- IanSeyler
- Member

- Posts: 337
- Joined: Mon Jul 28, 2008 9:46 am
- Location: Ontario, Canada
- GitHub: https://github.com/ReturnInfinity
- Contact:
Re: Question on using available AP's
Thanks for the info Brendan. I did go ahead with a function anyways that modifies an AP's stack but only use the function to get my AP's into the proper kernel loop. After than I use memory and a locking primitive to tell the AP's what to do.
All AP's now execute this once the OS is running:
Seems a bit hackish but it does the trick.
This function does the work:
Still much more work to be done!
All AP's now execute this once the OS is running:
Code: Select all
sleep_ap: ; AP's will be running here
hlt ; Wait for a "wakeup" IPI
;If we got here than an interrupt has been triggered or this AP was told to wake up.
mov rax, [mp_job_queue]
cmp rax, 0x0000000000000000
je sleep_ap
push rax
xor rax, rax
mov [mp_job_queue], rax ; Clear the job queue as an AP will be dealing with it.
mov byte [mp_job_queue_inuse], 0x00 ; Clear the inuse marker
pop rax
call rax
jmp sleep_ap
This function does the work:
Code: Select all
; os_dosomething --
; IN: AL = CPU#
; RBX = Code to execute
; OUT:
os_dosomething:
cmp byte [mp_job_queue_inuse], 0x00 ; Check if the marker is set to inuse
jne os_dosomething
lock
mov byte [mp_job_queue_inuse], 0x01 ; If not lock and set it
mov qword [mp_job_queue], rbx
call os_smp_wakeup ; Send an IPI to an AP
ret
BareMetal OS - http://www.returninfinity.com/
Mono-tasking 64-bit OS for x86-64 based computers, written entirely in Assembly
Mono-tasking 64-bit OS for x86-64 based computers, written entirely in Assembly
Re: Question on using available AP's
hmm.. I wasn't aware jumping to SMP was this simple... I think have some looks at your second stage bootloader to see how its done.
- IanSeyler
- Member

- Posts: 337
- Joined: Mon Jul 28, 2008 9:46 am
- Location: Ontario, Canada
- GitHub: https://github.com/ReturnInfinity
- Contact:
Re: Question on using available AP's
I did some more cleanup and now all cores (BSP included) run this:
Each AP gets a "fresh" start on wakeup. All registers are zeroed and the local stack is reset. taskdata is an area in memory that holds the address of a function for one of the AP's.
align 16
taskdata:
taskcpu00 dq 0x0000000000000000
datacpu00 dq 0x0000000000000000
taskcpu01 dq 0x0000000000000000
datacpu01 dq 0x0000000000000000
taskcpu02 dq 0x0000000000000000
datacpu02 dq 0x0000000000000000
etc...
Right before the BSP executes the sleep_ap code a task gets added so it starts the CLI:
Getting an AP to do something is as simple as this:
Seems to work well in testing. Overall I'm pretty proud of this!
If anyone wants to take a look at the code I have it hosted on Google: http://baremetal.googlecode.com
After some more work and documentation I'll put out a binary of the OS.
-Ian
Code: Select all
sleep_ap: ; AP's will be running here
; reset the stack
call os_smp_get_local_id
shl rax, 10 ; a 1024byte stack
add rax, 0x0000000000090400 ; stacks decrement when you "push", start at 1024 bytes in
mov rsp, rax
; clear registers
xor rax, rax ; aka r0
xor rbx, rbx ; aka r3
xor rcx, rcx ; aka r1
xor rdx, rdx ; aka r2
xor rsi, rsi ; aka r6
xor rdi, rdi ; aka r7
xor rbp, rbp ; aka r5
xor r8, r8
xor r9, r9
xor r10, r10
xor r11, r11
xor r12, r12
xor r13, r13
xor r14, r14
xor r15, r15
hlt ; Wait for a interrupt or "wakeup" IPI
call os_smp_get_local_id ; On wakeup find out which CPU we are
; check for a pending task
mov rsi, taskdata
shl rax, 4 ; quickly multiply RAX by 16 as each record (code+data) is 16 bytes (64bits x2)
add rsi, rax
push rsi
lodsq ; load the task address into RAX
; if there is none go back to sleep
cmp rax, 0x0000000000000000
je sleep_ap
; if there is then call RAX
call rax
; clear the pending task after execution
pop rdi
xor rax, rax
stosq
; go back to sleep
jmp sleep_ap
align 16
taskdata:
taskcpu00 dq 0x0000000000000000
datacpu00 dq 0x0000000000000000
taskcpu01 dq 0x0000000000000000
datacpu01 dq 0x0000000000000000
taskcpu02 dq 0x0000000000000000
datacpu02 dq 0x0000000000000000
etc...
Right before the BSP executes the sleep_ap code a task gets added so it starts the CLI:
Code: Select all
; assign the command line "program" to CPU 0
xor rax, rax ; Clear RAX to 0
mov rbx, os_command_line ; Set RBX to the memory address of the command line
call os_set_cpu_task ; Set it, don't need to wake it up as interrupts are enabled
Code: Select all
mov rax, 0x0000000000000001 ; CPU 1
mov rbx, helloap ; function to print "hello for AP"
call os_set_cpu_task ; set it
call os_smp_wakeup ; wake cpu up so it executes the task
If anyone wants to take a look at the code I have it hosted on Google: http://baremetal.googlecode.com
After some more work and documentation I'll put out a binary of the OS.
-Ian
BareMetal OS - http://www.returninfinity.com/
Mono-tasking 64-bit OS for x86-64 based computers, written entirely in Assembly
Mono-tasking 64-bit OS for x86-64 based computers, written entirely in Assembly
- IanSeyler
- Member

- Posts: 337
- Joined: Mon Jul 28, 2008 9:46 am
- Location: Ontario, Canada
- GitHub: https://github.com/ReturnInfinity
- Contact:
Question on using available AP's
After some more work it looks like I have SMP working properly. A program called "hellosmp" gets all available CPU's in the system to print a message to the screen. See the code below:
I used QEMU to emulate a 64-bit system with 64 processors. Here are the results:
http://picasaweb.google.com/lh/photo/T7 ... directlink

Code: Select all
[BITS 64]
[ORG 0x0000000000200000]
%INCLUDE "bmdev.asm"
start: ; Start of program label
find_AP: ; Go though the AP's and find the free ones
call os_smp_find_free ; Returns a free AP ID in RAX
jc no_more ; If there are none then the Carry flag is set
mov rbx, ap_print_hello
call os_smp_set_task ; Set the AP to run the print_hello task
jmp find_AP ; Check if there are more
no_more:
call os_smp_wakeup_all ; Send a "wakeup call" to all AP's
call ap_print_hello ; Once for the BSP to print hello
call os_smp_wait_for_aps ; Once the BSP is done wait for all AP's
call os_print_newline
ret ; Return to OS
; This procedure will be executed by each of the processors
; It requires mutually exclusive access while it creates the string and prints to the screen
; We must insure that only one CPU at a time can execute this code, so we employ a 'spinlock'.
ap_print_hello:
bt word [mutex], 0 ; Check if the mutex is free
jnc ap_print_hello ; If not check it again
lock ; The mutex was free, lock the bus
btr word [mutex], 0 ; Try to grab the mutex
jnc ap_print_hello ; Jump if we were unsuccessful
mov rsi, hellofrom
call os_print_string ; Print the "hello from" string
call os_smp_get_id ; Get the local APIC ID
mov rdi, tempstring
mov rsi, rdi
call os_int_to_string ; Convert it to a string
call os_print_string ; Print the APIC ID
bts word [mutex], 0 ; Release the mutex
ret
hellofrom db ' Hello from CPU #', 0
mutex dw 1 ; The MUTual-EXclustion flag
tempstring:
http://picasaweb.google.com/lh/photo/T7 ... directlink
BareMetal OS - http://www.returninfinity.com/
Mono-tasking 64-bit OS for x86-64 based computers, written entirely in Assembly
Mono-tasking 64-bit OS for x86-64 based computers, written entirely in Assembly
Re: Question on using available AP's
Congrats!iseyler wrote:After some more work it looks like I have SMP working properly.
JAL
- IanSeyler
- Member

- Posts: 337
- Joined: Mon Jul 28, 2008 9:46 am
- Location: Ontario, Canada
- GitHub: https://github.com/ReturnInfinity
- Contact:
Using all available CPU's
Added a function to set a free CPU to execute some code (instead of manually looking for one and then setting it). The main execution part of the hellosmp program can be changed to this:
The hellosmp program compiles to a 138 byte binary. My regular "Hello, world!" program is 31 bytes (14 of those bytes are for the string). Go Assembly!
Code: Select all
start: ; Start of program label
mov rax, ap_print_hello
set_AP: ; Go though the AP's and find the free ones
call os_smp_set_free ; Try to set a free AP to run the ap_print_hello function
jnc set_AP ; If carry was not set then there could be more free AP's
call os_smp_wakeup_all ; Send a "wakeup call" to all AP's
call ap_print_hello ; Once for the BSP to print hello
call os_smp_wait_for_aps ; Once the BSP is done wait for all AP's
call os_print_newline
ret ; Return to OS
BareMetal OS - http://www.returninfinity.com/
Mono-tasking 64-bit OS for x86-64 based computers, written entirely in Assembly
Mono-tasking 64-bit OS for x86-64 based computers, written entirely in Assembly