Page 3 of 3

Re: Problems with switching to new Code Segment

Posted: Fri Aug 21, 2015 1:29 pm
by FallenAvatar
onlyonemac wrote:So is refusing to help someone until after they've fumbled around with trying to figure out how to use a debugger that they are not yet experienced enough to understand.
Anyone who doesn't know how to use a debugger really should not be trying osdev.

- Monk

Re: Problems with switching to new Code Segment

Posted: Fri Aug 21, 2015 1:58 pm
by BASICFreak
Wow, I knew this was going to be a flame war, but come on people.

@OP: If you still having issues post your code and I'll (and hopefully another) will go threw it and explain what is wrong, no I will not fix it and give it back - only help you fix it.

Re: Problems with switching to new Code Segment

Posted: Fri Aug 21, 2015 2:04 pm
by onlyonemac
BASICFreak wrote:Wow, I knew this was going to be a flame war, but come on people.

@OP: If you still having issues post your code and I'll (and hopefully another) will go threw it and explain what is wrong, no I will not fix it and give it back - only help you fix it.
Finally someone with a bit of sense...

Re: Problems with switching to new Code Segment

Posted: Sat Aug 22, 2015 8:18 am
by StartOS
StartOS wrote:
Artlav wrote: Are you sure your GDT is correct and loaded?
Yes, it is.
Well, it wasn't.
I used the segment setup shown in http://wiki.osdev.org/GDT_Tutorial#Some ... _life_easy:
  • Null
  • Kernel Code - PL0, Base 0, Offset 0xFFFFFFFF
  • Kernel Data - PL0, Base 0, Offset 0xFFFFFFFF
  • User Code - PL3, Base 0, Offset 0xFFFFFFFF
  • User Data - PL3 Base 0, Offset 0xFFFFFFFF
I declared the GDT as a series of DD's in NASM
It caused problems, so I switched the DD's to DB's and everything worked just fine.
I think the byte order of the GDT entries might have been swapped (BECAUSE ENDIANNES!!!).
So it ended up being a trivial error.

Finally I apologise for your wasted time and creating a thread that spawned a flame war :oops:
Please forgive me :roll:
And BTW I'm switching to Bochs (writing a makefile target).

Re: Problems with switching to new Code Segment

Posted: Sat Aug 22, 2015 10:58 am
by onlyonemac
I'm glad you got your problem sorted out. I too have run into problems using DDs and DWs. The thing to remember is that with the x86 architecture, the least-significant byte is stored first. So if you say "DD 0x1234", the actual values in memory will be "0x34 0x12". This is not a problem as such because this is the way that the CPU expects it to be when reading the GDT, but when you chain together multiple DDs to make a DW (for example), you need to remember to do this manually, so e.g. to store "DW 0x12345678" you need to write "DD 0x5678\nDD 0x1234" to keep the bytes in the correct order as if they were a DW.

Re: Problems with switching to new Code Segment

Posted: Sat Aug 22, 2015 12:02 pm
by iansjack
I'm glad that you have resolved your problem.

This is exactly why the advice to use Bochs (or some other debugger) was given. A simple "info gdt" in the Bochs debugger allows you to inspect the table and immediately spot this sort of problem. (I believe that advice was given in the second or third reply to your OP.) You really can't beat examining your memory, either raw or via this sort of debugger command, when things aren't going as expected.

Re: Problems with switching to new Code Segment

Posted: Sat Aug 22, 2015 12:09 pm
by BASICFreak
onlyonemac wrote:I'm glad you got your problem sorted out. I too have run into problems using DDs and DWs. The thing to remember is that with the x86 architecture, the least-significant byte is stored first. So if you say "DD 0x1234", the actual values in memory will be "0x34 0x12". This is not a problem as such because this is the way that the CPU expects it to be when reading the GDT, but when you chain together multiple DDs to make a DW (for example), you need to remember to do this manually, so e.g. to store "DW 0x12345678" you need to write "DD 0x5678\nDD 0x1234" to keep the bytes in the correct order as if they were a DW.
You got your DD (double) and DW (word) confused... And just in case DB (Byte)

Re: Problems with switching to new Code Segment

Posted: Sat Aug 22, 2015 2:12 pm
by onlyonemac
@iansjack: I bet he didn't even know about the "info gdt" command in Bochs. So he would have gone through all the trouble of setting up Boches, most likely given up on his project alltogether because setting up Bochs was really not what he wanted to be doing, and still not have had a solution to his problem.

@BASICFreak: Yes you are right. I was thinking of "double" as "a double-byte" and "word" as "a 32-bit word", but that is not the case. Unrelated, but I get annoyed that not any assembler that I have seen has a data directive for a 64-bit (4-byte) word (normally called a QWORD I think) - this would be of great use when hardcoding descriptor tables, as the entries in the GDT, IDT and LDT are all 4 bytes long, and this would avoid the byte-ordering problem. Meh, I guess it's not like we're working with descriptor tables that much especially these days, but it would also be useful for 64-bit CPUs...

