Vcores always start in vcore context
[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/atomic.h>
9 #include <ros/arch/arch.h>
10
11 #define PROCINFO_MAX_ARGP 32
12 #define PROCINFO_ARGBUF_SIZE 3072
13
14 #ifdef ROS_KERNEL
15 #include <sys/queue.h>
16 #endif /* ROS_KERNEL */
17
18 /* Not necessary to expose all of this, but it doesn't hurt, and is convenient
19  * for the kernel.  Need to do some acrobatics for the TAILQ_ENTRY. */
20 struct vcore;
21 struct vcore {
22 #ifdef ROS_KERNEL
23         TAILQ_ENTRY(vcore)      list;
24 #else /* userspace */
25         void                            *dummy_ptr1;
26         void                            *dummy_ptr2;
27 #endif /* ROS_KERNEL */
28         uint32_t                        pcoreid;
29         bool                            valid;
30         bool                            preempt_served;
31         uint64_t                        preempt_pending;
32 };
33
34 struct pcore {
35         uint32_t                        vcoreid;
36         bool                            valid;
37 };
38
39 typedef struct procinfo {
40         pid_t pid;
41         pid_t ppid;
42         size_t max_vcores;
43         uint64_t tsc_freq;
44         void* heap_bottom;
45         /* for traditional forks, these two need to be memcpy'd over: */
46         char* argp[PROCINFO_MAX_ARGP];
47         char argbuf[PROCINFO_ARGBUF_SIZE];
48         /* glibc relies on stuff above this point.  if you change it, you need to
49          * rebuild glibc. */
50         bool is_mcp;                    /* is in multi mode */
51         struct vcore            vcoremap[MAX_NUM_CPUS];
52         uint32_t                        num_vcores;
53         struct pcore            pcoremap[MAX_NUM_CPUS];
54         seq_ctr_t                       coremap_seqctr;
55 } procinfo_t;
56 #define PROCINFO_NUM_PAGES  ((sizeof(procinfo_t)-1)/PGSIZE + 1) 
57
58 static int
59 procinfo_pack_args(procinfo_t* p, char* const* argv, char* const* envp)
60 {
61         int nargv = 0, nenvp = 0;
62         if(argv) while(argv[nargv]) nargv++;
63         if(envp) while(envp[nenvp]) nenvp++;
64
65         if(nargv+nenvp+2 > PROCINFO_MAX_ARGP)
66                 return -1;
67
68         int pos = 0;
69         for(int i = 0; i < nargv; i++)
70         {
71                 int len = strlen(argv[i])+1;
72                 if(pos+len > PROCINFO_ARGBUF_SIZE)
73                         return -1;
74                 p->argp[i] = ((procinfo_t*)UINFO)->argbuf+pos;
75                 memcpy(p->argbuf+pos,argv[i],len);
76                 pos += len;
77         }
78         p->argp[nargv] = 0;
79
80         for(int i = 0; i < nenvp; i++)
81         {
82                 int len = strlen(envp[i])+1;
83                 if(pos+len > PROCINFO_ARGBUF_SIZE)
84                         return -1;
85                 p->argp[nargv+1+i] = ((procinfo_t*)UINFO)->argbuf+pos;
86                 memcpy(p->argbuf+pos,envp[i],len);
87                 pos += len;
88         }
89         p->argp[nargv+nenvp+1] = 0;
90         
91         return 0;
92 }
93
94 // this is how user programs access the procinfo page
95 #ifndef ROS_KERNEL
96 # define __procinfo (*(procinfo_t*)UINFO)
97 #endif
98
99 #endif // !ROS_PROCDATA_H