• entries
51
129
• views
82692

344 views

Since last Thursday, I've been debugging multi-tasking. I had kernel-space threads running fine (which is cool, by the way. Multi-threaded kernels, that is), but I could never do a successful privilege level switch (aka ring switch, from kernel-space to user-space and back). I was always getting bogus SS values when switching back from user-space to kernel-space. This SS value is retrieved from the TSS. I hex-dumped the TSS in my OS and it showed up fine. After lots of reading and re-reading of the Intel manuals, OS-dev tutorials and documents, I couldn't find out what's wrong.

I downloaded the bochs source, and began modifying it - making it print more details when the bogus SS is loaded. I found out that it's loaded with a value that's totally outside the TSS, which means it's looking at a wrong TSS. I inspected the bochs code that loads the SS from the TSS, and from that it was obvious I'd have some retarded bug with my GDT setup.

This was actually one of the very rare occasions I made use of the 'openness' of an open-source software package. Obviously it's a nice thing to have with developer-oriented tools and applications. Thank God bochs builds in under a minute. (Binutils, I'm looking at you! [grin])

Anyway, back to the bug. My TSS setup was like so:
// TSS descriptorsegment_descriptor_t& tss_descriptor = m_gdt_entries[TSS_SEGMENT_DESCRIPTOR_INDEX];memory_set(&tss_descriptor, 0, sizeof(segment_descriptor_t));tss_descriptor.limit_0_15 = sizeof(tss_t) - 1;tss_descriptor.base_address_0_23 = reinterpret_cast(&tss) & 0xFFFF;tss_descriptor.base_address_24_31= (reinterpret_cast(&tss) >> 24) & 0xFF;tss_descriptor.segment_type = TSS_SEGMENT_TYPE;tss_descriptor.ring = 0;tss_descriptor.is_present = 1;tss_descriptor.granularity = GRANULARITY_BYTE;

In case you haven't spotted it, the problem is in the base_address. The base_address is split into 2 fields; one is 24-bit wide, the other is 8-bit wide. I was taking only the first 16-bits when filling the first field [dead]. That meant the CPU was reading from a memory address that is significantly lower than the actual TSS address.

So yeah, now I have proper kernel-space to user-space switching and back. Yay!

Throughout this week, I intend to:
- Write basic process management code
- Write a basic system call interface
- Implement separate address spaces for different processes (I'm currently using a flat protected memory model where only accesses to out-of-physical-bounds memory addresses are intercepted).
- Move the keyboard and mouse drivers to user-space
- Write a better user-space test shell (I already have one running in kernel-space)

That's probably too much for one week, but who knows.

This is more of a reminder to myself:
Things that can make it into the next entry:
- Keyboards are not only evil, but retarded too
- I hate to admit it, but gVIM is better than I thought
- Window managers are unnecessarily heavy (e.g. gnome vs xfce)
- Rox filer is cool
- Linux distros still suck at usability and stability
- Color schemes: Black on lightyellow is awesome
- Automake and Autoconf...the day I understand what they're about is the day I die.
- CodeWarrior 9 for the PalmOS is torture
- PalmOS 5 is torture
- Palm devices are torture
- C++ is torture
- So is assembly
- I think I love and hate C++ templates in the same time.
- Long live the 'typename' keyword.
- Writing a correct generic linked list implementation is NOT as easy as one might think
- Testing it isn't easy either
- STLPort std::list tests are not as exhaustive as I thought. In fact, they're more like a joke. Unless I missed something.
- Backward compatibility sucks equally for hardware and software.
- Unit-testing OS components is hard.
- Automating the tests is even harder.
- Debugging OSes is hard too.
- Floppy disks are the most unreliable pieces of junk EVER.
- I bought a new chair.
- And a new keyboard.
- And a new mouse.
- Dual keyboard setups are cool.
- And so is "Little Fighters 2".
- D-Link middle-east routers and switches suck. Probably Ethernet adapters too. A combination of an ethernet adapter, switch, and router is bound to drive you crazy.
- Green tea is way cooler than red tea. Not sure how it compares to coffee, though.
- I think I just had a brain haemorrhage.
- Haemorrhage (also hemorrhage) is such an interesting word. It almost defies correct spelling.
- Must...stop...now...

Ahh, writing your own OS...I remember those days...

Quote:
 Ahh, writing your own OS...I remember those days...

So, when was the last time you tried it? What kind of OS did you do? What was the outcome?

I really should be able to understand the majority of this entry - I'm studying OS structures at the moment, yet sadly most of it was way over my head [headshake]

Sounds interesting though and good to hear you're both alive and making progress!

Quote:
 - Keyboards are not only evil, but retarded too ... - I bought a new chair. - And a new keyboard. ... - Dual keyboard setups are cool.
[lol]Make your mind up! You start by saying you dislike keyboards, then not only do you buy a new one but you say that having MULTIPLE keyboards is cool... Surely if one is bad, then two must be worse? [razz]

Quote:
 - Green tea is way cooler than red tea. Not sure how it compares to coffee, though.
Fact: Coffee > *

Jack

Quote:
 I really should be able to understand the majority of this entry - I'm studying OS structures at the moment, yet sadly most of it was way over my head

No, no, you're OK. The stuff I'm talking about is all x86 specific. It isn't covered in typical OS courses, unless the course includes implementing a basic x86 OS.

Quote:
 Sounds interesting though and good to hear you're both alive and making progress!

Well, yeah. I've been making so much progress, actually, that I didn't have the time to stop and write about it [smile]

I discovered that a life without messenger and active forum participation can do wonders to your productivity, after all. wink wink nudge nudge

Quote:
 Make your mind up! You start by saying you dislike keyboards, then not only do you buy a new one but you say that having MULTIPLE keyboards is cool... Surely if one is bad, then two must be worse?

Well, keyboards are evil from a gamer's perspective, as SJ Baker illustrates. They're retarded from a driver programmer's perspective as I'll probably illustrate in a future entry. I bought a new Microsoft Natural Keyboard so that my hands don't kill me. And finally, multiple keyboard sure are cool, because they eliminate the source of keyboard evilness (from a gamer's perspective) - I can play 2 player games now with no trouble!

Quote:
 Fact: Coffee > *

For the sake of discussion, I propose that: Milk > Coffee > *

Quote:
 No, no, you're OK. The stuff I'm talking about is all x86 specific. It isn't covered in typical OS courses, unless the course includes implementing a basic x86 OS.
Ah, that's better then [smile] I'm just covering the high-level concepts of the various parts of an OS. It's not really that useful - most of it is common sense [lol]

Quote:
 I discovered that a life without messenger and active forum participation can do wonders to your productivity, after all.
Haha, I'm getting better - honest [oh]

Quote:
 I can play 2 player games now with no trouble!
That's assuming you can perform the adequate controls using one hand per keyboard [lol]

Quote:
 For the sake of discussion, I propose that: Milk > Coffee > *
[flaming] NEVER!

Jack

## Create an account

Register a new account