Homework 1 - Webcourse

Operating Systems (234123) – Spring 2012
(Homework Wet 1)
Exercise 1
Wet Part
Due date: 16/04/2012 12:30
TA in charge: Alex Kreimer
E-mails regarding this exercise should be sent only to the following email:
[email protected] with the subject line: cs234123hw1_wet.
1. Introduction
In the preliminary assignment, you learned the basic methods for changing
and compiling the Linux kernel. In this assignment you will use this
knowledge to make a more significant change to the functionality of the
kernel.
In this exercise you will implement process tags support in Linux. Every
process may be tagged with zero or more tags. Process tags may be used for
signal distribution purposes. For example if you'd like to send a SIGINT signal
to a number of processes at the same time, you may tag a number of
processes with the same tag and signal a tag instead of process.
2. Detailed Description
Please note that for the sake of the exercise the API and the capabilities of
tag cloud are limited.
Process tags possess the following properties:
 Each tag is a nonnegative integer id
 Process tag needs to be created before it may be used
 Every process may be tagged with at least 0 and at most 4 tags
 Upon creation a process has the same tags as its father at time of child
creation (i.e. tag is an inherited property)
 Processes swapper(id=0) should be tagged with tag=0 which is
created automatically when the system starts.
 There may at most be 256 tags in the system
Processes may read and alter their tags and perform some other operations
as described below.
You need to write code wrappers and internal system call implementations for
the following system calls (see the slides from Tutorial 2).
1
Operating Systems (234123) – Spring 2012
(Homework Wet 1)
For each one of the system calls you should implement a code wrapper with
the appropriate name (e.g sys_tag_create for tag_create). The return
value of the system call should be 0, or a positive value for success (see
below for the exact codes), or the appropriate negative error code. For
example, if it is said that tag_create should set errno to EINVAL then
sys_tag_create should return –EINVAL.
The following describes the system calls prototype and behavior. The
actual implementation of this behavior should be implemented in the
wrappers and system calls.

int tag_create() (system call #243)
Description: Creates a new tag. Every tag must be created prior to its
use. This call should return the lowest (unused) tag available. For
example, if tags 0-50 are taken tag_create will return 51, if tag 30 was
removed, tag_create should return 30.
@return a new tag or -1 if failed.
On failure 'errno' should contain one of the following values:


'EPERM': Operation is not permitted because the limit of 256
tags is reached.
int tag_delete(int tag) (system call #244)
Description: Deletes tag. The tag may only be deleted if no process in
the system is currently tagged with it.
@param

tag to delete

Return value: -1 on failure; 0 on success.
@return
On failure 'errno' should contain one of the following values:

'EBUSY': there is at least one process that has tag.

'ENOENT': tag doesn't exist.
2
Operating Systems (234123) – Spring 2012
(Homework Wet 1)

int tag_proc(int pid, int tag) (system call #245)
Description: Tag process pid with tag. If the process already has this
tag the call should succeed.
@param

pid process id

tag tag id
@return -1 on failure; 0 on success.
On failure 'errno' should contain one of the following values:


'ESRCH' (No such process): There is no process with the
specified pid.

'ENOENT': tag doesn't exist.

'EPERM': Operation is not permitted because the limit of 4 tags
is reached for this process.
int tag_untag_proc(int pid, int tag) (system call #246)
Description: Remove tag from process pid.
have the tag the call should succeed.
If the process doesn't
@param

pid process id

tag tag id
@return -1 on failure; 0 on success.
On failure 'errno' should contain one of the following values:


'ESRCH' (No such process): There is no process with this pid.

'ENOENT': tag doesn't exist.
int tag_get_procs_num(int tag) (system call #247)
Description: get a number of processes tagged with tag.
@param

tag is a tag id
@return -1 on failure; a number of processes that have tag on success.
On failure 'errno' should contain one of the following values:

'ENOENT': tag doesn't exist.
3
Operating Systems (234123) – Spring 2012
(Homework Wet 1)

int tag_get_procs(int tag, int n, int *pids) (system
call #248)
Description: get all processes that have tag. pids should be preallocated to hold the tags. Let's denote #pids a number of processes
tagged with tag. If n<#pids you should fill the pids array with n
smallest process ids. If n>#pids you should fill only the n first entries.
@param

tag is a tag id

n is the size of memory allocated for pids
@return

pids an array that contain process ids

Return value: -1 on failure; number of filled entries in pids array
on success.
On failure 'errno' should contain one of the following values:


'ENOENT': tag doesn't exist.
int tag_cloud(int pid, int tags[4]) (system call #249)
Description: get all tags of process pid.
@param

pid process id
@return

tags is an array that contains tags. Let's denote a number of
tags process has as n. Upon successful call completion
tags[0:n-1] should contain the process tags. All other tags
entries should be left as-is. If n is 0 tags should be left as-is.

Return value: -1 on failure; a number of tags that the process
has.
On failure 'errno' should contain one of the following values:

'ESRCH' (No such process): There is no process with the
specified pid.
4
Operating Systems (234123) – Spring 2012
(Homework Wet 1)
3. Example Wrapper
Here is an example of the code wrapper for wrapper (see explanation
below). Follow this example to write the other three code wrappers:
int wrapper(int *array, int count){
long __res;
__asm__ volatile (
"movl $245, %%eax;"
"movl %1, %%ebx;"
"movl %2, %%ecx;"
"int $0x80;"
"movl %%eax,%0"
: "=m" (__res)
: "m" ((long)array), "m" (count)
: "%eax","%ebx","%ecx"
);
if ((unsigned long)(__res) >= (unsigned long)(-125)) {
errno = -(__res);
__res = -1;
}
return (int)(__res);
}
Explanation of inline assembler:
1) movl $245, %%eax – copy system call number to register eax.
2) movl %1, %%ebx - copy first parameter (array) to register ebx.
3) movl %2, %%ecx - copy second parameter (size) to register ecx.
4) int $0x80 – system call invocation via interrupt 0x80.
5) movl %%eax,%0 – copy the value that was returned by the system
call to %0 (which is the first output operand).
6) : "=m" (__res) – output operand.
7) : "m" ((long)array), "m" (size) – input operands.
8) : %eax,%ebx,%ecx– list of clobbered registers. Inform gcc that we
use eax, ebx, ecx registers.
You should read the following document for information about the commands
used in the preceding code segment:
http://www-106.ibm.com/developerworks/linux/library/l-ia.html
The code wrappers will not be put as a part of the kernel but in a separate h
file (syscall_tags.h). This file shouldn't be put inside the kernel but as a
separate file. When you test your system calls you need to include this file in
your user mode test program.
5
Operating Systems (234123) – Spring 2012
(Homework Wet 1)
4. Notes and Tips

