Point a struct to a specific address
- AlfaOmega08
- Member
- Posts: 226
- Joined: Wed Nov 07, 2007 12:15 pm
- Location: Italy
Point a struct to a specific address
I'm trying to put my GDTR struct (in C++) to the address 0x500.
How should I declare the struct to do this
Thanks in advance
How should I declare the struct to do this
Thanks in advance
- AlfaOmega08
- Member
- Posts: 226
- Joined: Wed Nov 07, 2007 12:15 pm
- Location: Italy
I've already readed a bit of that pages...
I've also already tried the solution you posts. Now GCC, each time I try to modify an element in the struct gives me an error like:
GDTRegister is the name of the type of struct, GDTr is the name of the struct, and base is the element.
This is how I declared GDTRegister:
I've also already tried the solution you posts. Now GCC, each time I try to modify an element in the struct gives me an error like:
Code: Select all
gdt.cpp:36: error: request for member ‘base’ in ‘GDTr’, which is of non-class type ‘GDTRegister*’
This is how I declared GDTRegister:
Code: Select all
typedef struct GDTR {
Bit16u limit;
Bit32u base;
} GDTRegister;
- AlfaOmega08
- Member
- Posts: 226
- Joined: Wed Nov 07, 2007 12:15 pm
- Location: Italy
Each line with access to the struct gives error. Eg:
[/code]
Code: Select all
GDTr.base = (Bit32u) &GDT;
GDTr.limit = 0;
- AlfaOmega08
- Member
- Posts: 226
- Joined: Wed Nov 07, 2007 12:15 pm
- Location: Italy
It gives me the same error...combuster wrote:use -> instead of .
I was trying to put GDTr because the method you have posted, doesn't works for me...AJ wrote:just pass &GDTr when you LGDT
When I LGDT, bochs loads a GDT with base = 0 and limit = 65535 than crashes before I can update the segment registers
that sounds like you forgot to disable interrupts...AlfaOmega08 wrote: When I LGDT, bochs loads a GDT with base = 0 and limit = 65535 than crashes before I can update the segment registers
if interrupts arnt disabled, it will try to fire one, and crash, because the IDT is invalid for PMode (assuming it crashes after entering PMode -- you didnt specify, but did imply that... doesnt make sense that it would crash before that anyway...)
uhh, why wouldn't this work?
the above would put STRUCT gdt_ptr @ 0x500, and assign dummy values to limit and base. you can alter there placement with the '&' prefix.
Code: Select all
struct gdt_ptr
{
unsigned short limit;
unsigned int base;
} __attribute__((packed));
struct gdt_ptr *pGDT;
int main()
{
pGDT = 0x500; // new address of STRUCT gdt_ptr
// pGDT->limit is now at 0x500;
// pGDT->base is now at 0x502;
pGDT->limit = 0x1234;
pGDT->base = 0x5678;
Website: https://joscor.com
Gosh, look at all this helpful information, as to what supposedly doesn't work...
You probably refer to the segfault? Did you check in which line that segfault occurs, for example using a debugger?
So, it chokes when you write 0x1234 at memory address 0x500. Could this be, perhaps, because 0x500 does not represent a struct gdt_ptr just because you pointed a pointer at it?
This compiles and runs correctly. Try improving your grasp on elementary debugging, pointers, and how accessing memory works.
You probably refer to the segfault? Did you check in which line that segfault occurs, for example using a debugger?
Code: Select all
$ gcc -g test.c
$ gdb a.out
(gdb) break main
(gdb) run
Starting program: a.out
Breakpoint 1, main () at test.c:14
14 pGDT = (struct gdt_ptr *) 0x500; // new address of STRUCT gdt_ptr
(gdb) step
(gdb) step
17 pGDT->limit = 0x1234;
(gdb) step
Program received signal SIGSEGV, Segmentation fault.
0x0001068c in main () at test.c:17
17 pGDT->limit = 0x1234;
(gdb)
Code: Select all
#include <assert.h>
struct gdt_ptr
{
unsigned short limit;
unsigned int base;
} __attribute__((packed));
struct gdt_ptr *pGDT;
int main()
{
pGDT = (struct gdt_ptr *) 0x500;
assert( pGDT == (void *)0x500 );
assert( &(pGDT->limit) == (void *)0x500 );
assert( &(pGDT->base) == (void *)0x502 );
return 0;
}
Every good solution is obvious once you've found it.
wow, nice snap to a rhetorical question.
the code that I posted does work. Compiled fine under GCC and have even used that implementation in one of my previous kernels.
the code that I posted does work. Compiled fine under GCC and have even used that implementation in one of my previous kernels.
Website: https://joscor.com
The code you extracted your post from, perhaps. Taking the code from your post, adding a return 0; and a closing bracket, gives a compiler warning and segfaults when being run.01000101 wrote:the code that I posted does work. Compiled fine under GCC and have even used that implementation in one of my previous kernels.
Every good solution is obvious once you've found it.
Solar:Solar wrote:The code you extracted your post from, perhaps. Taking the code from your post, adding a return 0; and a closing bracket, gives a compiler warning and segfaults when being run.01000101 wrote:the code that I posted does work. Compiled fine under GCC and have even used that implementation in one of my previous kernels.
The code is changing a GDT. What makes you think it is designed to be run in a hosted environment? Of course under linux 0x500 is an invalid address, but it probably isn't in the OP's OS...