17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5b5b48cc1Ssudheer * Common Development and Distribution License (the "License"). 6b5b48cc1Ssudheer * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22b5b48cc1Ssudheer * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 24*263f549eSPatrick Mooney * Copyright 2016 Joyent, Inc. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #ifndef _SYS_MACHLOCK_H 287c478bd9Sstevel@tonic-gate #define _SYS_MACHLOCK_H 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 317c478bd9Sstevel@tonic-gate 327c478bd9Sstevel@tonic-gate #ifndef _ASM 337c478bd9Sstevel@tonic-gate #include <sys/types.h> 347c478bd9Sstevel@tonic-gate #include <sys/time.h> 357c478bd9Sstevel@tonic-gate #endif /* _ASM */ 367c478bd9Sstevel@tonic-gate 377c478bd9Sstevel@tonic-gate #ifdef __cplusplus 387c478bd9Sstevel@tonic-gate extern "C" { 397c478bd9Sstevel@tonic-gate #endif 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate #ifndef _ASM 427c478bd9Sstevel@tonic-gate 437c478bd9Sstevel@tonic-gate #ifdef _KERNEL 447c478bd9Sstevel@tonic-gate 457c478bd9Sstevel@tonic-gate extern void lock_set(lock_t *lp); 467c478bd9Sstevel@tonic-gate extern int lock_try(lock_t *lp); 477c478bd9Sstevel@tonic-gate extern int lock_spin_try(lock_t *lp); 487c478bd9Sstevel@tonic-gate extern int ulock_try(lock_t *lp); 497c478bd9Sstevel@tonic-gate extern void lock_clear(lock_t *lp); 507c478bd9Sstevel@tonic-gate extern void ulock_clear(lock_t *lp); 517c478bd9Sstevel@tonic-gate extern void lock_set_spl(lock_t *lp, int new_pil, ushort_t *old_pil); 527c478bd9Sstevel@tonic-gate extern void lock_clear_splx(lock_t *lp, int s); 537c478bd9Sstevel@tonic-gate 547c478bd9Sstevel@tonic-gate #endif /* _KERNEL */ 557c478bd9Sstevel@tonic-gate 567c478bd9Sstevel@tonic-gate #define LOCK_HELD_VALUE 0xff 577c478bd9Sstevel@tonic-gate #define LOCK_INIT_CLEAR(lp) (*(lp) = 0) 587c478bd9Sstevel@tonic-gate #define LOCK_INIT_HELD(lp) (*(lp) = LOCK_HELD_VALUE) 597c478bd9Sstevel@tonic-gate #define LOCK_HELD(lp) (*(volatile lock_t *)(lp) != 0) 607c478bd9Sstevel@tonic-gate 617c478bd9Sstevel@tonic-gate typedef lock_t disp_lock_t; /* dispatcher lock type */ 627c478bd9Sstevel@tonic-gate 637c478bd9Sstevel@tonic-gate /* 647c478bd9Sstevel@tonic-gate * SPIN_LOCK() macro indicates whether lock is implemented as a spin lock or 657c478bd9Sstevel@tonic-gate * an adaptive mutex, depending on what interrupt levels use it. 667c478bd9Sstevel@tonic-gate */ 677c478bd9Sstevel@tonic-gate #define SPIN_LOCK(pl) ((pl) > ipltospl(LOCK_LEVEL)) 687c478bd9Sstevel@tonic-gate 697c478bd9Sstevel@tonic-gate /* 707c478bd9Sstevel@tonic-gate * Macro to control loops which spin on a lock and then check state 717c478bd9Sstevel@tonic-gate * periodically. Its passed an integer, and returns a boolean value 727c478bd9Sstevel@tonic-gate * that if true indicates its a good time to get the scheduler lock and 737c478bd9Sstevel@tonic-gate * check the state of the current owner of the lock. 747c478bd9Sstevel@tonic-gate */ 757c478bd9Sstevel@tonic-gate #define LOCK_SAMPLE_INTERVAL(i) (((i) & 0xff) == 0) 767c478bd9Sstevel@tonic-gate 777c478bd9Sstevel@tonic-gate /* 787c478bd9Sstevel@tonic-gate * Externs for CLOCK_LOCK and clock resolution 797c478bd9Sstevel@tonic-gate */ 80*263f549eSPatrick Mooney extern volatile uint32_t hres_lock; 817c478bd9Sstevel@tonic-gate extern hrtime_t hrtime_base; 827c478bd9Sstevel@tonic-gate extern int clock_res; 837c478bd9Sstevel@tonic-gate 847c478bd9Sstevel@tonic-gate #endif /* _ASM */ 857c478bd9Sstevel@tonic-gate 867c478bd9Sstevel@tonic-gate /* 877c478bd9Sstevel@tonic-gate * The definitions of the symbolic interrupt levels: 887c478bd9Sstevel@tonic-gate * 897c478bd9Sstevel@tonic-gate * CLOCK_LEVEL => The level at which one must be to block the clock. 907c478bd9Sstevel@tonic-gate * 917c478bd9Sstevel@tonic-gate * LOCK_LEVEL => The highest level at which one may block (and thus the 927c478bd9Sstevel@tonic-gate * highest level at which one may acquire adaptive locks) 937c478bd9Sstevel@tonic-gate * Also the highest level at which one may be preempted. 947c478bd9Sstevel@tonic-gate * 957c478bd9Sstevel@tonic-gate * DISP_LEVEL => The level at which one must be to perform dispatcher 967c478bd9Sstevel@tonic-gate * operations. 977c478bd9Sstevel@tonic-gate * 987c478bd9Sstevel@tonic-gate * The constraints on the platform: 997c478bd9Sstevel@tonic-gate * 1007c478bd9Sstevel@tonic-gate * - CLOCK_LEVEL must be less than or equal to LOCK_LEVEL 1017c478bd9Sstevel@tonic-gate * - LOCK_LEVEL must be less than DISP_LEVEL 1027c478bd9Sstevel@tonic-gate * - DISP_LEVEL should be as close to LOCK_LEVEL as possible 1037c478bd9Sstevel@tonic-gate * 1047c478bd9Sstevel@tonic-gate * Note that LOCK_LEVEL and CLOCK_LEVEL have historically always been equal; 1057c478bd9Sstevel@tonic-gate * changing this relationship is probably possible but not advised. 1067c478bd9Sstevel@tonic-gate * 1077c478bd9Sstevel@tonic-gate */ 1087c478bd9Sstevel@tonic-gate 1097c478bd9Sstevel@tonic-gate #define PIL_MAX 15 1107c478bd9Sstevel@tonic-gate 1117c478bd9Sstevel@tonic-gate #define CLOCK_LEVEL 10 1127c478bd9Sstevel@tonic-gate #define LOCK_LEVEL 10 1137c478bd9Sstevel@tonic-gate #define DISP_LEVEL (LOCK_LEVEL + 1) 1147c478bd9Sstevel@tonic-gate 1157c478bd9Sstevel@tonic-gate #define HIGH_LEVELS (PIL_MAX - LOCK_LEVEL) 1167c478bd9Sstevel@tonic-gate 1177c478bd9Sstevel@tonic-gate /* 1187c478bd9Sstevel@tonic-gate * The following mask is for the cpu_intr_actv bits corresponding to 1197c478bd9Sstevel@tonic-gate * high-level PILs. It should equal: 1207c478bd9Sstevel@tonic-gate * ((((1 << PIL_MAX + 1) - 1) >> LOCK_LEVEL + 1) << LOCK_LEVEL + 1) 1217c478bd9Sstevel@tonic-gate */ 1227c478bd9Sstevel@tonic-gate #define CPU_INTR_ACTV_HIGH_LEVEL_MASK 0xF800 1237c478bd9Sstevel@tonic-gate 1247c478bd9Sstevel@tonic-gate /* 1257c478bd9Sstevel@tonic-gate * The semaphore code depends on being able to represent a lock plus 1267c478bd9Sstevel@tonic-gate * owner in a single 32-bit word. (Mutexes used to have a similar 1277c478bd9Sstevel@tonic-gate * dependency, but no longer.) Thus the owner must contain at most 1287c478bd9Sstevel@tonic-gate * 24 significant bits. At present only threads and semaphores 1297c478bd9Sstevel@tonic-gate * must be aware of this vile constraint. Different ISAs may handle this 1307c478bd9Sstevel@tonic-gate * differently depending on their capabilities (e.g. compare-and-swap) 1317c478bd9Sstevel@tonic-gate * and limitations (e.g. constraints on alignment and/or KERNELBASE). 1327c478bd9Sstevel@tonic-gate */ 1337c478bd9Sstevel@tonic-gate #define PTR24_LSB 5 /* lower bits all zero */ 1347c478bd9Sstevel@tonic-gate #define PTR24_MSB (PTR24_LSB + 24) /* upper bits all one */ 1357c478bd9Sstevel@tonic-gate #define PTR24_ALIGN 32 /* minimum alignment (1 << lsb) */ 1367c478bd9Sstevel@tonic-gate #define PTR24_BASE 0xe0000000 /* minimum ptr value (-1 >> (32-msb)) */ 1377c478bd9Sstevel@tonic-gate 1387c478bd9Sstevel@tonic-gate #ifdef __cplusplus 1397c478bd9Sstevel@tonic-gate } 1407c478bd9Sstevel@tonic-gate #endif 1417c478bd9Sstevel@tonic-gate 1427c478bd9Sstevel@tonic-gate #endif /* _SYS_MACHLOCK_H */ 143