OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Apr 25, 2024 2:05 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 12 posts ] 
Author Message
 Post subject: xv6 - why PIC and APIC ? [update]
PostPosted: Mon Mar 30, 2015 5:20 am 
Offline
Member
Member

Joined: Tue Mar 10, 2015 10:08 am
Posts: 97
Hello,

I am still reading the xv6 and noticed it does something interesting : setup PIC and APIC.

Everywhere it is said that it is one or another. In my case, it will be APIC.

xv6 initializes both. Why ?

Thanks


Last edited by JulienDarc on Tue Mar 31, 2015 11:43 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: xv6 - why PIC and APIC ?
PostPosted: Mon Mar 30, 2015 5:56 am 
Offline
Member
Member
User avatar

Joined: Sat Jan 15, 2005 12:00 am
Posts: 8561
Location: At his keyboard!
Hi,

JulienDarc wrote:
I am still reading the xv6 and noticed it does something interesting : setup PIC and APIC.

Everywhere it is said that it is one or another. In my case, it will be APIC.

xv6 initializes both. Why ?


You should either:
  • only use the PIC to handle IRQs, or
  • only use the IO APIC to handle IRQs

If you only use the PIC chips to handle IRQs, then you must:
  • mask all IRQs in both PIC chips (to be enable when the appropriate device driver is initialised)
  • install suitable interrupt handlers for each PIC chip's "spurious IRQ" (which is not a real IRQ and can not be disabled)
  • remap the PIC chips so that none of their IRQs conflict with anything (e.g. exceptions)
If you only use the IO APIC to handle IRQs, then you must:
  • mask all IRQs in both PIC chips
  • install suitable interrupt handlers for each PIC chip's "spurious IRQ" (which is not a real IRQ and can not be disabled)
  • remap the PIC chips so that the spurious IRQs don't conflict with anything (e.g. exceptions)
As you can see, the PIC chip initialisation ends up virtually identical in both cases.


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.


Top
 Profile  
 
 Post subject: Re: xv6 - why PIC and APIC ?
PostPosted: Tue Mar 31, 2015 8:05 am 
Offline
Member
Member

Joined: Thu May 19, 2011 5:13 am
Posts: 229
JulienDarc wrote:
I am still reading the xv6 and noticed it does something
interesting : setup PIC and APIC.
Everywhere it is said that it is one or another. In my case, it will be APIC.
xv6 initializes both. Why ?
To also work correctly on uniprocessors, Xv6 programs the programmable
interrupt controler (PIC)

_________________
Mike Gonta
look and see - many look but few see

https://mikegonta.com


Top
 Profile  
 
 Post subject: Re: xv6 - why PIC and APIC ?
PostPosted: Tue Mar 31, 2015 9:54 am 
Offline
Member
Member

Joined: Tue Mar 10, 2015 10:08 am
Posts: 97
Thanks it answers the question.

i will remap PIC interrupts (32+) and disable them as everyone does.

Thanks both of you,

Julien


Top
 Profile  
 
 Post subject: Re: xv6 - why PIC and APIC ?
PostPosted: Tue Mar 31, 2015 11:43 am 
Offline
Member
Member

Joined: Tue Mar 10, 2015 10:08 am
Posts: 97
Still,

as I look the code,

1) if we are on a uniproc, that is fine, lapic/ioapic won't be enabled
2) on a smp system, pic won't be disabled. APIC code will not disable (mask again) PIC. So we end up with enabled PIC and enabled lapic/ioapic. Both. That is wrong.

It works but it is wrong.


Top
 Profile  
 
 Post subject: Re: xv6 - why PIC and APIC ? [update]
PostPosted: Tue Mar 31, 2015 11:56 am 
Offline
Member
Member

Joined: Tue Mar 10, 2015 10:08 am
Posts: 97
Nope it is good :

Masking all interrupts disables them in the PIC.

Those two lines :

// Disable logical interrupt lines.
lapicw( LINT0 , MASKED );
lapicw( LINT1 , MASKED );

Disable the two interrupts lines used by PIC.

All is well that ends well, as expected.
For future readers, if you know that you will ONLY use multi cores, you have a mean to clean the code.


Top
 Profile  
 
 Post subject: Re: xv6 - why PIC and APIC ? [update]
PostPosted: Tue Mar 31, 2015 12:26 pm 
Offline
Member
Member
User avatar

Joined: Wed Oct 18, 2006 3:45 am
Posts: 9301
Location: On the balcony, where I can actually keep 1½m distance
Quote:
Disable the two interrupts lines used by PIC.
Eh, no. By default only one is the PIC (the slave routes through the master, not directly to the processor). More importantly, the other is the NMI which is historically used as the panic line, and should typically remain configured as such.

_________________
"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 ]


Top
 Profile  
 
 Post subject: Re: xv6 - why PIC and APIC ? [update]
