0% found this document useful (0 votes)
65 views

System Calls Passing

There are three main methods to pass parameters from a process to the operating system kernel when making a system call: 1. Passing parameters directly in registers. This is simplest but limited by number of registers. 2. Passing the address of a block of memory containing the parameters in a register. Used when there are more parameters than registers. 3. Pushing parameters onto the stack. The kernel can then pop them off the stack to access the data.

Uploaded by

Nishant Tomar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views

System Calls Passing

There are three main methods to pass parameters from a process to the operating system kernel when making a system call: 1. Passing parameters directly in registers. This is simplest but limited by number of registers. 2. Passing the address of a block of memory containing the parameters in a register. Used when there are more parameters than registers. 3. Pushing parameters onto the stack. The kernel can then pop them off the stack to access the data.

Uploaded by

Nishant Tomar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Methods to pass parameters to OS

If a system call occur, we have to pass parameter to the Kernal part of the
Operating system.
For example look at the given open() system call:

• C

//function call example

#include <fcntl.h>

int open(const char *pathname, int flags, mode_t mode);

Here pathname, flags and mode_t are the parameters.


So it is to be noted that :
• We can’t pass the parameters directly like in an ordinary function
call.
• In Kernal mode there is a different way to perform a function call.
So we can’t run it in the normal address space that the process had already
created and hence we cant place the parameters in the top of the stack
because it is not available to the Kernal of the operating system for
processing. so we have to adopt any other methods to pass the parameters
to the Kernal of the OS.
We can done it through,
1. Passing parameters in registers
2. Address of the block is passed as a parameter in a register.
3. Parameters are pushed into a stack.
Let us discuss about each points in detail:
1. Passing parameters in registers.
• It is the simplest method among the three
• Here we directly pass the parameters to registers.
• But it will it is limited when, number of parameters are greater
than the number of registers.
• Here is the C program code:

• C

// Passing parameters in registers.

#include <fcntl.h>
#include <stdio.h>
int main()
{
const char* pathname = "example.txt";
int flags = O_RDONLY;
mode_t mode = 0644;

int fd = open(pathname, flags, mode);


// in function call open(), we passed the parameters pathanme,flags,mode to the kern

if (fd == -1) {
perror("Error opening file");
return 1;
}

// File operations here...

close(fd);
return 0;
}

2.Address of the block is passed as parameters


• It can be applied when the number of parameters are greater than
the number of registers.
• Parameters are stored in blocks or table.
• The address of the block is passed to a register as a parameter.
• Most commonly used in Linux and Solaris.
• Here is the C program code:

• C

//Address of the block is passed as parameters

#include <stdio.h>
#include <fcntl.h>

int main() {
const char *pathname = "example.txt";
int flags = O_RDONLY;
mode_t mode = 0644;

int params[3];
// Block of data(parameters) in array
params[0] = (int)pathname;
params[1] = flags;
params[2] = mode;

int fd = syscall(SYS_open, params);


// system call

if (fd == -1) {
perror("Error opening file");
return 1;
}

// File operations here...

close(fd);
return 0;
}

3.Parameters are pushed in a stack


• In this method parameters can be pushed in using the program and
popped out using the operating system
• So the Kernal can easily access the data by retrieving information
from the top of the stack.
• Here is the C program code

• C

//parameters are pushed into the stack

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>

int main() {
const char *pathname = "example.txt";
int flags = O_RDONLY;
mode_t mode = 0644;

int fd;
asm volatile(
"mov %1, %%rdi\n"
"mov %2, %%rsi\n"
"mov %3, %%rdx\n"
"mov $2, %%rax\n"
"syscall"
: "=a" (fd)
: "r" (pathname), "r" (flags), "r" (mode)
: "%rdi", "%rsi", "%rdx"
);

if (fd == -1) {
perror("Error opening file");
return 1;
}

// File operations here...

close(fd);
return 0;
}

You might also like