1 /*- 2 * Copyright (c) 1990 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * William Jolitz. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * from: @(#)param.h 5.8 (Berkeley) 6/28/91 37 * $Id: param.h,v 1.39 1997/08/29 11:01:14 kato Exp $ 38 */ 39 40 #ifndef _MACHINE_PARAM_H_ 41 #define _MACHINE_PARAM_H_ 42 43 /* 44 * Machine dependent constants for Intel 386. 45 */ 46 47 #define MACHINE "i386" 48 #ifdef SMP 49 #define NCPUS 2 50 #else 51 #define NCPUS 1 52 #endif 53 #define MID_MACHINE MID_I386 54 55 #ifndef LOCORE 56 57 /* 58 * Round p (pointer or byte index) up to a correctly-aligned value 59 * for all data types (int, long, ...). The result is unsigned int 60 * and must be cast to any desired pointer type. 61 */ 62 #define ALIGNBYTES (sizeof(int) - 1) 63 #define ALIGN(p) (((unsigned)(p) + ALIGNBYTES) & ~ALIGNBYTES) 64 65 #define PAGE_SHIFT 12 /* LOG2(PAGE_SIZE) */ 66 #define PAGE_SIZE (1<<PAGE_SHIFT) /* bytes/page */ 67 #define PAGE_MASK (PAGE_SIZE-1) 68 #define NPTEPG (PAGE_SIZE/(sizeof (pt_entry_t))) 69 70 #define NPDEPG (PAGE_SIZE/(sizeof (pd_entry_t))) 71 #define PDRSHIFT 22 /* LOG2(NBPDR) */ 72 #define NBPDR (1<<PDRSHIFT) /* bytes/page dir */ 73 #define PDRMASK (NBPDR-1) 74 75 #define DEV_BSHIFT 9 /* log2(DEV_BSIZE) */ 76 #define DEV_BSIZE (1<<DEV_BSHIFT) 77 78 #define BLKDEV_IOSIZE 2048 79 #define MAXPHYS (64 * 1024) /* max raw I/O transfer size */ 80 81 #define IOPAGES 2 /* pages of i/o permission bitmap */ 82 #define UPAGES 2 /* pages of u-area */ 83 #define UPAGES_HOLE 2 /* pages of "hole" at top of user space where */ 84 /* the upages used to be. DO NOT CHANGE! */ 85 86 /* 87 * Constants related to network buffer management. 88 * MCLBYTES must be no larger than CLBYTES (the software page size), and, 89 * on machines that exchange pages of input or output buffers with mbuf 90 * clusters (MAPPED_MBUFS), MCLBYTES must also be an integral multiple 91 * of the hardware page size. 92 */ 93 #ifndef MSIZE 94 #define MSIZE 128 /* size of an mbuf */ 95 #endif /* MSIZE */ 96 97 #ifndef MCLSHIFT 98 #define MCLSHIFT 11 /* convert bytes to m_buf clusters */ 99 #endif /* MCLSHIFT */ 100 #define MCLBYTES (1 << MCLSHIFT) /* size of an m_buf cluster */ 101 #define MCLOFSET (MCLBYTES - 1) /* offset within an m_buf cluster */ 102 103 /* 104 * Some macros for units conversion 105 */ 106 107 /* clicks to bytes */ 108 #define ctob(x) ((x)<<PAGE_SHIFT) 109 110 /* bytes to clicks */ 111 #define btoc(x) (((unsigned)(x)+PAGE_MASK)>>PAGE_SHIFT) 112 113 /* 114 * btodb() is messy and perhaps slow because `bytes' may be an off_t. We 115 * want to shift an unsigned type to avoid sign extension and we don't 116 * want to widen `bytes' unnecessarily. Assume that the result fits in 117 * a daddr_t. 118 */ 119 #define btodb(bytes) /* calculates (bytes / DEV_BSIZE) */ \ 120 (sizeof (bytes) > sizeof(long) \ 121 ? (daddr_t)((unsigned long long)(bytes) >> DEV_BSHIFT) \ 122 : (daddr_t)((unsigned long)(bytes) >> DEV_BSHIFT)) 123 124 #define dbtob(db) /* calculates (db * DEV_BSIZE) */ \ 125 ((off_t)(db) << DEV_BSHIFT) 126 127 /* 128 * Mach derived conversion macros 129 */ 130 #define trunc_page(x) ((unsigned)(x) & ~PAGE_MASK) 131 #define round_page(x) ((((unsigned)(x)) + PAGE_MASK) & ~PAGE_MASK) 132 #define trunc_4mpage(x) ((unsigned)(x) & ~PDRMASK) 133 #define round_4mpage(x) ((((unsigned)(x)) + PDRMASK) & ~PDRMASK) 134 135 #define atop(x) ((unsigned)(x) >> PAGE_SHIFT) 136 #define ptoa(x) ((unsigned)(x) << PAGE_SHIFT) 137 138 #define i386_btop(x) ((unsigned)(x) >> PAGE_SHIFT) 139 #define i386_ptob(x) ((unsigned)(x) << PAGE_SHIFT) 140 141 #endif /* !LOCORE */ 142 143 144 #ifndef _SIMPLELOCK_H_ 145 #define _SIMPLELOCK_H_ 146 147 /* 148 * XXX some temp debug control of cpl locks 149 */ 150 #define REAL_ECPL /* exception.s: SCPL_LOCK/SCPL_UNLOCK */ 151 #define REAL_ICPL /* ipl.s: CPL_LOCK/CPL_UNLOCK/FAST */ 152 #define REAL_AICPL /* apic_ipl.s: SCPL_LOCK/SCPL_UNLOCK */ 153 #define REAL_AVCPL /* apic_vector.s: CPL_LOCK/CPL_UNLOCK */ 154 155 #define REAL_IFCPL /* ipl_funcs.c: SCPL_LOCK/SCPL_UNLOCK */ 156 157 #define REAL_MCPL_NOT /* microtime.s: CPL_LOCK/movl $0,_cpl_lock */ 158 159 160 #ifdef LOCORE 161 162 #ifdef SMP 163 164 #define MPLOCKED lock ; 165 166 /* 167 * Some handy macros to allow logical organization and 168 * convenient reassignment of various locks. 169 */ 170 171 #define FPU_LOCK call _get_fpu_lock 172 #define ALIGN_LOCK call _get_align_lock 173 #define SYSCALL_LOCK call _get_syscall_lock 174 #define ALTSYSCALL_LOCK call _get_altsyscall_lock 175 176 /* 177 * Protects INTR() ISRs. 178 */ 179 #define ISR_TRYLOCK \ 180 pushl $_mp_lock ; /* GIANT_LOCK */ \ 181 call _MPtrylock ; /* try to get lock */ \ 182 add $4, %esp 183 184 #define ISR_RELLOCK \ 185 pushl $_mp_lock ; /* GIANT_LOCK */ \ 186 call _MPrellock ; \ 187 add $4, %esp 188 189 /* 190 * Protects the IO APIC and apic_imen as a critical region. 191 */ 192 #define IMASK_LOCK \ 193 pushl $_imen_lock ; /* address of lock */ \ 194 call _s_lock ; /* MP-safe */ \ 195 addl $4, %esp 196 197 #define IMASK_UNLOCK \ 198 pushl $_imen_lock ; /* address of lock */ \ 199 call _s_unlock ; /* MP-safe */ \ 200 addl $4, %esp 201 202 /* 203 * Variations of CPL_LOCK protect spl updates as a critical region. 204 * Items within this 'region' include: 205 * cpl 206 * cil 207 * ipending 208 * ??? 209 */ 210 211 /* 212 * Botom half routines, ie. those already protected from INTs. 213 * 214 * Used in: 215 * sys/i386/i386/microtime.s (XXX currently NOT used, possible race?) 216 * sys/i386/isa/ipl.s: _doreti 217 * sys/i386/isa/apic_vector.s: _Xintr0, ..., _Xintr23 218 */ 219 #define CPL_LOCK \ 220 pushl $_cpl_lock ; /* address of lock */ \ 221 call _s_lock ; /* MP-safe */ \ 222 addl $4, %esp 223 224 #define CPL_UNLOCK \ 225 pushl $_cpl_lock ; /* address of lock */ \ 226 call _s_unlock ; /* MP-safe */ \ 227 addl $4, %esp 228 229 /* 230 * INT safe version for top half of kernel. 231 * 232 * Used in: 233 * sys/i386/i386/exception.s: _Xfpu, _Xalign, _Xsyscall, _Xint0x80_syscall 234 * sys/i386/isa/apic_ipl.s: splz() 235 */ 236 #define SCPL_LOCK \ 237 pushl $_cpl_lock ; \ 238 call _ss_lock ; \ 239 addl $4, %esp 240 241 #define SCPL_UNLOCK \ 242 pushl $_cpl_lock ; \ 243 call _ss_unlock ; \ 244 addl $4, %esp 245 246 #else /* SMP */ 247 248 #define MPLOCKED /* NOP */ 249 250 #define FPU_LOCK /* NOP */ 251 #define ALIGN_LOCK /* NOP */ 252 #define SYSCALL_LOCK /* NOP */ 253 #define ALTSYSCALL_LOCK /* NOP */ 254 255 #endif /* SMP */ 256 257 #else /* LOCORE */ 258 259 #ifdef SMP 260 261 /* 262 * Protects cpl/cil/ipending data as a critical region. 263 * 264 * Used in: 265 * sys/i386/isa/ipl_funcs.c: DO_SETBITS, softclockpending(), GENSPL, 266 * spl0(), splx(), splq() 267 */ 268 269 /* Bottom half */ 270 #define CPL_LOCK() s_lock(&cpl_lock) 271 #define CPL_UNLOCK() s_unlock(&cpl_lock) 272 273 /* INT safe version for top half of kernel */ 274 #define SCPL_LOCK() ss_lock(&cpl_lock) 275 #define SCPL_UNLOCK() ss_unlock(&cpl_lock) 276 277 /* 278 * Protects com/tty data as a critical region. 279 */ 280 #define COM_LOCK() s_lock(&com_lock) 281 #define COM_UNLOCK() s_unlock(&com_lock) 282 283 #else /* SMP */ 284 285 #define CPL_LOCK() 286 #define CPL_UNLOCK() 287 #define SCPL_LOCK() 288 #define SCPL_UNLOCK() 289 290 #define COM_LOCK() 291 #define COM_UNLOCK() 292 293 #endif /* SMP */ 294 295 /* 296 * A simple spin lock. 297 * 298 * This structure only sets one bit of data, but is sized based on the 299 * minimum word size that can be operated on by the hardware test-and-set 300 * instruction. It is only needed for multiprocessors, as uniprocessors 301 * will always run to completion or a sleep. It is an error to hold one 302 * of these locks while a process is sleeping. 303 */ 304 struct simplelock { 305 volatile int lock_data; 306 }; 307 308 /* functions in simplelock.s */ 309 void s_lock_init __P((struct simplelock *)); 310 void s_lock __P((struct simplelock *)); 311 int s_lock_try __P((struct simplelock *)); 312 void s_unlock __P((struct simplelock *)); 313 void ss_lock __P((struct simplelock *)); 314 void ss_unlock __P((struct simplelock *)); 315 316 /* global data in mp_machdep.c */ 317 extern struct simplelock imen_lock; 318 extern struct simplelock cpl_lock; 319 extern struct simplelock fast_intr_lock; 320 extern struct simplelock intr_lock; 321 extern struct simplelock com_lock; 322 323 #if !defined(SIMPLELOCK_DEBUG) && NCPUS > 1 324 /* 325 * The simple-lock routines are the primitives out of which the lock 326 * package is built. The machine-dependent code must implement an 327 * atomic test_and_set operation that indivisibly sets the simple lock 328 * to non-zero and returns its old value. It also assumes that the 329 * setting of the lock to zero below is indivisible. Simple locks may 330 * only be used for exclusive locks. 331 */ 332 333 #ifdef the_original_code 334 335 static __inline void 336 simple_lock_init(struct simplelock *lkp) 337 { 338 339 lkp->lock_data = 0; 340 } 341 342 static __inline void 343 simple_lock(struct simplelock *lkp) 344 { 345 346 while (test_and_set(&lkp->lock_data)) 347 continue; 348 } 349 350 static __inline int 351 simple_lock_try(struct simplelock *lkp) 352 { 353 354 return (!test_and_set(&lkp->lock_data)); 355 } 356 357 static __inline void 358 simple_unlock(struct simplelock *lkp) 359 { 360 361 lkp->lock_data = 0; 362 } 363 364 #else /* the_original_code */ 365 366 /* 367 * This set of defines turns on the real functions in i386/isa/apic_ipl.s. 368 * It has never actually been tested. 369 */ 370 #define simple_lock_init(alp) s_lock_init(alp) 371 #define simple_lock(alp) s_lock(alp) 372 #define simple_lock_try(alp) s_lock_try(alp) 373 #define simple_unlock(alp) s_unlock(alp) 374 375 #endif /* the_original_code */ 376 377 #endif /* NCPUS > 1 */ 378 #endif /* LOCORE */ 379 #endif /* !_SIMPLELOCK_H_ */ 380 381 #endif /* !_MACHINE_PARAM_H_ */ 382