Skip to main content

How to add a system call in xv6?


I will explain a simple example on adding a system call in xv6.
Assume that you have to add a system call to get the address space size of the currently running user program. You can name your system call as getmysize() and call this system call from within a user program.
You can write your user program as follows. Let the program name be myprog.c I assume that you know how to add a user program to xv6. So I will skip mentioning those steps here because it will be off the topic.
#include "types.h"
#include "stat.h"
#include "user.h"

int main(void){
  printf(1,"The size of my address space is %d bytes\n", getmysize());
  exit();
}
This program is written for the purpose of demonstrating the working of the system call only.
Now we will add our system call to xv6.
First add the following line at the end of the file syscall.h.
#define SYS_getmysize 23
Note that the 23 here might change depending on the number given before the line you are going to add in the file. That is if the system call number is 22 in the line before the line that you are going to add, your line should have the number 23. Simple as that.
Now add the following lines to the syscall.c file.
extern int sys_getmysize(void);
and in the array of syscalls, append the following line in.
[SYS_getmysize] sys_getmysize,
In the file sysproc.c, that is where the implementation of your system call goes if it is a system call related to process handling or memory management, put the implementation of our system call as follows.
int 
sys_getmysize(void)
{
   return proc->sz;
}
Now in the usys.S file, add the line,
SYSCALL(getmysize)
Then in the user.h file, add the following. This is how your user program sees your system call.
int getmysize(void);
Ok. Now you are ready to run your user program and see how your system call runs.
When you run myprog.c which calls your system call, the size of the address space of this same program will be returned according to what we have implemented here.

Comments

Popular posts from this blog

How to connect my database instance with elastic beanstalk instance in AWS?

If you have deployed your web application in Elastic Beanstalk in AWS and now you need to connect a database to this instance, and your database is actually residing in a different instance, how can you actually connect them? It's easy with Elastic Beanstalk. I will explain an example scenario that I used for connecting my elastic beanstalk web application with another instance containing my MongoDB database. By looking at this, you can customize as per your need. Don't worry. This is easy. :) The only things you need here are the details about the 1. Database name that you need to connect to. Ex:- "myDB" 2. Port at which the database instance is listening. EX:- In the case of MongoDB, the listening port is 27017 3. Host name of your database instance. EX:- Like localhost, in this case, it will be the Public DNS of your database instance 4. The password of your database if exists. First these details need to be set as environment variables in Elastic Be

How to import the Public Certificate of one WSO2 product to the trust store of another?

To demonstrate this point, I will use the 2 products WSO2 API Manager 2.1.0 (referred as APIM from here onwards) and WSO2 Enterprise Integrator 6.1.1 (referred as EI from here onwards). When using EI as the Business Process Server during configuration of Workflows in APIM, one step to perform is to import the public certificate of EI to the truststore of APIM [1]. So now let's see how this can be done. Step 1: Go to <EI_HOME>/repository/resources/security/ folder and execute the following keytool command. This command is used to export the public certificate of EI as a certificate file called wso2carbon.cer. Since the default keystore in EI is wso2carbon.jks, we have specified it as the keystore and the default alias is wso2carbon. Provide wso2carbon as the keystore password when prompted as it is the default password. After executing the above command from within the security folder in EI, you will see that a file with the name of wso2carbon.cer is created