Vcore timers are stored in ticks (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         uint32_t                        nr_preempts_sent;       /* these two differ when a preempt*/
33         uint32_t                        nr_preempts_done;       /* is in flight. */
34         uint64_t                        preempt_pending;
35         /* A process can see cumulative runtime as of the last resume, and can also
36          * calculate runtime in this interval, by adding (ns - resume) + total. */
37         uint64_t                        resume_ticks;           /* TSC at resume time */
38         uint64_t                        total_ticks;            /* ticks up to last offlining */
39 };
40
41 struct pcore {
42         uint32_t                        vcoreid;
43         bool                            valid;
44 };
45
46 typedef struct procinfo {
47         pid_t pid;
48         pid_t ppid;
49         size_t max_vcores;      /* TODO: change to a uint32_t */
50         uint64_t tsc_freq;
51         uint64_t timing_overhead;
52         void *heap_bottom;
53         /* for traditional forks, these two need to be memcpy'd over: */
54         char *argp[PROCINFO_MAX_ARGP];
55         char argbuf[PROCINFO_ARGBUF_SIZE];
56         /* glibc relies on stuff above this point.  if you change it, you need to
57          * rebuild glibc. */
58         bool is_mcp;                    /* is in multi mode */
59         unsigned long           res_grant[MAX_NUM_RESOURCES];
60         struct vcore            vcoremap[MAX_NUM_CPUS];
61         uint32_t                        num_vcores;
62         struct pcore            pcoremap[MAX_NUM_CPUS];
63         seq_ctr_t                       coremap_seqctr;
64 } procinfo_t;
65 #define PROCINFO_NUM_PAGES  ((sizeof(procinfo_t)-1)/PGSIZE + 1) 
66
67 static int
68 procinfo_pack_args(procinfo_t* p, char* const* argv, char* const* envp)
69 {
70         int nargv = 0, nenvp = 0;
71         if(argv) while(argv[nargv]) nargv++;
72         if(envp) while(envp[nenvp]) nenvp++;
73
74         if(nargv+nenvp+2 > PROCINFO_MAX_ARGP)
75                 return -1;
76
77         int pos = 0;
78         int i;
79         for(i = 0; i < nargv; i++)
80         {
81                 int len = strlen(argv[i])+1;
82                 if(pos+len > PROCINFO_ARGBUF_SIZE)
83                         return -1;
84                 p->argp[i] = ((procinfo_t*)UINFO)->argbuf+pos;
85                 memcpy(p->argbuf+pos,argv[i],len);
86                 pos += len;
87         }
88         p->argp[nargv] = 0;
89
90         for(i = 0; i < nenvp; i++)
91         {
92                 int len = strlen(envp[i])+1;
93                 if(pos+len > PROCINFO_ARGBUF_SIZE)
94                         return -1;
95                 p->argp[nargv+1+i] = ((procinfo_t*)UINFO)->argbuf+pos;
96                 memcpy(p->argbuf+pos,envp[i],len);
97                 pos += len;
98         }
99         p->argp[nargv+nenvp+1] = 0;
100         
101         return 0;
102 }
103
104 // this is how user programs access the procinfo page
105 #ifndef ROS_KERNEL
106 # define __procinfo (*(procinfo_t*)UINFO)
107
108 #include <ros/common.h>
109 #include <ros/atomic.h>
110 #include <ros/syscall.h>
111
112 /* Figure out what your vcoreid is from your pcoreid and procinfo.  Only low
113  * level or debugging code should call this. */
114 static inline uint32_t __get_vcoreid_from_procinfo(void)
115 {
116         /* The assumption is that any IPIs/KMSGs would knock userspace into the
117          * kernel before it could read the closing of the seqctr.  Put another way,
118          * there is a 'memory barrier' between the IPI write and the seqctr write.
119          * I think this is true. */
120         uint32_t kpcoreid, kvcoreid;
121         seq_ctr_t old_seq;
122         do {
123                 cmb();
124                 old_seq = __procinfo.coremap_seqctr;
125                 kpcoreid = __ros_syscall_noerrno(SYS_getpcoreid, 0, 0, 0, 0, 0, 0);
126                 if (!__procinfo.pcoremap[kpcoreid].valid)
127                         continue;
128                 kvcoreid = __procinfo.pcoremap[kpcoreid].vcoreid;
129         } while (seqctr_retry(old_seq, __procinfo.coremap_seqctr));
130         return kvcoreid;
131 }
132
133 static inline uint32_t __get_vcoreid(void)
134 {
135         /* since sys_getvcoreid could lie (and might never change) */
136         return __get_vcoreid_from_procinfo();
137 }
138
139 #endif /* ifndef ROS_KERNEL */
140
141 #endif // !ROS_PROCDATA_H