Page 1 of 1

Long mode getting cursor position returnes 2 less

Posted: Sun Jul 24, 2022 8:55 am
by Cyao
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: Select all

    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: Select all

    *((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

Re: Long mode getting cursor position returnes 2 less

Posted: Sun Jul 24, 2022 8:56 am
by Cyao
Burh why is the image soo big

Re: Long mode getting cursor position returnes 2 less

Posted: Sun Jul 24, 2022 9:29 am
by nullplan
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: Select all

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.*)
    }
}

Re: Long mode getting cursor position returnes 2 less

Posted: Sun Jul 24, 2022 9:30 am
by Cyao
Thanks! I will look into linker scripts

Re: Long mode getting cursor position returnes 2 less

Posted: Sun Jul 24, 2022 1:53 pm
by Octocontrabass
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.

Re: Long mode getting cursor position returnes 2 less

Posted: Sun Jul 24, 2022 3:06 pm
by Cyao
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

Re: Long mode getting cursor position returnes 2 less

Posted: Sun Jul 24, 2022 3:17 pm
by Octocontrabass
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.

Re: Long mode getting cursor position returnes 2 less

Posted: Sun Jul 24, 2022 3:21 pm
by nexos
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.

Re: Long mode getting cursor position returnes 2 less

Posted: Mon Jul 25, 2022 2:59 am
by Cyao
Thanks all! The cursor is now returning the right position

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

Re: Long mode getting cursor position returnes 2 less

Posted: Mon Jul 25, 2022 6:03 am
by nexos