OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 4:59 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: Long mode getting cursor position returnes 2 less
PostPosted: Sun Jul 24, 2022 8:55 am 
Offline
Member
Member

Joined: Tue Jun 07, 2022 11:23 am
Posts: 78
Location: France
I've just went from pm to lm, and now i'm fixing my print char function, and i found out that the get cursor function returns the cursor position but 3 less, the get cursor pos was working perfectly in pm.

My get cursor function:
Code:
    outb(REG_SCREEN_CTRL, 14);
    int offset = inb(REG_SCREEN_DATA) << 8;
    outb(REG_SCREEN_CTRL, 15);
    offset += inb(REG_SCREEN_DATA);
    return offset;

Little print test:
Code:
    *((volatile char *) 0xB80A0) = 'O'; // If the get_cursor_offset() works right, 'H' should cover the 'O'
    *((volatile char *) 0xB80A0 + get_cursor_offset()) = 'H';

Output:
Image
I think i'm just doing something stupid but idk what, can someone help me look?
thx

if you need to see any more code you can look here https://www.github.com/cheyao/OS


Last edited by Cyao on Sun Jul 24, 2022 3:00 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Long mode getting cursor position returnes 2 less
PostPosted: Sun Jul 24, 2022 8:56 am 
Offline
Member
Member

Joined: Tue Jun 07, 2022 11:23 am
Posts: 78
Location: France
Burh why is the image soo big

_________________
https://github.com/cheyao/Achieve-Core


Top
 Profile  
 
 Post subject: Re: Long mode getting cursor position returnes 2 less
PostPosted: Sun Jul 24, 2022 9:29 am 
Offline
Member
Member

Joined: Wed Aug 30, 2017 8:24 am
Posts: 1593
cyao1234 wrote:
Burh why is the image soo big
Seems like you have linker issues. Fix those. You may wish to consult linker options such as "-z max-page-size=0x1000". Otherwise we will need to see your linker script to know where the problem is. BTW, my linker script looks like this:
Code:
ENTRY(_kstart)
OUTPUT_FORMAT("elf64-x86-64")

PHDRS {
    headers PT_PHDR PHDRS;
    text PT_LOAD FILEHDR PHDRS;
    data PT_LOAD;
}

SECTIONS {
    . = 0xffffffff80000000 + SIZEOF_HEADERS;
    .text : {
        *(.text)
        *(.text.*)
    } :text
    .rodata : {
        *(.rodata)
        *(.rodata.*)
    }

    /* Normally, the overlap between text and data section is handled by having
     * two different pages for the last bits of text and the first bits of data.
     * That way, if the last bits of text are overwritten, it won't affect the
     * text that is actually used. Unfortunately, for the kernel this is not
     * possible. The whole file is loaded into memory en bloc, so the same page
     * would be mapped twice. Therefore, a write access to the writable page
     * would end up being visible in the non-writable side of things. Therefore,
     * we must actually page-align here.
     */
    . = ALIGN(2M);
    .data : {
        *(.data)
        *(.data.*)
    } :data
    .bss : {
        *(.bss)
        *(COMMON)
        *(.bss.*)
    }
}

_________________
Carpe diem!


Top
 Profile  
 
 Post subject: Re: Long mode getting cursor position returnes 2 less
PostPosted: Sun Jul 24, 2022 9:30 am 
Offline
Member
Member

Joined: Tue Jun 07, 2022 11:23 am
Posts: 78
Location: France
Thanks! I will look into linker scripts

_________________
https://github.com/cheyao/Achieve-Core


Top
 Profile  
 
 Post subject: Re: Long mode getting cursor position returnes 2 less
PostPosted: Sun Jul 24, 2022 1:53 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5100
cyao1234 wrote:
I think i'm just doing something stupid but idk what

You never switch to a 64-bit code segment, so the CPU is still in 32-bit compatibility mode. The two modes are similar enough that your code still runs, but it gives you incorrect results.


Top
 Profile  
 
 Post subject: Re: Long mode getting cursor position returnes 2 less
PostPosted: Sun Jul 24, 2022 3:06 pm 
Offline
Member
Member

Joined: Tue Jun 07, 2022 11:23 am
Posts: 78
Location: France
Octocontrabass wrote:
You never switch to a 64-bit code segment, so the CPU is still in 32-bit compatibility mode. The two modes are similar enough that your code still runs, but it gives you incorrect results.

So I would need to do a `jmp CODE_SEG:a_lable_at_almost_the_same_place` after the lgdt right?

And do I need to clear the 32 bit mode bit in cr0?

thx

_________________
https://github.com/cheyao/Achieve-Core


Top
 Profile  
 
 Post subject: Re: Long mode getting cursor position returnes 2 less
PostPosted: Sun Jul 24, 2022 3:17 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5100
cyao1234 wrote:
So I would need to do a `jmp CODE_SEG:a_lable_at_almost_the_same_place` after the lgdt right?

You need to perform the JMP after you've enabled long mode by setting CR4.PAE, EFER.LME, and CR0.PG. You don't have a 64-bit code segment in your GDT, so you will need to add one.

cyao1234 wrote:
And do I need to clear the 32 bit mode bit in cr0?

There is no 32-bit mode bit in CR0.


Top
 Profile  
 
 Post subject: Re: Long mode getting cursor position returnes 2 less
PostPosted: Sun Jul 24, 2022 3:21 pm 
Offline
Member
Member

Joined: Tue Feb 18, 2020 3:29 pm
Posts: 1071
Quote:
And do I need to clear the 32 bit mode bit in cr0?

Do you mean CR0.PE?

If so, no, you shouldn't clear that bit.

_________________
"How did you do this?"
"It's very simple — you read the protocol and write the code." - Bill Joy
Projects: NexNix | libnex | nnpkg


Top
 Profile  
 
 Post subject: Re: Long mode getting cursor position returnes 2 less
PostPosted: Mon Jul 25, 2022 2:59 am 
Offline
Member
Member

Joined: Tue Jun 07, 2022 11:23 am
Posts: 78
Location: France
Thanks all! The cursor is now returning the right position

And does anyone know some good tutorial/documentation about linker scripts? thanks!

_________________
https://github.com/cheyao/Achieve-Core


Top
 Profile  
 
 Post subject: Re: Long mode getting cursor position returnes 2 less
PostPosted: Mon Jul 25, 2022 6:03 am 
Offline
Member
Member

Joined: Tue Feb 18, 2020 3:29 pm
Posts: 1071
These are the official docs:

https://sourceware.org/binutils/docs/ld/Scripts.html

_________________
"How did you do this?"
"It's very simple — you read the protocol and write the code." - Bill Joy
Projects: NexNix | libnex | nnpkg


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 71 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group