Helpers to determine vcoreid (XCC)
[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/resource.h>
9 #include <ros/atomic.h>
10 #include <ros/arch/arch.h>
11 #include <string.h>
12
13 #define PROCINFO_MAX_ARGP 32
14 #define PROCINFO_ARGBUF_SIZE 3072
15
16 #ifdef ROS_KERNEL
17 #include <sys/queue.h>
18 #endif /* ROS_KERNEL */
19
20 /* Not necessary to expose all of this, but it doesn't hurt, and is convenient
21  * for the kernel.  Need to do some acrobatics for the TAILQ_ENTRY. */
22 struct vcore;
23 struct vcore {
24 #ifdef ROS_KERNEL
25         TAILQ_ENTRY(vcore)      list;
26 #else /* userspace */
27         void                            *dummy_ptr1;
28         void                            *dummy_ptr2;
29 #endif /* ROS_KERNEL */
30         uint32_t                        pcoreid;
31         bool                            valid;
32         bool                            preempt_served;
33         uint64_t                        preempt_pending;
34 };
35
36 struct pcore {
37         uint32_t                        vcoreid;
38         bool                            valid;
39 };
40
41 typedef struct procinfo {
42         pid_t pid;
43         pid_t ppid;
44         size_t max_vcores;
45         uint64_t tsc_freq;
46         void* heap_bottom;
47         /* for traditional forks, these two need to be memcpy'd over: */
48         char* argp[PROCINFO_MAX_ARGP];
49         char argbuf[PROCINFO_ARGBUF_SIZE];
50         /* glibc relies on stuff above this point.  if you change it, you need to
51          * rebuild glibc. */
52         bool is_mcp;                    /* is in multi mode */
53         unsigned long           res_grant[MAX_NUM_RESOURCES];
54         struct vcore            vcoremap[MAX_NUM_CPUS];
55         uint32_t                        num_vcores;
56         struct pcore            pcoremap[MAX_NUM_CPUS];
57         seq_ctr_t                       coremap_seqctr;
58 } procinfo_t;
59 #define PROCINFO_NUM_PAGES  ((sizeof(procinfo_t)-1)/PGSIZE + 1) 
60
61 static int
62 procinfo_pack_args(procinfo_t* p, char* const* argv, char* const* envp)
63 {
64         int nargv = 0, nenvp = 0;
65         if(argv) while(argv[nargv]) nargv++;
66         if(envp) while(envp[nenvp]) nenvp++;
67
68         if(nargv+nenvp+2 > PROCINFO_MAX_ARGP)
69                 return -1;
70
71         int pos = 0;
72         for(int i = 0; i < nargv; i++)
73         {
74                 int len = strlen(argv[i])+1;
75                 if(pos+len > PROCINFO_ARGBUF_SIZE)
76                         return -1;
77                 p->argp[i] = ((procinfo_t*)UINFO)->argbuf+pos;
78                 memcpy(p->argbuf+pos,argv[i],len);
79                 pos += len;
80         }
81         p->argp[nargv] = 0;
82
83         for(int i = 0; i < nenvp; i++)
84         {
85                 int len = strlen(envp[i])+1;
86                 if(pos+len > PROCINFO_ARGBUF_SIZE)
87                         return -1;
88                 p->argp[nargv+1+i] = ((procinfo_t*)UINFO)->argbuf+pos;
89                 memcpy(p->argbuf+pos,envp[i],len);
90                 pos += len;
91         }
92         p->argp[nargv+nenvp+1] = 0;
93         
94         return 0;
95 }
96
97 // this is how user programs access the procinfo page
98 #ifndef ROS_KERNEL
99 # define __procinfo (*(procinfo_t*)UINFO)
100
101 #include <ros/common.h>
102 #include <ros/atomic.h>
103 #include <ros/syscall.h>
104
105 /* Figure out what your vcoreid is from your pcoreid and procinfo.  Only low
106  * level or debugging code should call this. */
107 static inline uint32_t __get_vcoreid_from_procinfo(void)
108 {
109         /* The assumption is that any IPIs/KMSGs would knock userspace into the
110          * kernel before it could read the closing of the seqctr.  Put another way,
111          * there is a 'memory barrier' between the IPI write and the seqctr write.
112          * I think this is true. */
113         uint32_t kpcoreid, kvcoreid;
114         seq_ctr_t old_seq;
115         do {
116                 cmb();
117                 old_seq = __procinfo.coremap_seqctr;
118                 kpcoreid = __ros_syscall(SYS_getpcoreid, 0, 0, 0, 0, 0, 0, NULL);
119                 if (!__procinfo.pcoremap[kpcoreid].valid)
120                         continue;
121                 kvcoreid = __procinfo.pcoremap[kpcoreid].vcoreid;
122         } while (seqctr_retry(old_seq, __procinfo.coremap_seqctr));
123         return kvcoreid;
124 }
125
126 static inline uint32_t __get_vcoreid(void)
127 {
128         /* since sys_getvcoreid could lie (and might never change) */
129         return __get_vcoreid_from_procinfo();
130 }
131
132 #endif /* ifndef ROS_KERNEL */
133
134 #endif // !ROS_PROCDATA_H