Subject: Re: [Help] making my system calls
To: Ilhwan Kwon <ik26+@andrew.cmu.edu>
From: Dr. Bill Studenmund <wrstuden@loki.stanford.edu>
List: tech-kern
Date: 02/05/1999 15:02:20
On Fri, 5 Feb 1999, Ilhwan Kwon wrote:

> Hi.
> 
> I am trying to write my own system call functions for BSD.
> I am trying to calculate the maximum amount of memory a PC needs
> when it is used as a router.
> First, I declare "u_long max_memused" in /usr/src/sys/sys/mbuf.h.
> 
> Second, I add following code in m_devget function in
> /usr/src/sys/kern/uipc_mbuf.c.
> 
> 	u_long mem_used = size of memory currently used by mbufs and clusters.
> 	if(mem_used > max_memused) max_memused = mem_used;
> 
> Third, I make prototype for my system calls in mbuf.h like following.
> 	
> 	long get_mem_stat __P((void));
> 	void clear_mem_stat __P((void));
> 
> Fourth, I declare above system calls in uipc_mbuf.c like following.
> 
> 	long get_mem_stat(){
> 		return (long) max_memused;
> 	}

Won't work. Positive return values are treated as errors. You need to
correctly regenerate the syscal stubs (see below). Look at the other
syscalls, and you'll see they are passed arguments, even if the userland
call is void. One of these parameters is *retval. Your get_mem_stat needs
to be:

	int get_mem_stat(p, v, retval)
		struct proc *p;
		void *v;
		register_t *retval;
	{
		*retval = (what you want to return);
		return (0);
	}

> 	void clear_mem_stat(){
> 		max_memused = 0;
> 	}
> 
> Fifth, I put entries in init_sysent.c like following.

Don't do that! As the top of the file says:
 * DO NOT EDIT-- this file is automatically generated.

Edit syscalls.master, then re-run syscalls.conf on it to set things up.

> 	struct sysent sysent[] = {
> 		/*......*/
> 		{0, (*sy_call)get_mem_stat},	/*241 = get_mem_stat */
> 		{0, (*sy_call)clear_mem_stat},  /*242 = clear_mem_stat */
> 		/* ..... */
> 	};
> 	
> 	the 241th and 242th wer originally looked like {0, (*sy_call)no_sys},
> 
> finally, I put entries in syscalls.c
> 
> 	"get_mem_stat"		/*241 = get_mem_stat*/
> 	"clear_mem_stat" 	/*242 = clear_mem_stat*/
> 	
> 	they were originally "no_sys".
> 
> Now, I recompile the kernel and reboot.  but kernel library is not updated.
> I copy /usr/src/sys/sys/mbuf.h to /usr/include/sys/mbuf.h.
> I write a small program which includes <sys/mbuf.h> and calls get_mem_stat
> and clear_mem_stat system calls.
>
> Whe I compile, I get
> "get_mem_stat" : undefined reference from the text segent"
> "clear_mem_stat" : undefined reference from the text segent"

Did you modify libc? Go to sys/lib/libc/sys & edit Makefile.inc. Add
get_mem_stat.o and clear_mem_stat.o to the ASM= assignment. Then rebuild
libc.

> What did I do wrong?
> Any help on this matter will be greatly appreciated.

No libc changes.