PostPosted: Tue Mar 31, 2015 1:01 pm 
Offline
Member
Member

Joined: Tue Mar 10, 2015 10:08 am
Posts: 97
So that line

Code:
lapicw( LINT1 , MASKED );


could be a problem then, right ?


Top
 Profile  
 
 Post subject: Re: xv6 - why PIC and APIC ?
PostPosted: Tue Mar 31, 2015 1:25 pm 
Offline
Member
Member
User avatar

Joined: Sat Jan 15, 2005 12:00 am
Posts: 8561
Location: At his keyboard!
Hi,

JulienDarc wrote:
1) if we are on a uniproc, that is fine, lapic/ioapic won't be enabled


Local APIC is awesome (e.g. the timer alone surpasses everything that's existed before or since, for both precision and overhead). IO APICs are also awesome (e.g. more IRQs and less "PCI IRQ sharing", more flexible IRQ priorities, lower overhead due to being memory mapped, etc). This applies to single CPU systems.

Intel started including a local APIC in Pentium Pro, and all later CPUs include one. Originally it was disabled on desktop systems due to silly compatibility concerns (but left enabled on single CPU servers). In the same way single CPU servers included an IO APIC; and eventually (about 15 years ago if I remember correctly) Microsoft started requiring that the IO APIC and local APIC exist on all computers (including single CPU systems) as part of their "Windows Logo Program".

Basically; for single CPU systems both local APIC and IO APIC should be used if they exist; and for a lot of single CPU systems they do exist (especially if the OS already supports them, for SMP).

JulienDarc wrote:
Those two lines :

// Disable logical interrupt lines.
lapicw( LINT0 , MASKED );
lapicw( LINT1 , MASKED );

Disable the two interrupts lines used by PIC.


That's a very bad hack (local APIC inputs should either be left "as is" or programmed according to what ACPI tells the OS to program them; and you should not diddle with them based on random/idiotic assumptions).

At this point I'd have to ask why you're looking at the XV6 source code. I'm not convinced that it should be trusted for educational use.


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.


Top
 Profile  
 
 Post subject: Re: xv6 - why PIC and APIC ? [update]
PostPosted: Tue Mar 31, 2015 1:58 pm 
Offline
Member
Member

Joined: Tue Mar 10, 2015 10:08 am
Posts: 97
Hello,

I look at the xv6 because I thought that MIT could help me to learn properly but it seems that there are many hacks that confuse me more than anything else.

The major problem I face is that for modern operating system, the resources that are simple to understand (i.e not as convoluted as the Linux kernel) and more or less up to date are difficult to find. especially when 64 bit is involved.

Minix 1/3 are a whole another design.

Tatos, os just in assembly == lol (that's cool actually, I love assembly, but.. that is too much. And I don't know if you can write a whole operating system in assembly that could compete with, say, gcc and parts of assembly).

xv6 had the advantage of being explained, even if weird at times.

I have a lot of readings but it is too theoretical. I need to code.
I like to be able to see a simple system, analyze it and get my feet wet immediatly.

That is why I have been seduced by the xv6 concept :)

Do you know another system that is "correct" yet simple enough (please, not Linux) to analyze (preferably in c or c++ in second choice)?


Top
 Profile  
 
 Post subject: Re: xv6 - why PIC and APIC ? [update]
PostPosted: Tue Mar 31, 2015 2:46 pm 
Offline
Member
Member
User avatar

Joined: Sat Jan 15, 2005 12:00 am
Posts: 8561
Location: At his keyboard!
Hi,

JulienDarc wrote:
Do you know another system that is "correct" yet simple enough (please, not Linux) to analyze (preferably in c or c++ in second choice)?


To be honest, I'm not even sure that "correct" and "simple" aren't mutually exclusive.

For an example, consider getting a key press from the keyboard. To do it correctly you need a USB driver. To do a USB driver correctly you need to handle IRQs. To determine IRQs correctly you need an AML interpreter. For just this one case, "correctly" adds up to several million lines of code. In comparison, an ugly/dodgy hack that relies on the (likely broken) firmware's "legacy PS/2 emulation" is many orders of magnitude simpler.


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.


Top
 Profile  
 
 Post subject: Re: xv6 - why PIC and APIC ? [update]
PostPosted: Tue Mar 31, 2015 3:01 pm 
Offline
Member
Member
User avatar

Joined: Wed Jan 06, 2010 7:07 pm
Posts: 792
As long as you continue checking xv6's assumptions regarding hardware (which are often oversimplified or wrong) you can still get a lot out of it regarding the software side of things. It's (obviously) not the best design, but it demonstrates the big picture far better than any pile of specs.

An ideal example or tutorial would probably do a lot of things the simpler/less correct way but also make sure to include exactly why it's wrong and what directions you could take instead. At that point the pile of specs is much more useful.

_________________
[www.abubalay.com]


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 12 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 217 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group