How to assign execution to a specific processor

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.
Post Reply
kemosparc
Member
Member
Posts: 207
Joined: Tue Oct 29, 2013 1:13 pm

How to assign execution to a specific processor

Post by kemosparc »

Hi,

I wrote a small OS that starts with my own boot loader and switches to long mode.

I was able to allocate the GDT and IDT and map 4 GB of memory, set up a small video driver, and access disk using LBA.

My next step now is how to work with processors.

Can any on let me now how can I detect available processors, assign a processor to work on a specific piece of code, and how to switch between different processors.

Basically I would like to write a small multithreading library and start my own schedular.

Where can I read about that step by step with tutorials and examples.

Thanks a lot
Karim.
chickendinner
Posts: 11
Joined: Thu Aug 01, 2013 9:47 am

Re: How to assign execution to a specific processor

Post by chickendinner »

First off jumping straight into a multiprocessor scheduler isn't wise IMO. Write a single processor scheduler first so you can be sure you're context switching properly and have all your interrupt handlers, Timer and IRQ handling code done correctly.

To detect multiple CPU's i believe you can use the MP/ACPI tables to detect them (ACPI being the reccomended way and MP as a backup from what i've read). ACPI requires you to actually write an interpreter for a special language called AML to use it properly, so it's a not trivial exercise to work with. There are freestanding libraries that can do this for you however. There are plenty of articles/threads on this on how to handle these tables and establish what cores you have to work with. Read up on SMP in the intel manuals to see how to actually turn the other cores on and get them working on code.

It would be very silly to start all of this without proper memory management first. You're stuck with using static BSS buffers that you can't free otherwise. If you've just identity mapped the first 4GB without using a memory map it's likely you'll get problems. Memory addresses have holes and reserved areas you can't use. Grab a memory map of usable areas from your bootloaders multiboot header or use the BIOS call to get it. Parse the table into a data structure such as a linked list or bitmap and have a few routines to grab pointers to chunks of memory and free them. and then functions to map and unmap those physical pages into your page table in the form of a heap above your kernel image (or whatever scheme you like). Once you have this done it would be a good point to start worrying about scheduling and multiple cores.
User avatar
Combuster
Member
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: How to assign execution to a specific processor

Post by Combuster »

kemosparc wrote:Can any on let me now how can I detect available processors, assign a processor to work on a specific piece of code, and how to switch between different processors.
This isn't The Sims 4, where you play god and manually take control of individual people. Each processor is an independent worker and they have to communicate.
Where can I read about that step by step with tutorials and examples.
It's here: Beginner Mistakes
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Post Reply