T
T
Type Programmer2021-03-20 17:56:24
C++ / C#
Type Programmer, 2021-03-20 17:56:24

How can I correctly read the descriptor table?

I write my kernel in C and assembler.
To build the "project: "I used the scripts from this article https://xakep.ru/2018/06/18/lets-write-a-kernel/

The problem was that when I ran the kernel in qemu via the -kernel key, everything worked fine, but when run through easyBCD on real hardware, all the text disappeared and it broke a bit.
I accidentally found out that the matter was in the descriptor table, when I changed the selector to the code segment for the interrupt gate, the code started working on real hardware, but at the same moment it stopped working on the virtual machine.

I decided that I would just set my descriptor table (at least I would try), and first of all I decided to read it, using the code below I got the address of the table, passed a pointer to the structure as an argument (the structures below.

_getGDTR:
mov eax,[esp+4]
sgdt [eax]
ret


And when I counted the segments, I got some kind of garbage, and even when I decided to overwrite them with 0xFF values, NOTHING happened, the code was executed further, I got the output on the screen, as if I had not touched any important information.

An example of what lies in the descriptors (what is in zero, what is in the next, etc. there are not many differences there)
60560becb68e9610830559.png

I have a guess about the fact that my code / data segment does not completely cover 4 gigabytes, and my calls to the address turn out to be shifted due to the shift itself segment. But I'm not sure, maybe I screwed up somewhere.

Structures in C code:
struct GDTR{
    unsigned short limit;
    unsigned long addr;
};

struct SegmentDescription {
  unsigned char limit_low_low;
  unsigned char limit_low_hi;
  unsigned char	address_low_low;
  unsigned char	address_low_hi;
  unsigned char address_middle;		
  unsigned char access_rights;		
  unsigned char limit_hi_and_flags;
  unsigned char address_hi;	
};


I will also attach a memory dump at the "address" (I'm not sure if this is really a dump from the right address) of the table:
60560da4e346b880803247.png

I will be glad for any hints.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Type Programmer, 2021-03-29
@MegaCraZy6

Most likely the problem is related to how the C compiler packs structures.
As long as the actions take place inside the C code, this is not so important. But if the structure is used to store data in some strict byte order format, or is not explicitly used from the outside, this already causes problems.
More:
https://m.habr.com/ru/post/142662/
PS: I asked the question myself, I answered it myself. I will not mark the answer, somehow it does not turn out beautifully, and I'm not sure that I'm right. But it seemed to help me, using the knowledge from the link above, I was able to make a working crutch (good luck?).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question