]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/sys/pcpu.h
Copy head to stable/8 as part of 8.0 Release cycle.
[FreeBSD/stable/8.git] / sys / sys / pcpu.h
1 /*-
2  * Copyright (c) 2001 Wind River Systems, Inc.
3  * All rights reserved.
4  * Written by: John Baldwin <jhb@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 4. Neither the name of the author nor the names of any co-contributors
15  *    may be used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * $FreeBSD$
31  */
32
33 #ifndef _SYS_PCPU_H_
34 #define _SYS_PCPU_H_
35
36 #ifdef LOCORE
37 #error "no assembler-serviceable parts inside"
38 #endif
39
40 #include <sys/queue.h>
41 #include <sys/vmmeter.h>
42 #include <sys/resource.h>
43 #include <machine/pcpu.h>
44
45 struct pcb;
46 struct thread;
47
48 /*
49  * Define a set for pcpu data.
50  * 
51  * We don't use SET_DECLARE because it defines the set as 'a' when we
52  * want 'aw'.  GCC considers uninitialized data in a seperate section
53  * writable and there is no generic zero initializer that works for
54  * structs and scalars.
55  */
56 extern uintptr_t *__start_set_pcpu;
57 extern uintptr_t *__stop_set_pcpu;
58
59 #if defined(__arm__)
60 __asm__(".section set_pcpu, \"aw\", %progbits");
61 #else
62 __asm__(".section set_pcpu, \"aw\", @progbits");
63 #endif
64 __asm__(".previous");
65
66 /*
67  * Array of dynamic pcpu base offsets.  Indexed by id.
68  */
69 extern uintptr_t dpcpu_off[];
70
71 /*
72  * Convenience defines.
73  */
74 #define DPCPU_START             (uintptr_t)&__start_set_pcpu
75 #define DPCPU_STOP              (uintptr_t)&__stop_set_pcpu
76 #define DPCPU_BYTES             (DPCPU_STOP - DPCPU_START)
77 #define DPCPU_MODMIN            2048
78 #define DPCPU_SIZE              roundup2(DPCPU_BYTES, PAGE_SIZE)
79 #define DPCPU_MODSIZE           (DPCPU_SIZE - (DPCPU_BYTES - DPCPU_MODMIN))
80
81 /*
82  * Declaration and definition.
83  */
84 #define DPCPU_NAME(n)           pcpu_entry_##n
85 #define DPCPU_DECLARE(t, n)     extern t DPCPU_NAME(n)
86 #define DPCPU_DEFINE(t, n)      t DPCPU_NAME(n) __section("set_pcpu") __used
87
88 /*
89  * Accessors with a given base.
90  */
91 #define _DPCPU_PTR(b, n)                                                \
92     (__typeof(DPCPU_NAME(n))*)((b) + (uintptr_t)&DPCPU_NAME(n))
93 #define _DPCPU_GET(b, n)        (*_DPCPU_PTR(b, n))
94 #define _DPCPU_SET(b, n, v)     (*_DPCPU_PTR(b, n) = v)
95
96 /*
97  * Accessors for the current cpu.
98  */
99 #define DPCPU_PTR(n)            _DPCPU_PTR(PCPU_GET(dynamic), n)
100 #define DPCPU_GET(n)            (*DPCPU_PTR(n))
101 #define DPCPU_SET(n, v)         (*DPCPU_PTR(n) = v)
102
103 /*
104  * Accessors for remote cpus.
105  */
106 #define DPCPU_ID_PTR(i, n)      _DPCPU_PTR(dpcpu_off[(i)], n)
107 #define DPCPU_ID_GET(i, n)      (*DPCPU_ID_PTR(i, n))
108 #define DPCPU_ID_SET(i, n, v)   (*DPCPU_ID_PTR(i, n) = v)
109
110 /* 
111  * XXXUPS remove as soon as we have per cpu variable
112  * linker sets and  can define rm_queue in _rm_lock.h
113 */
114 struct rm_queue {
115         struct rm_queue* volatile rmq_next;
116         struct rm_queue* volatile rmq_prev;
117 };
118
119 #define PCPU_NAME_LEN (sizeof("CPU ") + sizeof(__XSTRING(MAXCPU) + 1))
120
121
122 /*
123  * This structure maps out the global data that needs to be kept on a
124  * per-cpu basis.  The members are accessed via the PCPU_GET/SET/PTR
125  * macros defined in <machine/pcpu.h>.  Machine dependent fields are
126  * defined in the PCPU_MD_FIELDS macro defined in <machine/pcpu.h>.
127  */
128 struct pcpu {
129         struct thread   *pc_curthread;          /* Current thread */
130         struct thread   *pc_idlethread;         /* Idle thread */
131         struct thread   *pc_fpcurthread;        /* Fp state owner */
132         struct thread   *pc_deadthread;         /* Zombie thread or NULL */
133         struct pcb      *pc_curpcb;             /* Current pcb */
134         uint64_t        pc_switchtime;
135         int             pc_switchticks;
136         u_int           pc_cpuid;               /* This cpu number */
137         cpumask_t       pc_cpumask;             /* This cpu mask */
138         cpumask_t       pc_other_cpus;          /* Mask of all other cpus */
139         SLIST_ENTRY(pcpu) pc_allcpu;
140         struct lock_list_entry *pc_spinlocks;
141 #ifdef KTR
142         char            pc_name[PCPU_NAME_LEN]; /* String name for KTR. */
143 #endif
144         struct vmmeter  pc_cnt;                 /* VM stats counters */
145         long            pc_cp_time[CPUSTATES];  /* statclock ticks */
146         struct device   *pc_device;
147         void            *pc_netisr;             /* netisr SWI cookie. */
148
149         /* 
150          * Stuff for read mostly lock
151          * 
152          * XXXUPS remove as soon as we have per cpu variable
153          * linker sets.
154          */
155         struct rm_queue  pc_rm_queue; 
156
157         /*
158          * Dynamic per-cpu data area.
159          */
160         uintptr_t       pc_dynamic;
161
162         /*
163          * Keep MD fields last, so that CPU-specific variations on a
164          * single architecture don't result in offset variations of
165          * the machine-independent fields of the pcpu. Even though
166          * the pcpu structure is private to the kernel, some ports
167          * (e.g. lsof, part of gtop) define _KERNEL and include this
168          * header. While strictly speaking this is wrong, there's no
169          * reason not to keep the offsets of the MI fields contants.
170          * If only to make kernel debugging easier...
171          */
172         PCPU_MD_FIELDS;
173 } __aligned(128);
174
175 #ifdef _KERNEL
176
177 SLIST_HEAD(cpuhead, pcpu);
178
179 extern struct cpuhead cpuhead;
180
181 #define curcpu          PCPU_GET(cpuid)
182 #define curproc         (curthread->td_proc)
183 #ifndef curthread
184 #define curthread       PCPU_GET(curthread)
185 #endif
186 #define curvidata       PCPU_GET(vidata)
187
188 /*
189  * Machine dependent callouts.  cpu_pcpu_init() is responsible for
190  * initializing machine dependent fields of struct pcpu, and
191  * db_show_mdpcpu() is responsible for handling machine dependent
192  * fields for the DDB 'show pcpu' command.
193  */
194
195 extern struct pcpu *cpuid_to_pcpu[MAXCPU];
196
197
198 void    cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size);
199 void    db_show_mdpcpu(struct pcpu *pcpu);
200
201 void    pcpu_destroy(struct pcpu *pcpu);
202 struct  pcpu *pcpu_find(u_int cpuid);
203 void    pcpu_init(struct pcpu *pcpu, int cpuid, size_t size);
204 void    *dpcpu_alloc(int size);
205 void    dpcpu_copy(void *s, int size);
206 void    dpcpu_free(void *s, int size);
207 void    dpcpu_init(void *dpcpu, int cpuid);
208
209 #endif  /* _KERNEL */
210
211 #endif /* !_SYS_PCPU_H_ */