Page 1 of 1

Getting user input from meaty Skeleton

Posted: Tue Mar 10, 2015 4:14 pm
by JulienDarc
Hello,

I may ask some stupid question, but please bear with me, I am just beginning.

Under Linux, when I have to deal with old assembly to wait for user input (which doesn't happen that much), I do :

Code: Select all

    __asm__("loop:\n\t"
            "movl $3, %%eax\n\t"
            "movl $1, %%ebx\n\t"
            "movl (%[buffer]), %%edx\n\t"
            "int $0x80"
            :
            : [buffer]"g"(buffer)
            : "rax","rbx","rcx","memory"
    );
Now, I started with the meaty skeleton scripts. I compiled and used qemu-system-i386 -kernel myos.bin.

It starts correctly. Fine.

I tried to put some user interaction, but the screen keeps flickering with the bios inscriptions (booting from rom.. ).
I am doing it wrong.
I used the code I found in the keyboard interrupt wiki section, translated it to at&t syntax and put it in the kernel_main like so:

Code: Select all

void kernel_main( void ) {

        printf( "Hello, kernel World!\n" );
        printf( "prompt\n" );

        __asm__("push %%eax\n\t"
                         "in $0x60 ,%%al\n\t"
                         "mov $0x20, %%al\n\t"
                         "out %%al, $0x20 \n\t"
                         "pop %%eax\n\t"
                         "iret"
                    :
                    :
                    : "al"
            );
}
But :
1) I am sure that at that point, I am still in real mode (I didn't set any gdt or family up yet). The Bios should catch the keyboard input.
2) If I take "iret" out (not returning from the interrupt properly), the code doesn't care and finish as if this section was non existent.
3) Maybe I set the wrong interrupts or cannot put that in kernel_main or worst, my asm code is nonsense in this context..

Could you please give me a hint ?

Re: Getting user input from meaty Skeleton

Posted: Tue Mar 10, 2015 4:20 pm
by sortie
I'm really tired right now, but some quick comments: You don't seem to know what you're doing. That's a bad sign. You need to truly know your tools, code and the hardware - everything - to succeed at osdev. Don't randomly combine tutorials without true comprehension. That's just quick observations that might not be the true case here, only a quick look.

A more serious remark, inline assembly is useful but it must be used with precision. Your use here doesn't show the proper levels of caution that you must use to use it well. It's surprisingly hard to use correct. I very much suggest you only use inline assembly written by experts that has been community verified, at least until you are established and read the proper documentation about it and understand the dangers. I truly recommend you just do regular assembly files with functions instead of inline assembly. There's no optimizer that you can lie to inadvertently, so the assembly actually does what you intend and don't corrupt the surrounding code if you follow the calling conventions.

I have no idea why you iret. This is not an interrupt handler. It's a kernel main function. This doesn't make sense.

You forgot to read my post in the other thread and the bare bones tutorial. Multiboot loads you into 32-bit protected mode with paging disabled.

Re: Getting user input from meaty Skeleton

Posted: Tue Mar 10, 2015 4:41 pm
by iansjack
As Sortie says, you are in protected mode, so can't use BIOS calls. In any case, the code that you used from Linux doesn't use the BIOS, it makes a Linux system call. You're not in Linux, so you can't use system calls (until you have written your own). And you can't just randomly take code from an interrupt routine and stick it into a normal function then expect it to work.

I think you need to do a lot more reading before trying to progress further, particularly the Intel Programmer's Manuals. I'm not convinced that you understand how the processor works, how interrupts work, etc. If you really want to see how an OS works you could study the code in the course I linked to in another post. It presents an OS that is simple enough to understand, but complete enough to show most of the essentials. You won't understand any of it at first, but the more you read the more you will get it.

I'm afraid this just demonstrates the problems with online tutorials; they are always going to be too limited to be of much value but they tempt people to cut and paste code without understanding it. OS development is not easy and there's no substitute for reading.

Re: Getting user input from meaty Skeleton

Posted: Tue Mar 10, 2015 5:05 pm
by bace
If you're trying to wait for the user to press a key, you can't just read from this IO port. This returns the value in the keyboard's buffer, not waits until the user presses a key and then returns it (AFAIK). You can get and interrupt when a key is pressed/released, but you'll probably want to write a memory manager, an (A)PIC driver, and setup an IDT first.

And, as the others have said, it really does not look like you have any idea what you're doing. :(

Re: Getting user input from meaty Skeleton

Posted: Wed Mar 11, 2015 12:17 am
by JulienDarc
:cry: You are all right, i fear.

I am sorry mates. I missed the disabled paging protected mode.
The inline asm works fine, but I agree, it is hacky. i actually don't write the registers %% but let gcc decide with input/output operands, but i wanted to show about the logic.

Will do some reading.

See you soon :)

And thanks for your time

Re: Getting user input from meaty Skeleton

Posted: Wed Mar 11, 2015 2:42 am
by JulienDarc
The Mit course about the xv6 is very helping.

I need to start over. That is the first key.

Thanks for your good advices, you helped a lot. A lot. :wink:

Re: Getting user input from meaty Skeleton

Posted: Wed Mar 11, 2015 6:31 am
by ExeTwezz
Hello,

You can try reading Andrew Tanenbaum's books:
  • Modern Operating Systems
  • Operating Systems. Design and Implementation
The first one is only theory about how operating systems work, what they use and what they do.
The second one is similar to the first one, but includes practice (explains how does MINIX 3 work and its source code).

Re: Getting user input from meaty Skeleton

Posted: Wed Mar 11, 2015 11:32 pm
by Roman
Combuster wrote:illegal content removed
Unfortunately, a lot of people in Russia don't care about copyrights.

Re: Getting user input from meaty Skeleton

Posted: Thu Mar 12, 2015 1:19 am
by Candy
Roman wrote:
Combuster wrote:illegal content removed
Unfortunately, a lot of people in Russia don't care about copyrights.
This forum is not in Russia.

Re: Getting user input from meaty Skeleton

Posted: Thu Mar 12, 2015 1:22 am
by Roman
Candy wrote:
Roman wrote:
Combuster wrote:illegal content removed
Unfortunately, a lot of people in Russia don't care about copyrights.
This forum is not in Russia.
I was talking about ExeTwezz.
ExeTwezz's GitHub page wrote:Syktyvkar, Russia

Re: Getting user input from meaty Skeleton

Posted: Thu Mar 12, 2015 9:24 am
by JulienDarc
Thanks for the new pointers.
I read it a bit and it seems very well explained !

Thanks a lot !