OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Apr 18, 2024 5:09 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: System Calls Implementation
PostPosted: Mon Jan 29, 2018 1:39 pm 
Offline

Joined: Sun Aug 21, 2016 10:18 am
Posts: 17
Location: Cyprus, Greece
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


Top
 Profile  
 
 Post subject: Re: System Calls Implementation
PostPosted: Mon Jan 29, 2018 2:27 pm 
Offline
Member
Member

Joined: Thu May 17, 2007 1:27 pm
Posts: 999
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].


Top
 Profile  
 
 Post subject: Re: System Calls Implementation
PostPosted: Mon Jan 29, 2018 2:37 pm 
Offline

Joined: Sun Aug 21, 2016 10:18 am
Posts: 17
Location: Cyprus, Greece
Hello

Thanks for the quick answer.

Quote:
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.

Quote:
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


Top
 Profile  
 
 Post subject: Re: System Calls Implementation
PostPosted: Mon Jan 29, 2018 4:47 pm 
Offline
Member
Member

Joined: Wed Jan 25, 2017 5:31 pm
Posts: 27
Well you need some things first:

1. A VFS (Virtual File System) Implementation
2. I/O syscalls (open, read, write, close)


Top
 Profile  
 
 Post subject: Re: System Calls Implementation
PostPosted: Tue Jan 30, 2018 4:41 am 
Offline

Joined: Sun Aug 21, 2016 10:18 am
Posts: 17
Location: Cyprus, Greece
Quote:
1. A VFS (Virtual File System) Implementation


I wrote this.

Quote:
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.


Top
 Profile  
 
 Post subject: Re: System Calls Implementation
PostPosted: Tue Jan 30, 2018 5:25 am 
Offline
Member
Member
User avatar

Joined: Sun Oct 22, 2006 7:01 am
Posts: 2646
Location: Devon, UK
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


Top
 Profile  
 
 Post subject: Re: System Calls Implementation
PostPosted: Tue Jan 30, 2018 5:35 am 
Offline

Joined: Sun Aug 21, 2016 10:18 am
Posts: 17
Location: Cyprus, Greece
Hello

Quote:
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:
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:
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


Top
 Profile  
 
 Post subject: Re: System Calls Implementation
PostPosted: Mon Feb 05, 2018 7:39 pm 
Offline
Member
Member

Joined: Mon Jul 25, 2016 6:54 pm
Posts: 223
Location: Adelaide, Australia
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]scanning(...) what does clearBuffer(buffer) do? newBuffer is the param but what does buffer contain?


Top
 Profile  
 
 Post subject: Re: System Calls Implementation
PostPosted: Wed Feb 14, 2018 11:14 am 
Offline

Joined: Sun Aug 21, 2016 10:18 am
Posts: 17
Location: Cyprus, Greece
Hello,

It runs but it doesn't populate buf.

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


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Majestic-12 [Bot] and 234 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