James Molloy's Tutorial Known Bugs: Difference between revisions
m
→Inline Assembly optimiser problem with GCC 4.8
[unchecked revision] | [unchecked revision] |
(Moved heap bug details from the user mode section to the heap section) |
|||
(13 intermediate revisions by 8 users not shown) | |||
Line 1:
Several sources - including this Wiki - point to [http://www.jamesmolloy.co.uk/tutorial_html/ James Molloy's Roll your own toy UNIX-clone OS] Tutorial as a starting point. This is
== Before you follow the tutorial ==
Line 13:
== Problem: __cdecl calling convention ==
The tutorial states that the <tt>__cdecl</tt> calling convention is used. This is, however, a Windows term.
== Problem: CFLAGS ==
Line 46:
== Problem: Missing functions ==
The
== Problem: Interrupt handlers corrupt interrupted state ==
Line 53:
There are two ways around this. The most practical method is to pass the structure as a pointer instead, which allows you to explicitly edit the register state when needed - very useful for system calls, without having the compiler randomly doing it for you. The compiler can still edit the pointer on the stack when it's not specifically needed. The second option is to make another copy the structure and pass that.
== Problem: ISR 17 and
The interrupt handling code in the downloadable code have a bug where it handles ISR 17 and
== Problem: struct registers::esp is useless ==
Line 63:
== Problem: __attribute__((packed)) ==
This attribute packs the associated structure. This is useful in a few cases, such as the
== Problem: cli and sti in interrupt handlers ==
Line 74:
The <tt>kmalloc</tt> function in 6.4.1 only 1-byte aligns or page-aligns its memory address. This means you can only reliably use it allocate memories for chars (size 1), but not any larger types unless you use page-alignment. A proper malloc implementation returns pointers that are aligned such that they are suitable for all the common types, for instance it could be 64-bit (8-byte) aligned. You'll also want to modify the parameters such that it uses <tt>size_t</tt> appropriately rather than <tt>u32int</tt>.
Additionally the check if the address is page aligned is wrong.
<nowiki>
if (align == 1 && (placement_address & 0xFFFFF000)) // If the address is not already page-aligned</nowiki>
should be
<nowiki>
if (align == 1 && (placement_address & 0x00000FFF)) // If the address is not already page-aligned</nowiki>
== Problem: Paging Code ==
The paging code isn't terribly good and it is worth it to fully understand paging and design it all yourself. Paging code tends to be quite ugly, but it'll probably be decent after your fifth design revision. There is no need to always re-enable paging in <tt>switch_page_directory</tt>, it is likely best to have a special function the first time paging is enabled. The
== Problem: Heap Code ==
Line 87 ⟶ 94:
To fix the problem, change this:
<nowiki>
if ((location+sizeof(header_t)) & 0xFFFFF000
to this:
<nowiki>
if
See the section on user mode below for more details.
Line 102 ⟶ 109:
It's advisable to get a copy of <tt>multiboot.h</tt> from the GRUB source code rather than copied from the tutorial. Beware, the copy in the GRUB documentation is out of date, use one from an official release.
== Problem: Multitasking ==
Line 107 ⟶ 115:
It is strongly recommended that you write your own implementation of this and disregard the tutorial. The tutorial attempts to implement forking kernel threads by searching for magic values on the stack, which is insanity. If you wish to create a new kernel thread, simply decide which registers it should have and point its stack pointer at its freshly allocated stack. It will then start executing at your desired entry point. The part where it disables paging is bad and you should just map the source and destination physical frames at appropriate virtual addresses and memcpy with paging on at all times. Section 9.3 in particular is insanity and has blown up at least one well-established hobby operating system.
=== Inline
As mentioned above, writing
<nowiki>
Line 133 ⟶ 142:
10388c: ff e1 jmp *%ecx</nowiki>
Note how the
A way to fix this is to remove the
<nowiki>
; Here we:
Line 143 ⟶ 152:
; * Set the base and stack pointers
; * Set the page directory
; * Put a dummy value (0x12345) in EAX so that above we can
; switched task.
; * Restart interrupts. The STI instruction has a delay - it doesn't take effect until after
Line 152 ⟶ 161:
perform_task_switch:
cli;
mov ecx, [esp+4] ;
mov eax, [esp+8] ; physical address of current directory
mov ebp, [esp+12] ;
mov esp, [esp+16] ;
mov cr3, eax ; set the page directory
mov eax, 0x12345 ; magic number to detect a task switch
Line 164 ⟶ 173:
<nowiki>
extern void perform_task_switch(u32int, u32int, u32int, u32int);</nowiki>
and replace the
<nowiki>
perform_task_switch(eip, current_directory->physicalAddr, ebp, esp);</nowiki>
Line 197 ⟶ 206:
%endmacro</nowiki>
=== Problem 2: Don't forget to allow interrupts in user mode in idt_set_gate ===
Find this comment in chapter 4 and uncomment the code:
Line 225 ⟶ 234:
This fixes a problem where the syscall hander won't get called.
=== Problem 4:
<tt>KERNEL_STACK_SIZE</tt> needs defining in <tt>task.h</tt>:
|