Re: Problems with switching to new Code Segment

Posted: Sat Aug 22, 2015 2:24 pm
by iansjack
@iansjack: I bet he didn't even know about the "info gdt" command in Bochs. So he would have gone through all the trouble of setting up Boches, most likely given up on his project alltogether because setting up Bochs was really not what he wanted to be doing, and still not have had a solution to his problem.
You seem to have a very low opinion of the intelligence of people on this forum. You have, in this thread, continually told us that the OP wouldn't understand this or wouldn't understand that. I believe you are mistaken. The third post in this thread told the OP about the "info gdt" command in Bochs.

Basically you have spent the thread trying to push your own, erroneous, interpretation of the problem on the OP without considering other possibilities or even sensibly examining the evidence provided. Just let it rest, accept that you were wrong, and accept that the OP has decided to take the very sensible advice that he was given.

The problem has been solved and that's the end of my interest in it.

Re: Problems with switching to new Code Segment

Posted: Sat Aug 22, 2015 2:32 pm
by iansjack
onlyonemac wrote:Unrelated, but I get annoyed that not any assembler that I have seen has a data directive for a 64-bit (4-byte) word
From the MASM manual:
DB, DW, DD, DQ, DT, DO, DY and DZ are used, much as in MASM, to declare initialized data in the output file. They can be invoked in a wide range of ways:

db 0x55 ; just the byte 0x55
db 0x55,0x56,0x57 ; three bytes in succession
db 'a',0x55 ; character constants are OK
db 'hello',13,10,'$' ; so are string constants
dw 0x1234 ; 0x34 0x12
dw 'a' ; 0x61 0x00 (it's just a number)
dw 'ab' ; 0x61 0x62 (character constant)
dw 'abc' ; 0x61 0x62 0x63 0x00 (string)
dd 0x12345678 ; 0x78 0x56 0x34 0x12
dd 1.234567e20 ; floating-point constant
dq 0x123456789abcdef0 ; eight byte constant
dq 1.234567e20 ; double-precision float
dt 1.234567e20 ; extended-precision float
DT, DO, DY and DZ do not accept numeric constants as operands.
The directive that you are looking for is "dq". And note that there are eight bytes in a 64-bit word.

The correspond directive in the GNU assembler is ".quad" (again, 8 bytes not 4 - the corresponding directives for a 4-byte word are "dd" and ".word").

Re: Problems with switching to new Code Segment

Posted: Sat Aug 22, 2015 9:25 pm
by Roman
iansjack wrote:the corresponding directives for a 4-byte word are "dd" and ".word"
To be correct, ".word" in the GNU assembler is for 2 bytes, when ".long" is for 4 bytes.

Re: Problems with switching to new Code Segment

Posted: Sat Aug 22, 2015 11:36 pm
by iansjack
To be really correct, the behaviour of the directives depends upon the target architecture. There are other directives that emit precise numbers of bytes. I'll leave anyone interested to RTFM.

The point remains - there are directives in commonly used assemblers to emit 8 (and 4 ;)) bytes.

Re: Problems with switching to new Code Segment

Posted: Sun Aug 23, 2015 2:00 am
by onlyonemac
iansjack wrote:
@iansjack: I bet he didn't even know about the "info gdt" command in Bochs. So he would have gone through all the trouble of setting up Boches, most likely given up on his project alltogether because setting up Bochs was really not what he wanted to be doing, and still not have had a solution to his problem.
You seem to have a very low opinion of the intelligence of people on this forum. You have, in this thread, continually told us that the OP wouldn't understand this or wouldn't understand that. I believe you are mistaken. The third post in this thread told the OP about the "info gdt" command in Bochs.

Basically you have spent the thread trying to push your own, erroneous, interpretation of the problem on the OP without considering other possibilities or even sensibly examining the evidence provided. Just let it rest, accept that you were wrong, and accept that the OP has decided to take the very sensible advice that he was given.

The problem has been solved and that's the end of my interest in it.
I wasn't assuming that the OP was stupid. I was just saying that we cannot assume anything about the intellegence of a newbie.

Re: Problems with switching to new Code Segment

Posted: Sun Aug 23, 2015 2:22 am
by iansjack
onlyonemac wrote:I was just saying that we cannot assume anything about the intellegence of a newbie.
At least we're thinking the same way there. So it probably is not polite to keep saying that he probably wouldn't understand this and probably wouldn't understand that. Let's do what most people did and give him the benefit of the doubt until we have evidence to the contrary.

We can only deduce the intelligence of a poster by reading what he posts. ;)

The bottom line is that the OP has solved his problem (it was a problem with the GDT, which was certainly the most likely explanation from the symptoms reported, as suggested in post #2, together with a suggestion of how to confirm that. Really, after that post the rest of this thread has been pretty much a waste of time.) and that he has taken on board the advice about using good debugging facilities.