Moved brk/sbrk into user-space
[akaros.git] / kern / include / ros / procinfo.h
1 /* See COPYRIGHT for copyright information. */
2
3 #ifndef ROS_PROCINFO_H
4 #define ROS_PROCINFO_H
5
6 #include <ros/memlayout.h>
7 #include <ros/common.h>
8
9 #define PROCINFO_MAX_ARGP 32
10 #define PROCINFO_ARGBUF_SIZE 3072
11
12 typedef struct procinfo {
13         pid_t pid;
14         pid_t ppid;
15         size_t max_harts;
16         uint64_t tsc_freq;
17         void* heap_bottom;
18
19         char* argp[PROCINFO_MAX_ARGP];
20         char argbuf[PROCINFO_ARGBUF_SIZE];
21 } procinfo_t;
22 #define PROCINFO_NUM_PAGES  ((sizeof(procinfo_t)-1)/PGSIZE + 1) 
23
24 static int
25 procinfo_pack_args(procinfo_t* p, char* const* argv, char* const* envp)
26 {
27         int nargv = 0, nenvp = 0;
28         if(argv) while(argv[nargv]) nargv++;
29         if(envp) while(envp[nenvp]) nenvp++;
30
31         if(nargv+nenvp+2 > PROCINFO_MAX_ARGP)
32                 return -1;
33
34         int pos = 0;
35         for(int i = 0; i < nargv; i++)
36         {
37                 int len = strlen(argv[i])+1;
38                 if(pos+len > PROCINFO_ARGBUF_SIZE)
39                         return -1;
40                 p->argp[i] = ((procinfo_t*)UINFO)->argbuf+pos;
41                 memcpy(p->argbuf+pos,argv[i],len);
42                 pos += len;
43         }
44         p->argp[nargv] = 0;
45
46         for(int i = 0; i < nenvp; i++)
47         {
48                 int len = strlen(envp[i])+1;
49                 if(pos+len > PROCINFO_ARGBUF_SIZE)
50                         return -1;
51                 p->argp[nargv+1+i] = ((procinfo_t*)UINFO)->argbuf+pos;
52                 memcpy(p->argbuf+pos,envp[i],len);
53                 pos += len;
54         }
55         p->argp[nargv+nenvp+1] = 0;
56         
57         return 0;
58 }
59
60 // this is how user programs access the procinfo page
61 #ifndef ROS_KERNEL
62 # define __procinfo (*(procinfo_t*)UINFO)
63 #endif
64
65 #endif // !ROS_PROCDATA_H