New vcoremap in procinfo
[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 #include <ros/arch/arch.h>
9
10 #define PROCINFO_MAX_ARGP 32
11 #define PROCINFO_ARGBUF_SIZE 3072
12
13 // TODO: move me to an atomic header, and give me some support functions.
14 #ifndef __TMP_SEQ_CTR
15 #define __TMP_SEQ_CTR
16 typedef uint8_t seq_ctr_t;
17 #endif
18
19 /* Not necessary to expose all of this, but it doesn't hurt, and is convenient
20  * for the kernel. */
21 struct vcore {
22         uint32_t                        pcoreid;
23         bool                            valid;
24         bool                            preempt_served;
25         uint64_t                        preempt_pending;
26         struct trapframe        *tf_to_run;
27 };
28
29 typedef struct procinfo {
30         pid_t pid;
31         pid_t ppid;
32         size_t max_harts;
33         uint64_t tsc_freq;
34         void* heap_bottom;
35         char* argp[PROCINFO_MAX_ARGP];
36         char argbuf[PROCINFO_ARGBUF_SIZE];
37         /* glibc relies on stuff above this point.  if you change it, you need to
38          * rebuild glibc. */
39         struct vcore            vcoremap[MAX_NUM_CPUS];
40         uint32_t                        num_vcores;
41         seq_ctr_t                       coremap_edit;
42 } procinfo_t;
43 #define PROCINFO_NUM_PAGES  ((sizeof(procinfo_t)-1)/PGSIZE + 1) 
44
45 static int
46 procinfo_pack_args(procinfo_t* p, char* const* argv, char* const* envp)
47 {
48         int nargv = 0, nenvp = 0;
49         if(argv) while(argv[nargv]) nargv++;
50         if(envp) while(envp[nenvp]) nenvp++;
51
52         if(nargv+nenvp+2 > PROCINFO_MAX_ARGP)
53                 return -1;
54
55         int pos = 0;
56         for(int i = 0; i < nargv; i++)
57         {
58                 int len = strlen(argv[i])+1;
59                 if(pos+len > PROCINFO_ARGBUF_SIZE)
60                         return -1;
61                 p->argp[i] = ((procinfo_t*)UINFO)->argbuf+pos;
62                 memcpy(p->argbuf+pos,argv[i],len);
63                 pos += len;
64         }
65         p->argp[nargv] = 0;
66
67         for(int i = 0; i < nenvp; i++)
68         {
69                 int len = strlen(envp[i])+1;
70                 if(pos+len > PROCINFO_ARGBUF_SIZE)
71                         return -1;
72                 p->argp[nargv+1+i] = ((procinfo_t*)UINFO)->argbuf+pos;
73                 memcpy(p->argbuf+pos,envp[i],len);
74                 pos += len;
75         }
76         p->argp[nargv+nenvp+1] = 0;
77         
78         return 0;
79 }
80
81 // this is how user programs access the procinfo page
82 #ifndef ROS_KERNEL
83 # define __procinfo (*(procinfo_t*)UINFO)
84 #endif
85
86 #endif // !ROS_PROCDATA_H