System Calls Implementation

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Chpetrou
Posts: 17
Joined: Sun Aug 21, 2016 10:18 am
Libera.chat IRC: Chpetrou
Location: Cyprus, Greece
Contact:

System Calls Implementation

Post by Chpetrou »

Hello,

I am trying to implement system calls in my OS. How can i implement the read system call?
I want to be able to use scanf / get user input from keyboard.

Thanks in Advance
Korona
Member
Member
Posts: 1000
Joined: Thu May 17, 2007 1:27 pm
Contact:

Re: System Calls Implementation

Post by Korona »

That is a broad question. You posted a topic about reading from the keyboard before. How far did you get? Does your keyboard driver work now?

In general, read() will never read directly from the keyboard. In UNIX-like systems, read() gets its data from an abstract "file", which can be a terminal, a regular file, a socket or something entirely different. Thus, you probably want a file abstraction in your kernel (assuming its a monolithic kernel) that provides a read() call that is overriden for each individual file implementation. Then you want a certain file that represents the keyboard. In the simplest version, the keyboard driver fetches scan codes from the device (i.e. the keyboard), translates the scan codes to characters and puts them into a buffer and notifies processes that are blocked in a read() on the keyboard file. The read() on this file then dequeues data from the buffer. Note that this would really only be a rudimentary design though, as scan code to character translation should probably not happen in the kernel (as it has no change of knowing the correct input method that a user wants to use).
managarm: Microkernel-based OS capable of running a Wayland desktop (Discord: https://discord.gg/7WB6Ur3). My OS-dev projects: [mlibc: Portable C library for managarm, qword, Linux, Sigma, ...] [LAI: AML interpreter] [xbstrap: Build system for OS distributions].
Chpetrou
Posts: 17
Joined: Sun Aug 21, 2016 10:18 am
Libera.chat IRC: Chpetrou
Location: Cyprus, Greece
Contact:

Re: System Calls Implementation

Post by Chpetrou »

Hello

Thanks for the quick answer.
How far did you get? Does your keyboard driver work now?
I have the keyboard driver working. I just don't know how to write the read system call so when scanf calls it, to make it read from the keyboard.
Then you want a certain file that represents the keyboard. In the simplest version, the keyboard driver fetches scan codes from the device (i.e. the keyboard), translates the scan codes to characters and puts them into a buffer and notifies processes that are blocked in a read() on the keyboard file.
Also how can i make the keyboard look as a file, and read from that file?

Thanks
ronsor
Member
Member
Posts: 27
Joined: Wed Jan 25, 2017 5:31 pm
Libera.chat IRC: Ronsor

Re: System Calls Implementation

Post by ronsor »

Well you need some things first:

1. A VFS (Virtual File System) Implementation
2. I/O syscalls (open, read, write, close)
Chpetrou
Posts: 17
Joined: Sun Aug 21, 2016 10:18 am
Libera.chat IRC: Chpetrou
Location: Cyprus, Greece
Contact:

Re: System Calls Implementation

Post by Chpetrou »

1. A VFS (Virtual File System) Implementation
I wrote this.
2. I/O syscalls (open, read, write, close)
If you mean the actual system calls, i have the write one. I don't know how to make the read one.
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: System Calls Implementation

Post by AJ »

Hi,

Generally you will store keyboard events in some kind of (ring?) buffer in the driver. The call to scanf may, for example, tell the keyboard driver that a particular process is expecting keyboard input and the system call may release the buffer once it hits a LF/CR/CRLF.

If you are multitasking, you will of course need to track the process that has focus and will also need to decide how to handle throwing away unwanted keystrokes from the buffer if nothing has focus at the moment.

Cheers,
Adam
Chpetrou
Posts: 17
Joined: Sun Aug 21, 2016 10:18 am
Libera.chat IRC: Chpetrou
Location: Cyprus, Greece
Contact:

Re: System Calls Implementation

Post by Chpetrou »

Hello
Generally you will store keyboard events in some kind of (ring?) buffer in the driver. The call to scanf may, for example, tell the keyboard driver that a particular process is expecting keyboard input and the system call may release the buffer once it hits a LF/CR/CRLF.
I already have something like this. It is a kscanf function, which puts the characters in a buffer and then copies the buffer to the target buffer. It works just fine in kernel space. But when i use it in the read system call, it doesn't work.

Code: Select all

static int sys_read(int fd, char *buf, size_t len) {
    kscanf("%s", buf);

    return 0;
}
This is what i use inside the kscanf:

Code: Select all

int scanning(char *newBuffer, int size) {
    clearBuffer(newBuffer);
    clearBuffer(buffer);
    bufferNum = 0;
    isScanEnabled = 1;
    char tempChar;
    while (isScanEnabled == 1) {
        tempChar = getChar();
        if (tempChar == '\n') {
            isScanEnabled = 0;
            return 1;
        }
        else {
            if (addToBuffer(newBuffer, size, tempChar) == 0) return 0;
            monPut(tempChar);
        }
    }
    return 0;
}
Thanks
StudlyCaps
Member
Member
Posts: 232
Joined: Mon Jul 25, 2016 6:54 pm
Location: Adelaide, Australia

Re: System Calls Implementation

Post by StudlyCaps »

Hi.
Could you describe what happens when you call sys_read(...)? Does it run but not populate buf or is there a fault of some type generated? Also in

Code: Select all

scanning(...) what does clearBuffer(buffer) do? newBuffer is the param but what does buffer contain?
Chpetrou
Posts: 17
Joined: Sun Aug 21, 2016 10:18 am
Libera.chat IRC: Chpetrou
Location: Cyprus, Greece
Contact:

Re: System Calls Implementation

Post by Chpetrou »

Hello,

It runs but it doesn't populate buf.

clearBuffer just populates the buffer (which is just an extra temporary buffer) with null - '\0' .
Post Reply