You are not allowed to use syscall functions to implement code wrappers,
or to write the code wrappers for your system calls using the macro
_syscall1. You should write the code wrappers according to the example of
the code wrapper given above.

You should use the functions copy_from_user, copy_to_user, etc. in order
to copy data from user space to kernel space and vice versa.

All your changes must be made in the kernel level.

Submit only modified files from the Linux kernel.

No need to print your code as part of the wet HW paper submission to the
course's cell. You need only to explain what you have been done in the
code.

Start working on the assignment as soon as possible. The deadline is final,
NO postponements will be given, and high load on the VMWare machines
will not be accepted as an excuse for late submissions.

'-ENOENT' stands for 'minus ENOENT'

Don't forget to initialize all your data structures.

Write your own tests. We will check your assignment also with our test
program.

Linux is case-sensitive. entry.S means entry.S, not Entry.s, Entry.S or
entry.s.

You can assume that the system is with a single CPU.

In case you decide to allocate memory in the kernel (depends on your
implementation), you should use kmalloc and kfree in the kernel in order
to allocate and release memory. If kmalloc fails you should return
ENOSPC. For the kmalloc function use flag GFP_KERNEL for the memory
for kernel use.

Pay attention that the process descriptor size is limited. Do not add too
many new fields. New fields should be added at the end of the process
descriptor struct in order for the code to be backward compatible.
6
Operating Systems (234123) – Spring 2012
(Homework Wet 1)
5. What should you do?
Use VMware, like you learned in the preliminary assignment, in order to make
the following changes in the Linux kernel:
1. Put the implementation of the new system calls in the file
kernel/syscall_tags.c that you will have to create and add to the
kernel. Update the makefile in that directory to compile your new file too.
(Tip: add it to obj-y).
2. Update sched.h (constant + new fields definition)
3. Update entry.S (add new system call number)
4. Make any necessary changes in the kernel code so the new system calls
can be used like any other existing Linux system call. Your changes can
include modifying any .c, .h or .S (assembly) file that you find
necessary.
5. Find init_idle function in sched.c that's responsible for initializing first
process task structure and initialize the tags there.
6. Make necessary changes in file fork.c.
7. Put the wrappers functions in syscall_tags.h
8. Recompile and run the new kernel like you did in the preliminary
assignment.
9. Boot with your new Linux, and try to compile and run the test program
to make sure the new system calls work as expected.
Did it all? Good work, Submit your assignment. 
6. Submission
The submission of this assignment has two parts:

An electronic submission – you should create a zip file (use zip only,
not gzip, tar, rar, 7z or anything else) containing the following files:
a. A tarball named kernel.tar.gz containing all the files in the
kernel that you created or modified (including any source,
assembly or makefile).
To create the tarball run (inside VMWare):
cd /usr/src/linux-2.4.18-14custom
tar -czf kernel.tar.gz <list of modified or added files>
For example, if the only files you changed are
arch/i386/kernel/entry.S and kernel/syscall_tags.c you
7
Operating Systems (234123) – Spring 2012
(Homework Wet 1)
should run (the second command should be on one line, it was
split due to being too long on paper):
cd /usr/src/linux-2.4.18-14custom
tar –czf kernel.tar.gz arch/i386/kernel/entry.S
kernel/syscall_tags.c
Make sure you don't forget any file and that you use relative
paths in the tar command, i.e., use kernel/syscall_tags.c and not
/usr/src/linux-2.4.18-14custom/kernel/ syscall_tags.c
b. A file named submitters.txt which includes the ID, name and
email of the participating students. The following format should
be used:
Bill Gates [email protected] 123456789
Linus Torvalds [email protected] 234567890
Steve Jobs jobs@os_is_best.com 345678901
c. A file named syscall_tags.h that contains the implementation
of your wrapper functions.
Important Note: Make the outlined zip structure exactly. In
particular, the zip should contain only the following files (no
directories!): kernel.tar.gz, submitters.txt and syscall_tags.h. You can
create the zip by running (inside VMware):
zip final.zip kernel.tar.gz submitters.txt syscall_tags.h
The zip should look as follows:
zipfile -+
|
+- kernel.tar.gz
|
+- submitters.txt
|
+- syscall_tags.h

A printed submission. You should write a short (1 page) summary
explaining what you have done in this assignment, which changes to
the kernel data structures you have done and which functions have
been modified and how. This need to be attached with the relevant
cover page (separate from the dry) and submitted to the course cell.
Best Wishes,
The course staff
8