xref: /minix/minix/kernel/proc.h (revision 0a6a1f1d)
1 #ifndef PROC_H
2 #define PROC_H
3 
4 #include <minix/const.h>
5 #include <sys/cdefs.h>
6 
7 #ifndef __ASSEMBLY__
8 
9 /* Here is the declaration of the process table.  It contains all process
10  * data, including registers, flags, scheduling priority, memory map,
11  * accounting, message passing (IPC) information, and so on.
12  *
13  * Many assembly code routines reference fields in it.  The offsets to these
14  * fields are defined in the assembler include file sconst.h.  When changing
15  * struct proc, be sure to change sconst.h to match.
16  */
17 #include <minix/com.h>
18 #include <minix/portio.h>
19 #include "const.h"
20 #include "priv.h"
21 
22 struct proc {
23   struct stackframe_s p_reg;	/* process' registers saved in stack frame */
24   struct segframe p_seg;	/* segment descriptors */
25   proc_nr_t p_nr;		/* number of this process (for fast access) */
26   struct priv *p_priv;		/* system privileges structure */
27   volatile u32_t p_rts_flags;	/* process is runnable only if zero */
28   volatile u32_t p_misc_flags;	/* flags that do not suspend the process */
29 
30   char p_priority;		/* current process priority */
31   u64_t p_cpu_time_left;	/* time left to use the cpu */
32   unsigned p_quantum_size_ms;	/* assigned time quantum in ms
33 				   FIXME remove this */
34   struct proc *p_scheduler;	/* who should get out of quantum msg */
35   unsigned p_cpu;		/* what CPU is the process running on */
36 #ifdef CONFIG_SMP
37   bitchunk_t p_cpu_mask[BITMAP_CHUNKS(CONFIG_MAX_CPUS)]; /* what CPUs is the
38 							    process allowed to
39 							    run on */
40   bitchunk_t p_stale_tlb[BITMAP_CHUNKS(CONFIG_MAX_CPUS)]; /* On which cpu are
41 				possibly stale entries from this process and has
42 				to be fresed the next kernel touches this
43 				processes memory
44 				 */
45 #endif
46 
47   /* Accounting statistics that get passed to the process' scheduler */
48   struct {
49 	u64_t enter_queue;	/* time when enqueued (cycles) */
50 	u64_t time_in_queue;	/* time spent in queue */
51 	unsigned long dequeues;
52 	unsigned long ipc_sync;
53 	unsigned long ipc_async;
54 	unsigned long preempted;
55   } p_accounting;
56 
57   clock_t p_user_time;		/* user time in ticks */
58   clock_t p_sys_time;		/* sys time in ticks */
59 
60   clock_t p_virt_left;		/* number of ticks left on virtual timer */
61   clock_t p_prof_left;		/* number of ticks left on profile timer */
62 
63   u64_t p_cycles;		/* how many cycles did the process use */
64   u64_t p_kcall_cycles;		/* kernel cycles caused by this proc (kcall) */
65   u64_t p_kipc_cycles;		/* cycles caused by this proc (ipc) */
66 
67   struct proc *p_nextready;	/* pointer to next ready process */
68   struct proc *p_caller_q;	/* head of list of procs wishing to send */
69   struct proc *p_q_link;	/* link to next proc wishing to send */
70   endpoint_t p_getfrom_e;	/* from whom does process want to receive? */
71   endpoint_t p_sendto_e;	/* to whom does process want to send? */
72 
73   sigset_t p_pending;		/* bit map for pending kernel signals */
74 
75   char p_name[PROC_NAME_LEN];	/* name of the process, including \0 */
76 
77   endpoint_t p_endpoint;	/* endpoint number, generation-aware */
78 
79   message p_sendmsg;		/* Message from this process if SENDING */
80   message p_delivermsg;		/* Message for this process if MF_DELIVERMSG */
81   vir_bytes p_delivermsg_vir;	/* Virtual addr this proc wants message at */
82 
83   /* If handler functions detect a process wants to do something with
84    * memory that isn't present, VM has to fix it. Until it has asked
85    * what needs to be done and fixed it, save necessary state here.
86    *
87    * The requester gets a copy of its request message in reqmsg and gets
88    * VMREQUEST set.
89    */
90   struct {
91 	struct proc	*nextrestart;	/* next in vmrestart chain */
92 	struct proc	*nextrequestor;	/* next in vmrequest chain */
93 #define VMSTYPE_SYS_NONE	0
94 #define VMSTYPE_KERNELCALL	1
95 #define VMSTYPE_DELIVERMSG	2
96 #define VMSTYPE_MAP		3
97 
98 	int		type;		/* suspended operation */
99 	union ixfer_saved{
100 		/* VMSTYPE_SYS_MESSAGE */
101 		message		reqmsg;	/* suspended request message */
102 	} saved;
103 
104 	/* Parameters of request to VM */
105 	int		req_type;
106 	endpoint_t	target;
107 	union ixfer_params{
108 		struct {
109 			vir_bytes 	start, length;	/* memory range */
110 			u8_t		writeflag;	/* nonzero for write access */
111 		} check;
112 	} params;
113 	/* VM result when available */
114 	int		vmresult;
115 
116 	/* If the suspended operation is a sys_call, its details are
117 	 * stored here.
118 	 */
119   } p_vmrequest;
120 
121   int p_found;	/* consistency checking variables */
122   int p_magic;		/* check validity of proc pointers */
123 
124   /* if MF_SC_DEFER is set, this struct is valid and contains the
125    * do_ipc() arguments that are still to be executed
126    */
127   struct { reg_t r1, r2, r3; } p_defer;
128 
129 #if DEBUG_TRACE
130   int p_schedules;
131 #endif
132 };
133 
134 #endif /* __ASSEMBLY__ */
135 
136 /* Bits for the runtime flags. A process is runnable iff p_rts_flags == 0. */
137 #define RTS_SLOT_FREE	0x01	/* process slot is free */
138 #define RTS_PROC_STOP	0x02	/* process has been stopped */
139 #define RTS_SENDING	0x04	/* process blocked trying to send */
140 #define RTS_RECEIVING	0x08	/* process blocked trying to receive */
141 #define RTS_SIGNALED	0x10	/* set when new kernel signal arrives */
142 #define RTS_SIG_PENDING	0x20	/* unready while signal being processed */
143 #define RTS_P_STOP	0x40	/* set when process is being traced */
144 #define RTS_NO_PRIV	0x80	/* keep forked system process from running */
145 #define RTS_NO_ENDPOINT	0x100	/* process cannot send or receive messages */
146 #define RTS_VMINHIBIT	0x200	/* not scheduled until pagetable set by VM */
147 #define RTS_PAGEFAULT	0x400	/* process has unhandled pagefault */
148 #define RTS_VMREQUEST	0x800	/* originator of vm memory request */
149 #define RTS_VMREQTARGET	0x1000	/* target of vm memory request */
150 #define RTS_PREEMPTED	0x4000	/* this process was preempted by a higher
151 				   priority process and we should pick a new one
152 				   to run. Processes with this flag should be
153 				   returned to the front of their current
154 				   priority queue if they are still runnable
155 				   before we pick a new one
156 				 */
157 #define RTS_NO_QUANTUM	0x8000	/* process ran out of its quantum and we should
158 				   pick a new one. Process was dequeued and
159 				   should be enqueued at the end of some run
160 				   queue again */
161 #define RTS_BOOTINHIBIT	0x10000	/* not ready until VM has made it */
162 
163 /* A process is runnable iff p_rts_flags == 0. */
164 #define rts_f_is_runnable(flg)	((flg) == 0)
165 #define proc_is_runnable(p)	(rts_f_is_runnable((p)->p_rts_flags))
166 
167 #define proc_is_preempted(p)	((p)->p_rts_flags & RTS_PREEMPTED)
168 #define proc_no_quantum(p)	((p)->p_rts_flags & RTS_NO_QUANTUM)
169 #define proc_ptr_ok(p)		((p)->p_magic == PMAGIC)
170 #define proc_used_fpu(p)	((p)->p_misc_flags & (MF_FPU_INITIALIZED))
171 
172 /* test whether the process is scheduled by the kernel's default policy  */
173 #define proc_kernel_scheduler(p)	((p)->p_scheduler == NULL || \
174 					(p)->p_scheduler == (p))
175 
176 /* Macro to return: on which process is a certain process blocked?
177  * return endpoint number (can be ANY) or NONE. It's important to
178  * check RTS_SENDING first, and then RTS_RECEIVING, as they could
179  * both be on (if a ipc_sendrec() blocks on sending), and p_getfrom_e
180  * could be nonsense even though RTS_RECEIVING is on.
181  */
182 #define P_BLOCKEDON(p)							\
183 	(								\
184 		((p)->p_rts_flags & RTS_SENDING) ? 			\
185 		(p)->p_sendto_e : 					\
186 		(							\
187 			(						\
188 				((p)->p_rts_flags & RTS_RECEIVING) ?	\
189 				(p)->p_getfrom_e : 			\
190 				NONE					\
191 			) 						\
192 		)							\
193 	)
194 
195 /* These runtime flags can be tested and manipulated by these macros. */
196 
197 #define RTS_ISSET(rp, f) (((rp)->p_rts_flags & (f)) == (f))
198 
199 
200 /* Set flag and dequeue if the process was runnable. */
201 #define RTS_SET(rp, f)							\
202 	do {								\
203 		const int rts = (rp)->p_rts_flags;			\
204 		(rp)->p_rts_flags |= (f);				\
205 		if(rts_f_is_runnable(rts) && !proc_is_runnable(rp)) {	\
206 			dequeue(rp);					\
207 		}							\
208 	} while(0)
209 
210 /* Clear flag and enqueue if the process was not runnable but is now. */
211 #define RTS_UNSET(rp, f) 						\
212 	do {								\
213 		int rts;						\
214 		rts = (rp)->p_rts_flags;				\
215 		(rp)->p_rts_flags &= ~(f);				\
216 		if(!rts_f_is_runnable(rts) && proc_is_runnable(rp)) {	\
217 			enqueue(rp);					\
218 		}							\
219 	} while(0)
220 
221 /* Set flags to this value. */
222 #define RTS_SETFLAGS(rp, f)					\
223 	do {								\
224 		if(proc_is_runnable(rp) && (f)) { dequeue(rp); }		\
225 		(rp)->p_rts_flags = (f);				\
226 	} while(0)
227 
228 /* Misc flags */
229 #define MF_REPLY_PEND	0x001	/* reply to IPC_REQUEST is pending */
230 #define MF_VIRT_TIMER	0x002	/* process-virtual timer is running */
231 #define MF_PROF_TIMER	0x004	/* process-virtual profile timer is running */
232 #define MF_KCALL_RESUME 0x008	/* processing a kernel call was interrupted,
233 				   most likely because we need VM to resolve a
234 				   problem or a long running copy was preempted.
235 				   We need to resume the kernel call execution
236 				   now
237 				 */
238 #define MF_DELIVERMSG	0x040	/* Copy message for him before running */
239 #define MF_SIG_DELAY	0x080	/* Send signal when no longer sending */
240 #define MF_SC_ACTIVE	0x100	/* Syscall tracing: in a system call now */
241 #define MF_SC_DEFER	0x200	/* Syscall tracing: deferred system call */
242 #define MF_SC_TRACE	0x400	/* Syscall tracing: trigger syscall events */
243 #define MF_FPU_INITIALIZED	0x1000  /* process already used math, so fpu
244 					 * regs are significant (initialized)*/
245 #define MF_SENDING_FROM_KERNEL	0x2000 /* message of this process is from kernel */
246 #define MF_CONTEXT_SET	0x4000 /* don't touch context */
247 #define MF_SPROF_SEEN	0x8000 /* profiling has seen this process */
248 #define MF_FLUSH_TLB	0x10000	/* if set, TLB must be flushed before letting
249 				   this process run again. Currently it only
250 				   applies to SMP */
251 #define MF_SENDA_VM_MISS 0x20000 /* set if a processes wanted to receive an asyn
252 				    message from this sender but could not
253 				    because of VM modifying the sender's address
254 				    space*/
255 #define MF_STEP		 0x40000 /* Single-step process */
256 #define MF_MSGFAILED	 0x80000
257 
258 /* Magic process table addresses. */
259 #define BEG_PROC_ADDR (&proc[0])
260 #define BEG_USER_ADDR (&proc[NR_TASKS])
261 #define END_PROC_ADDR (&proc[NR_TASKS + NR_PROCS])
262 
263 #define proc_addr(n)      (&(proc[NR_TASKS + (n)]))
264 #define proc_nr(p) 	  ((p)->p_nr)
265 
266 #define isokprocn(n)      ((unsigned) ((n) + NR_TASKS) < NR_PROCS + NR_TASKS)
267 #define isemptyn(n)       isemptyp(proc_addr(n))
268 #define isemptyp(p)       ((p)->p_rts_flags == RTS_SLOT_FREE)
269 #define iskernelp(p)	  ((p) < BEG_USER_ADDR)
270 #define iskerneln(n)	  ((n) < 0)
271 #define isuserp(p)        isusern((p) >= BEG_USER_ADDR)
272 #define isusern(n)        ((n) >= 0)
273 #define isrootsysn(n)	  ((n) == ROOT_SYS_PROC_NR)
274 
275 #ifndef __ASSEMBLY__
276 
277 EXTERN struct proc proc[NR_TASKS + NR_PROCS];	/* process table */
278 
279 int mini_send(struct proc *caller_ptr, endpoint_t dst_e, message *m_ptr,
280 	int flags);
281 
282 #endif /* __ASSEMBLY__ */
283 
284 #endif /* PROC_H */
285