├── COPYING ├── LICENSE ├── Makefile ├── README.md ├── asm.c ├── asm_goto.c ├── final_presentation.odp ├── include ├── asm.h ├── misc.h ├── nops.h ├── s-keys.h └── s-keys_x86.h ├── jl.c ├── jump_label.c ├── jump_label_x86.c ├── main.c ├── run_tests.sh └── test ├── basic.c ├── basic_f.c ├── basic_t.c ├── main.c └── multi.c /COPYING: -------------------------------------------------------------------------------- 1 | /* 2 | Original code copyright: 3 | 4 | Copyright (C) 2009-2012 Jason Baron 5 | Copyright (C) 2011-2012 Peter Zijlstra 6 | 7 | New code / modified code copyright: 8 | 9 | Copyright (C) 2014 Patrick McCormick 10 | 11 | Distributable under the terms of the GPLv2, see LICENSE for full text. 12 | */ 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | CPPFLAGS+=-Iinclude 2 | CFLAGS+=-O1 -g 3 | 4 | .PHONY: all 5 | all: main 6 | 7 | main: main.c jump_label.c jump_label_x86.c 8 | 9 | .PHONY: clean 10 | clean: 11 | rm -f main *.o *.s 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | s-keys 2 | === 3 | 4 | Port of linux kernel jump labels to userspace. Uses GCC goto asm extension and 5 | some other gcc/gas specific extensions (always inline, .pushsection, section attributes). 6 | 7 | Jump labels described in the kernel's jump_label.h: 8 | 9 | > Jump labels provide an interface to generate dynamic branches using 10 | > self-modifying code. Assuming toolchain and architecture support the result 11 | > of a "if (static_key_false(&key))" statement is a unconditional branch (which 12 | > defaults to false - and the true block is placed out of line). 13 | 14 | Original authors Jason Baron and Peter Zijlstra. 15 | 16 | See these two LWN articles for an introduction: 17 | 18 | > http://lwn.net/Articles/412072/ 19 | 20 | > http://lwn.net/Articles/436041/ 21 | 22 | Another good one on kernel elf special sections: 23 | 24 | > http://lwn.net/Articles/531148/ 25 | 26 | And the kernel documentation: 27 | 28 | > https://www.kernel.org/doc/Documentation/static-keys.txt 29 | 30 | Installing: 31 | ``` 32 | git clone https://github.com/computee/s-keys.git 33 | cd s-keys 34 | make 35 | ``` 36 | 37 | To use static keys, just include the header and initialize like this: 38 | 39 | ``` 40 | #include 41 | 42 | // must declare globally: 43 | struct static_key key = STATIC_KEY_INIT_TRUE; 44 | 45 | int main (int argc, char *argv[]) 46 | { 47 | jump_label_init(); 48 | 49 | if (static_key_true(&key)) 50 | printf("A\n"); 51 | else 52 | printf("B\n"); 53 | 54 | } 55 | ``` 56 | 57 | Todo 58 | === 59 | 60 | pull requests / patches welcome 61 | 62 | * Ideal nops: possibly re-implement this system 63 | * Cross compilation: need to test 64 | * RPM/deb skeleton files 65 | * header-only arrangement? 66 | * unit tests -like make targets and programs 67 | 68 | 69 | Contact 70 | === 71 | 72 | Patrick McCormick 73 | patm()pdx.edu 74 | -------------------------------------------------------------------------------- /asm.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #define STATIC_KEY_INIT_NOP P6_NOP5_ATOMIC 10 | 11 | struct jump_entry { 12 | uint64_t code; 13 | uint64_t target; 14 | uint64_t key; 15 | }; 16 | 17 | typedef struct { 18 | int counter; 19 | } atomic_t; 20 | 21 | 22 | struct static_key { 23 | atomic_t enabled; 24 | struct jump_entry *entries; 25 | }; 26 | 27 | struct static_key key; 28 | 29 | struct jump_entry __start___jump_table[] __attribute__ ((section ("__jump_table"))); 30 | struct jump_entry __stop___jump_table[] __attribute__ ((section ("__jump_table"))); 31 | 32 | static bool __always_inline f(struct static_key *k) 33 | { 34 | __asm__ volatile goto ("1:" 35 | ".byte " stringify(STATIC_KEY_INIT_NOP) "\n\t" 36 | ".pushsection __jump_table, \"aw\" \n\t" 37 | _ASM_ALIGN "\n\t" 38 | _ASM_PTR "1b, %l[l_yes], %c0 \n\t" 39 | ".popsection \n\t" 40 | : : "i" (k) : : l_yes); 41 | return false; 42 | l_yes: 43 | return true; 44 | } 45 | 46 | int main (int argc, char *argv[]) 47 | { 48 | // __asm__ volatile goto (_ASM_ALIGN "\n\t" _ASM_PTR "%l[l_yes]\n\t" : : : : l_yes ); 49 | __asm__ volatile (".pushsection __jump_table, \"aw\" \n\t" 50 | ".byte " stringify(STATIC_KEY_INIT_NOP) "\n\t" 51 | ".byte " stringify(STATIC_KEY_INIT_NOP) "\n\t" 52 | ".byte " stringify(STATIC_KEY_INIT_NOP) "\n\t" 53 | ".popsection \n\t" ); 54 | 55 | return 0; 56 | } 57 | 58 | 59 | -------------------------------------------------------------------------------- /asm_goto.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define cmpxchg( ptr, _old, _new ) { \ 4 | volatile uint32_t *__ptr = (volatile uint32_t *)(ptr); \ 5 | uint32_t __ret; \ 6 | __asm__ volatile( "lock; cmpxchgl %2,%1" \ 7 | : "=a" (__ret), "+m" (*__ptr) \ 8 | : "r" (_new), "0" (_old) \ 9 | : "memory"); \ 10 | __ret; \ 11 | } 12 | 13 | int main (int argc, char * argv[]) 14 | { 15 | 16 | struct Item { 17 | volatile struct Item* next; 18 | }; 19 | 20 | volatile struct Item *head; 21 | 22 | void addItem( struct Item *i ) { 23 | volatile struct Item *oldHead; 24 | 25 | again: 26 | oldHead = head; 27 | i->next = oldHead; 28 | cmpxchg( &head, oldHead, i); 29 | } 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /final_presentation.odp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/computee/s-keys/91cc487c51acfa8f2385edfdd242c49db578ab67/final_presentation.odp -------------------------------------------------------------------------------- /include/asm.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #define __ASM_FORM(x) " " #x " " 4 | #define __ASM_FORM_RAW(x) #x 5 | #define __ASM_FORM_COMMA(x) " " #x "," 6 | 7 | #ifndef __LP64__ 8 | # define __ASM_SEL(a,b) __ASM_FORM(a) 9 | # define __ASM_SEL_RAW(a,b) __ASM_FORM_RAW(a) 10 | #else 11 | # define __ASM_SEL(a,b) __ASM_FORM(b) 12 | # define __ASM_SEL_RAW(a,b) __ASM_FORM_RAW(b) 13 | #endif 14 | 15 | #define _ASM_PTR __ASM_SEL(.long, .quad) 16 | #define _ASM_ALIGN __ASM_SEL(.balign 4, .balign 8) 17 | 18 | -------------------------------------------------------------------------------- /include/misc.h: -------------------------------------------------------------------------------- 1 | #ifndef _MISC_H 2 | #define _MISC_H 3 | 4 | /* misc macros needed for compilation */ 5 | 6 | #define __stringify(x...) #x 7 | #define stringify(x...) __stringify(x) 8 | 9 | #define asm_volatile_goto(x) do { __asm__ goto (x); __asm__ (""); } while (0) 10 | 11 | #define likely(x) __builtin_expect(!!(x), 1) 12 | #define unlikely(x) __builtin_expect(!!(x), 0) 13 | 14 | /** 15 | * container_of - cast a member of a structure out to the containing structure 16 | * @ptr: the pointer to the member. 17 | * @type: the type of the container struct this is embedded in. 18 | * @member: the name of the member within the struct. 19 | * 20 | */ 21 | #define container_of(ptr, type, member) \ 22 | ((type *)((char *)(ptr) - offsetof(type, member))) 23 | 24 | 25 | #endif /* _MISC_H */ 26 | -------------------------------------------------------------------------------- /include/nops.h: -------------------------------------------------------------------------------- 1 | #ifndef _ASM_X86_NOPS_H 2 | #define _ASM_X86_NOPS_H 3 | 4 | /* 5 | * Define nops for use with alternative() and for tracing. 6 | * 7 | * *_NOP5_ATOMIC must be a single instruction. 8 | */ 9 | 10 | #define NOP_DS_PREFIX 0x3e 11 | 12 | /* generic versions from gas 13 | 1: nop 14 | the following instructions are NOT nops in 64-bit mode, 15 | for 64-bit mode use K8 or P6 nops instead 16 | 2: movl %esi,%esi 17 | 3: leal 0x00(%esi),%esi 18 | 4: leal 0x00(,%esi,1),%esi 19 | 6: leal 0x00000000(%esi),%esi 20 | 7: leal 0x00000000(,%esi,1),%esi 21 | */ 22 | #define GENERIC_NOP1 0x90 23 | #define GENERIC_NOP2 0x89,0xf6 24 | #define GENERIC_NOP3 0x8d,0x76,0x00 25 | #define GENERIC_NOP4 0x8d,0x74,0x26,0x00 26 | #define GENERIC_NOP5 GENERIC_NOP1,GENERIC_NOP4 27 | #define GENERIC_NOP6 0x8d,0xb6,0x00,0x00,0x00,0x00 28 | #define GENERIC_NOP7 0x8d,0xb4,0x26,0x00,0x00,0x00,0x00 29 | #define GENERIC_NOP8 GENERIC_NOP1,GENERIC_NOP7 30 | #define GENERIC_NOP5_ATOMIC NOP_DS_PREFIX,GENERIC_NOP4 31 | 32 | /* Opteron 64bit nops 33 | 1: nop 34 | 2: osp nop 35 | 3: osp osp nop 36 | 4: osp osp osp nop 37 | */ 38 | #define K8_NOP1 GENERIC_NOP1 39 | #define K8_NOP2 0x66,K8_NOP1 40 | #define K8_NOP3 0x66,K8_NOP2 41 | #define K8_NOP4 0x66,K8_NOP3 42 | #define K8_NOP5 K8_NOP3,K8_NOP2 43 | #define K8_NOP6 K8_NOP3,K8_NOP3 44 | #define K8_NOP7 K8_NOP4,K8_NOP3 45 | #define K8_NOP8 K8_NOP4,K8_NOP4 46 | #define K8_NOP5_ATOMIC 0x66,K8_NOP4 47 | 48 | /* K7 nops 49 | uses eax dependencies (arbitrary choice) 50 | 1: nop 51 | 2: movl %eax,%eax 52 | 3: leal (,%eax,1),%eax 53 | 4: leal 0x00(,%eax,1),%eax 54 | 6: leal 0x00000000(%eax),%eax 55 | 7: leal 0x00000000(,%eax,1),%eax 56 | */ 57 | #define K7_NOP1 GENERIC_NOP1 58 | #define K7_NOP2 0x8b,0xc0 59 | #define K7_NOP3 0x8d,0x04,0x20 60 | #define K7_NOP4 0x8d,0x44,0x20,0x00 61 | #define K7_NOP5 K7_NOP4,K7_NOP1 62 | #define K7_NOP6 0x8d,0x80,0,0,0,0 63 | #define K7_NOP7 0x8D,0x04,0x05,0,0,0,0 64 | #define K7_NOP8 K7_NOP7,K7_NOP1 65 | #define K7_NOP5_ATOMIC NOP_DS_PREFIX,K7_NOP4 66 | 67 | /* P6 nops 68 | uses eax dependencies (Intel-recommended choice) 69 | 1: nop 70 | 2: osp nop 71 | 3: nopl (%eax) 72 | 4: nopl 0x00(%eax) 73 | 5: nopl 0x00(%eax,%eax,1) 74 | 6: osp nopl 0x00(%eax,%eax,1) 75 | 7: nopl 0x00000000(%eax) 76 | 8: nopl 0x00000000(%eax,%eax,1) 77 | Note: All the above are assumed to be a single instruction. 78 | There is kernel code that depends on this. 79 | */ 80 | #define P6_NOP1 GENERIC_NOP1 81 | #define P6_NOP2 0x66,0x90 82 | #define P6_NOP3 0x0f,0x1f,0x00 83 | #define P6_NOP4 0x0f,0x1f,0x40,0 84 | #define P6_NOP5 0x0f,0x1f,0x44,0x00,0 85 | #define P6_NOP6 0x66,0x0f,0x1f,0x44,0x00,0 86 | #define P6_NOP7 0x0f,0x1f,0x80,0,0,0,0 87 | #define P6_NOP8 0x0f,0x1f,0x84,0x00,0,0,0,0 88 | #define P6_NOP5_ATOMIC P6_NOP5 89 | 90 | #define _ASM_MK_NOP(x) ".byte " stringify(x) "\n" 91 | 92 | #if defined(CONFIG_MK7) 93 | #define ASM_NOP1 _ASM_MK_NOP(K7_NOP1) 94 | #define ASM_NOP2 _ASM_MK_NOP(K7_NOP2) 95 | #define ASM_NOP3 _ASM_MK_NOP(K7_NOP3) 96 | #define ASM_NOP4 _ASM_MK_NOP(K7_NOP4) 97 | #define ASM_NOP5 _ASM_MK_NOP(K7_NOP5) 98 | #define ASM_NOP6 _ASM_MK_NOP(K7_NOP6) 99 | #define ASM_NOP7 _ASM_MK_NOP(K7_NOP7) 100 | #define ASM_NOP8 _ASM_MK_NOP(K7_NOP8) 101 | #define ASM_NOP5_ATOMIC _ASM_MK_NOP(K7_NOP5_ATOMIC) 102 | #elif defined(CONFIG_X86_P6_NOP) 103 | #define ASM_NOP1 _ASM_MK_NOP(P6_NOP1) 104 | #define ASM_NOP2 _ASM_MK_NOP(P6_NOP2) 105 | #define ASM_NOP3 _ASM_MK_NOP(P6_NOP3) 106 | #define ASM_NOP4 _ASM_MK_NOP(P6_NOP4) 107 | #define ASM_NOP5 _ASM_MK_NOP(P6_NOP5) 108 | #define ASM_NOP6 _ASM_MK_NOP(P6_NOP6) 109 | #define ASM_NOP7 _ASM_MK_NOP(P6_NOP7) 110 | #define ASM_NOP8 _ASM_MK_NOP(P6_NOP8) 111 | #define ASM_NOP5_ATOMIC _ASM_MK_NOP(P6_NOP5_ATOMIC) 112 | #elif defined(CONFIG_X86_64) 113 | #define ASM_NOP1 _ASM_MK_NOP(K8_NOP1) 114 | #define ASM_NOP2 _ASM_MK_NOP(K8_NOP2) 115 | #define ASM_NOP3 _ASM_MK_NOP(K8_NOP3) 116 | #define ASM_NOP4 _ASM_MK_NOP(K8_NOP4) 117 | #define ASM_NOP5 _ASM_MK_NOP(K8_NOP5) 118 | #define ASM_NOP6 _ASM_MK_NOP(K8_NOP6) 119 | #define ASM_NOP7 _ASM_MK_NOP(K8_NOP7) 120 | #define ASM_NOP8 _ASM_MK_NOP(K8_NOP8) 121 | #define ASM_NOP5_ATOMIC _ASM_MK_NOP(K8_NOP5_ATOMIC) 122 | #else 123 | #define ASM_NOP1 _ASM_MK_NOP(GENERIC_NOP1) 124 | #define ASM_NOP2 _ASM_MK_NOP(GENERIC_NOP2) 125 | #define ASM_NOP3 _ASM_MK_NOP(GENERIC_NOP3) 126 | #define ASM_NOP4 _ASM_MK_NOP(GENERIC_NOP4) 127 | #define ASM_NOP5 _ASM_MK_NOP(GENERIC_NOP5) 128 | #define ASM_NOP6 _ASM_MK_NOP(GENERIC_NOP6) 129 | #define ASM_NOP7 _ASM_MK_NOP(GENERIC_NOP7) 130 | #define ASM_NOP8 _ASM_MK_NOP(GENERIC_NOP8) 131 | #define ASM_NOP5_ATOMIC _ASM_MK_NOP(GENERIC_NOP5_ATOMIC) 132 | #endif 133 | 134 | #define ASM_NOP_MAX 8 135 | #define NOP_ATOMIC5 (ASM_NOP_MAX+1) /* Entry for the 5-byte atomic NOP */ 136 | 137 | // hardcode these for x86_64 for now: 138 | const unsigned char * const *ideal_nops; 139 | void arch_init_ideal_nops(void); 140 | 141 | #endif /* _ASM_X86_NOPS_H */ 142 | -------------------------------------------------------------------------------- /include/s-keys.h: -------------------------------------------------------------------------------- 1 | /* 2 | Original code copyright: 3 | 4 | Copyright (C) 2009-2012 Jason Baron 5 | Copyright (C) 2011-2012 Peter Zijlstra 6 | 7 | New code / modified code copyright: 8 | 9 | Copyright (C) 2014 Patrick McCormick 10 | 11 | Distributable under the terms of the GPLv2, see LICENSE for full text. 12 | */ 13 | #pragma once 14 | 15 | #include 16 | #include 17 | 18 | /* check for gcc >= 4.8.2, on anything else 19 | * fall back to normal branching code */ 20 | #ifdef __GNUC__ 21 | 22 | #if __GNUC__ > 4 || \ 23 | (__GNUC__ == 4 && (__GNUC_MINOR__ > 8 || \ 24 | (__GNUC_MINOR__ == 8 && \ 25 | __GNUC_PATCHLEVEL__ >= 2))) 26 | 27 | #define ENABLE_SK 28 | 29 | #else 30 | #warn "Disabling s-keys, non gcc or gcc < 4.8.2" 31 | #endif 32 | 33 | #endif 34 | 35 | /* architecture plus size detection */ 36 | #ifdef ENABLE_SK 37 | 38 | #if defined(__x86_64__) || defined(__i386__) 39 | #include 40 | #elif defined(__arm__) 41 | #include 42 | #else 43 | #error "Unknown architecture" 44 | #endif 45 | 46 | #endif 47 | 48 | // atomic defs should go in arch-dependent header: 49 | typedef struct { 50 | int counter; 51 | } atomic_t; 52 | 53 | #define ATOMIC_INIT(i) { (i) } 54 | 55 | #define WARN(x,...) if (x) fprintf(stderr, __VA_ARGS__); 56 | 57 | static bool static_key_initialized = false; 58 | 59 | #define STATIC_KEY_CHECK_USE() WARN(!static_key_initialized, \ 60 | "%s used before call to jump_label_init", \ 61 | __func__) 62 | 63 | 64 | struct static_key { 65 | atomic_t enabled; 66 | /* Set lsb bit to 1 if branch is default true, 0 ot */ 67 | struct jump_entry *entries; 68 | }; 69 | 70 | enum jump_label_type { 71 | JUMP_LABEL_DISABLE = 0, 72 | JUMP_LABEL_ENABLE, 73 | }; 74 | 75 | static inline atomic_t static_key_count(struct static_key *key) 76 | { 77 | //return atomic_read(&key->enabled); 78 | return key->enabled; 79 | } 80 | 81 | #ifdef ENABLE_SK 82 | 83 | #define JUMP_LABEL_TYPE_FALSE_BRANCH 0UL 84 | #define JUMP_LABEL_TYPE_TRUE_BRANCH 1UL 85 | #define JUMP_LABEL_TYPE_MASK 1UL 86 | 87 | static 88 | inline struct jump_entry *jump_label_get_entries(struct static_key *key) 89 | { 90 | return (struct jump_entry *)((unsigned long)key->entries 91 | & ~JUMP_LABEL_TYPE_MASK); 92 | } 93 | 94 | static inline bool jump_label_get_branch_default(struct static_key *key) 95 | { 96 | if (((unsigned long)key->entries & JUMP_LABEL_TYPE_MASK) == 97 | JUMP_LABEL_TYPE_TRUE_BRANCH) 98 | return true; 99 | return false; 100 | } 101 | 102 | static __always_inline bool static_key_false(struct static_key *key) 103 | { 104 | return arch_static_branch(key); 105 | } 106 | 107 | static __always_inline bool static_key_true(struct static_key *key) 108 | { 109 | return !static_key_false(key); 110 | } 111 | 112 | // platform specific functions/defines needed: 113 | 114 | // currently in the x86 header/impl: 115 | // bool arch_static_branch(struct static_key *key) 116 | // struct jump_entry { 117 | 118 | extern struct jump_entry __start___jump_table[] __attribute__ ((section ("__jump_table"))); 119 | extern struct jump_entry __stop___jump_table[] __attribute__ ((section ("__jump_table"))); 120 | 121 | void arch_jump_label_transform(struct jump_entry *entry, enum jump_label_type type); 122 | 123 | // in generic jl impl: 124 | void jump_label_init(void); 125 | void jump_label_lock(void); 126 | void jump_label_unlock(void); 127 | 128 | int jump_label_text_reserved(void *start, void *end); 129 | 130 | void static_key_slow_inc(struct static_key *key); 131 | void static_key_slow_dec(struct static_key *key); 132 | 133 | // both !? 134 | void arch_jump_label_transform_static(struct jump_entry *entry, enum jump_label_type type); 135 | 136 | 137 | //extern void jump_label_apply_nops(struct module *mod); 138 | 139 | #define STATIC_KEY_INIT_TRUE \ 140 | { .enabled = ATOMIC_INIT(1), \ 141 | .entries = (void *)JUMP_LABEL_TYPE_TRUE_BRANCH } 142 | #define STATIC_KEY_INIT_FALSE \ 143 | { .enabled = ATOMIC_INIT(0), \ 144 | .entries = (void *)JUMP_LABEL_TYPE_FALSE_BRANCH } 145 | 146 | #else /* !ENABLE_SK */ 147 | 148 | static __always_inline void jump_label_init(void) 149 | { 150 | static_key_initialized = true; 151 | } 152 | 153 | static __always_inline bool static_key_false(struct static_key *key) 154 | { 155 | if (unlikely(static_key_count(key) > 0)) 156 | return true; 157 | return false; 158 | } 159 | 160 | static __always_inline bool static_key_true(struct static_key *key) 161 | { 162 | if (likely(static_key_count(key) > 0)) 163 | return true; 164 | return false; 165 | } 166 | 167 | static inline void static_key_slow_inc(struct static_key *key) 168 | { 169 | STATIC_KEY_CHECK_USE(); 170 | atomic_inc(&key->enabled); 171 | } 172 | 173 | static inline void static_key_slow_dec(struct static_key *key) 174 | { 175 | STATIC_KEY_CHECK_USE(); 176 | atomic_dec(&key->enabled); 177 | } 178 | 179 | static inline int jump_label_text_reserved(void *start, void *end) 180 | { 181 | return 0; 182 | } 183 | 184 | #define STATIC_KEY_INIT_TRUE ((struct static_key) \ 185 | { .enabled = ATOMIC_INIT(1) }) 186 | #define STATIC_KEY_INIT_FALSE ((struct static_key) \ 187 | { .enabled = ATOMIC_INIT(0) }) 188 | 189 | #endif /* HAVE_JUMP_LABEL */ 190 | 191 | #define STATIC_KEY_INIT STATIC_KEY_INIT_FALSE 192 | 193 | static inline bool static_key_enabled(struct static_key *key) 194 | { 195 | return static_key_count(key).counter > 0; 196 | } 197 | 198 | -------------------------------------------------------------------------------- /include/s-keys_x86.h: -------------------------------------------------------------------------------- 1 | /* 2 | Original code copyright: 3 | 4 | Copyright (C) 2009-2012 Jason Baron 5 | Copyright (C) 2011-2012 Peter Zijlstra 6 | 7 | New code / modified code copyright: 8 | 9 | Copyright (C) 2014 Patrick McCormick 10 | 11 | Distributable under the terms of the GPLv2, see LICENSE for full text. 12 | */ 13 | #pragma once 14 | 15 | #include 16 | #include 17 | 18 | #include 19 | #include 20 | #include 21 | 22 | #define JUMP_LABEL_NOP_SIZE 5 23 | 24 | #ifdef __LP64__ 25 | # define STATIC_KEY_INIT_NOP P6_NOP5_ATOMIC 26 | #else 27 | # define STATIC_KEY_INIT_NOP GENERIC_NOP5_ATOMIC 28 | #endif 29 | 30 | #ifdef __LP64__ 31 | typedef uint64_t jump_label_t; 32 | #else 33 | typedef uint32_t jump_label_t; 34 | #endif 35 | 36 | struct static_key; 37 | 38 | struct jump_entry { 39 | jump_label_t code; 40 | jump_label_t target; 41 | jump_label_t key; 42 | }; 43 | 44 | static bool __always_inline arch_static_branch(struct static_key *key) 45 | { 46 | asm_volatile_goto ("1:" 47 | ".byte " stringify(STATIC_KEY_INIT_NOP) "\n\t" 48 | ".pushsection __jump_table, \"aw\" \n\t" 49 | _ASM_ALIGN "\n\t" 50 | _ASM_PTR "1b, %l[l_yes], %c0 \n\t" 51 | ".popsection \n\t" 52 | : : "i" (key) : : l_yes); 53 | 54 | return false; 55 | l_yes: 56 | return true; 57 | } 58 | 59 | -------------------------------------------------------------------------------- /jl.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #define STATIC_KEY_INIT_NOP P6_NOP5_ATOMIC 10 | 11 | struct jump_entry { 12 | uint64_t code; 13 | uint64_t target; 14 | uint64_t key; 15 | }; 16 | 17 | typedef struct { 18 | int counter; 19 | } atomic_t; 20 | 21 | 22 | struct static_key { 23 | atomic_t enabled; 24 | struct jump_entry *entries; 25 | }; 26 | 27 | struct static_key key; 28 | 29 | struct jump_entry __start___jump_table[] __attribute__ ((section ("__jump_table"))); 30 | struct jump_entry __stop___jump_table[] __attribute__ ((section ("__jump_table"))); 31 | 32 | static bool __always_inline f(struct static_key *k) 33 | { 34 | __asm__ volatile goto ("1:" 35 | ".byte " stringify(STATIC_KEY_INIT_NOP) "\n\t" 36 | ".pushsection __jump_table, \"aw\" \n\t" 37 | _ASM_ALIGN "\n\t" 38 | _ASM_PTR "1b, %l[l_yes], %c0 \n\t" 39 | ".popsection \n\t" 40 | : : "i" (k) : : l_yes); 41 | return false; 42 | l_yes: 43 | return true; 44 | } 45 | 46 | int main (int argc, char *argv[]) 47 | { 48 | printf("before\n"); 49 | 50 | f(&key); 51 | 52 | printf("after\n"); 53 | return 0; 54 | } 55 | 56 | 57 | -------------------------------------------------------------------------------- /jump_label.c: -------------------------------------------------------------------------------- 1 | /* 2 | Original code copyright: 3 | 4 | Copyright (C) 2009-2012 Jason Baron 5 | Copyright (C) 2011-2012 Peter Zijlstra 6 | 7 | New code / modified code copyright: 8 | 9 | Copyright (C) 2014 Patrick McCormick 10 | 11 | Distributable under the terms of the GPLv2, see LICENSE for full text. 12 | */ 13 | #include 14 | #include 15 | #include 16 | 17 | #include 18 | #include 19 | 20 | 21 | // need to translate to c11 atomic ops or not? stub for now 22 | 23 | static inline int atomic_read(const atomic_t *v) 24 | { 25 | return (*(volatile int *)&(v)->counter); 26 | } 27 | 28 | int atomic_inc(atomic_t *p) 29 | { 30 | return ++(p->counter); 31 | } 32 | 33 | /** 34 | * Increment by 1 if p is non-zero, otherwise do nothing. 35 | * Return non-zero if p was incremented. 36 | **/ 37 | int atomic_inc_not_zero(atomic_t *p) 38 | { 39 | if (p->counter) { 40 | p->counter += 1; 41 | return p->counter; 42 | } else 43 | return 0; 44 | } 45 | 46 | /** 47 | * Decrement, no mutex for now 48 | */ 49 | int atomic_dec_and_mutex_lock(atomic_t *p) 50 | { 51 | return --(p->counter); 52 | } 53 | 54 | // stub out kernel_text_address() for now 55 | int kernel_text_address(unsigned long addr) 56 | { 57 | return 1; 58 | } 59 | 60 | static int jump_label_cmp(const void *a, const void *b) 61 | { 62 | const struct jump_entry *jea = a; 63 | const struct jump_entry *jeb = b; 64 | 65 | if (jea->key < jeb->key) 66 | return -1; 67 | 68 | if (jea->key > jeb->key) 69 | return 1; 70 | 71 | return 0; 72 | } 73 | 74 | static void 75 | jump_label_sort_entries(struct jump_entry *start, struct jump_entry *stop) 76 | { 77 | unsigned long size; 78 | 79 | size = (((unsigned long)stop - (unsigned long)start) 80 | / sizeof(struct jump_entry)); 81 | qsort(start, size, sizeof(struct jump_entry), jump_label_cmp); 82 | } 83 | 84 | static void jump_label_update(struct static_key *key, int enable); 85 | 86 | /* export */ 87 | void static_key_slow_inc(struct static_key *key) 88 | { 89 | STATIC_KEY_CHECK_USE(); 90 | if (atomic_inc_not_zero(&key->enabled)) 91 | return; 92 | 93 | // jl lock() 94 | if (atomic_read(&key->enabled) == 0) { 95 | if (!jump_label_get_branch_default(key)) 96 | jump_label_update(key, JUMP_LABEL_ENABLE); 97 | else 98 | jump_label_update(key, JUMP_LABEL_DISABLE); 99 | } 100 | atomic_inc(&key->enabled); 101 | // jl unlock() 102 | } 103 | 104 | static void __static_key_slow_dec(struct static_key *key) 105 | { 106 | // lock and aquire mutex 107 | if (!atomic_dec_and_mutex_lock(&key->enabled)) { 108 | WARN(atomic_read(&key->enabled) < 0, 109 | "jump label: negative count!\n"); 110 | return; 111 | } 112 | 113 | if (!jump_label_get_branch_default(key)) 114 | jump_label_update(key, JUMP_LABEL_DISABLE); 115 | else 116 | jump_label_update(key, JUMP_LABEL_ENABLE); 117 | // unlock mutex() 118 | } 119 | 120 | /* export */ 121 | void static_key_slow_dec(struct static_key *key) 122 | { 123 | STATIC_KEY_CHECK_USE(); 124 | __static_key_slow_dec(key); 125 | } 126 | 127 | static int addr_conflict(struct jump_entry *entry, void *start, void *end) 128 | { 129 | if (entry->code <= (unsigned long)end && 130 | entry->code + JUMP_LABEL_NOP_SIZE > (unsigned long)start) 131 | return 1; 132 | 133 | return 0; 134 | } 135 | 136 | static int __jump_label_text_reserved(struct jump_entry *iter_start, 137 | struct jump_entry *iter_stop, void *start, void *end) 138 | { 139 | struct jump_entry *iter; 140 | 141 | iter = iter_start; 142 | while (iter < iter_stop) { 143 | if (addr_conflict(iter, start, end)) 144 | return 1; 145 | iter++; 146 | } 147 | 148 | return 0; 149 | } 150 | 151 | /* 152 | * Update code which is definitely not currently executing. 153 | * Architectures which need heavyweight synchronization to modify 154 | * running code can override this to make the non-live update case 155 | * cheaper. 156 | */ 157 | void arch_jump_label_transform_static(struct jump_entry *entry, 158 | enum jump_label_type type) 159 | { 160 | arch_jump_label_transform(entry, type); 161 | } 162 | 163 | static void __jump_label_update(struct static_key *key, 164 | struct jump_entry *entry, 165 | struct jump_entry *stop, int enable) 166 | { 167 | for (; (entry < stop) && 168 | (entry->key == (jump_label_t)(unsigned long)key); 169 | entry++) { 170 | /* 171 | * entry->code set to 0 invalidates module init text sections 172 | * kernel_text_address() verifies we are not in core kernel 173 | * init code, see jump_label_invalidate_module_init(). 174 | */ 175 | if (entry->code && kernel_text_address(entry->code)) 176 | arch_jump_label_transform(entry, enable); 177 | } 178 | } 179 | 180 | static enum jump_label_type jump_label_type(struct static_key *key) 181 | { 182 | bool true_branch = jump_label_get_branch_default(key); 183 | bool state = static_key_enabled(key); 184 | 185 | if ((!true_branch && state) || (true_branch && !state)) 186 | return JUMP_LABEL_ENABLE; 187 | 188 | return JUMP_LABEL_DISABLE; 189 | } 190 | 191 | 192 | void jump_label_init(void) 193 | { 194 | struct jump_entry *iter_start = __start___jump_table; 195 | struct jump_entry *iter_stop = __stop___jump_table; 196 | struct static_key *key = NULL; 197 | struct jump_entry *iter; 198 | 199 | // jl lock() 200 | jump_label_sort_entries(iter_start, iter_stop); 201 | 202 | for (iter = iter_start; iter < iter_stop; iter++) { 203 | struct static_key *iterk; 204 | 205 | iterk = (struct static_key *)(unsigned long)iter->key; 206 | arch_jump_label_transform_static(iter, jump_label_type(iterk)); 207 | if (iterk == key) 208 | continue; 209 | 210 | key = iterk; 211 | /* 212 | * Set key->entries to iter, but preserve JUMP_LABEL_TRUE_BRANCH. 213 | */ 214 | *((unsigned long *)&key->entries) += (unsigned long)iter; 215 | } 216 | static_key_initialized = true; 217 | // jl unlock 218 | } 219 | 220 | /*** 221 | * jump_label_text_reserved - check if addr range is reserved 222 | * @start: start text addr 223 | * @end: end text addr 224 | * 225 | * checks if the text addr located between @start and @end 226 | * overlaps with any of the jump label patch addresses. Code 227 | * that wants to modify kernel text should first verify that 228 | * it does not overlap with any of the jump label addresses. 229 | * Caller must hold jump_label_mutex. 230 | * 231 | * returns 1 if there is an overlap, 0 otherwise 232 | */ 233 | int jump_label_text_reserved(void *start, void *end) 234 | { 235 | int ret = __jump_label_text_reserved(__start___jump_table, 236 | __stop___jump_table, start, end); 237 | 238 | if (ret) 239 | return ret; 240 | 241 | return ret; 242 | } 243 | 244 | static void jump_label_update(struct static_key *key, int enable) 245 | { 246 | struct jump_entry *stop = __stop___jump_table; 247 | struct jump_entry *entry = jump_label_get_entries(key); 248 | 249 | /* if there are no users, entry can be NULL */ 250 | if (entry) 251 | __jump_label_update(key, entry, stop, enable); 252 | } 253 | 254 | -------------------------------------------------------------------------------- /jump_label_x86.c: -------------------------------------------------------------------------------- 1 | /* 2 | Original code copyright: 3 | 4 | Copyright (C) 2009-2012 Jason Baron 5 | Copyright (C) 2011-2012 Peter Zijlstra 6 | 7 | New code / modified code copyright: 8 | 9 | Copyright (C) 2014 Patrick McCormick 10 | 11 | Distributable under the terms of the GPLv2, see LICENSE for full text. 12 | */ 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | #include 20 | #include 21 | 22 | #include 23 | #include 24 | 25 | /* 26 | * TODO init pagesize once per program 27 | */ 28 | static inline void *pageof(const void* p) 29 | { 30 | size_t pagesize = sysconf(_SC_PAGESIZE); 31 | return (void*) ((uintptr_t)p & ~(pagesize - 1)); 32 | } 33 | 34 | /** 35 | * Unprotect page where entry->code lies 36 | */ 37 | void *sk_mprotect(void *addr, size_t len) 38 | { 39 | if (mprotect(pageof(addr), len, PROT_READ | PROT_WRITE | PROT_EXEC)) { 40 | perror("mprotect"); 41 | return NULL; 42 | } 43 | 44 | return addr; 45 | } 46 | 47 | union jump_code_union { 48 | char code[JUMP_LABEL_NOP_SIZE]; 49 | struct { 50 | char jump; 51 | int offset; 52 | } __attribute__((packed)); 53 | }; 54 | 55 | static void bug_at(unsigned char *ip, int line) 56 | { 57 | /* 58 | * The location is not an op that we were expecting. 59 | * Something went wrong. Crash the box, as something could be 60 | * corrupting the kernel. 61 | */ 62 | 63 | printf("Unexpected op at %p (%02x %02x %02x %02x %02x) %s:%d\n", 64 | ip, ip[0], ip[1], ip[2], ip[3], ip[4], __FILE__, line); 65 | //assert(false); 66 | } 67 | 68 | static void __jump_label_transform(struct jump_entry *entry, 69 | enum jump_label_type type, 70 | int init) 71 | { 72 | const unsigned char default_nop[] = { STATIC_KEY_INIT_NOP }; 73 | const unsigned char ideal_nop[] = { STATIC_KEY_INIT_NOP }; 74 | union jump_code_union code = { .jump = 0xe9, 75 | .offset = entry->target - (entry->code + JUMP_LABEL_NOP_SIZE) 76 | }; 77 | 78 | if (type == JUMP_LABEL_ENABLE) { 79 | // printf("ENABLE\n"); 80 | if (init) { 81 | /* 82 | * Jump label is enabled for the first time. 83 | * So we expect a default_nop... 84 | */ 85 | if (unlikely(memcmp((void *)entry->code, default_nop, 5) 86 | != 0)) 87 | bug_at((void *)entry->code, __LINE__); 88 | } else { 89 | /* 90 | * ...otherwise expect an ideal_nop. Otherwise 91 | * something went horribly wrong. 92 | */ 93 | if (unlikely(memcmp((void *)entry->code, ideal_nop, 5) != 0)) 94 | bug_at((void *)entry->code, __LINE__); 95 | } 96 | 97 | } else { 98 | // printf("DISABLE\n"); 99 | /* 100 | * We are disabling this jump label. If it is not what 101 | * we think it is, then something must have gone wrong. 102 | * If this is the first initialization call, then we 103 | * are converting the default nop to the ideal nop. 104 | */ 105 | if (init) { 106 | if (unlikely(memcmp((void *)entry->code, default_nop, 5) != 0)) 107 | bug_at((void *)entry->code, __LINE__); 108 | } else { 109 | if (unlikely(memcmp((void *)entry->code, &code, 5) != 0)) 110 | bug_at((void *)entry->code, __LINE__); 111 | } 112 | memcpy(&code, ideal_nop, JUMP_LABEL_NOP_SIZE); 113 | } 114 | 115 | /* 116 | * Make text_poke_bp() a default fallback poker. 117 | * 118 | * At the time the change is being done, just ignore whether we 119 | * are doing nop -> jump or jump -> nop transition, and assume 120 | * always nop being the 'currently valid' instruction 121 | * 122 | */ 123 | 124 | if (sk_mprotect((void *)entry->code, JUMP_LABEL_NOP_SIZE) == NULL) 125 | bug_at("sk_mprotect", __LINE__); 126 | 127 | // and now do the actual copy: 128 | memcpy((void*) entry->code, &code, JUMP_LABEL_NOP_SIZE); 129 | } 130 | 131 | void arch_jump_label_transform(struct jump_entry *entry, 132 | enum jump_label_type type) 133 | { 134 | __jump_label_transform(entry, type, 0); 135 | } 136 | 137 | -------------------------------------------------------------------------------- /main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | struct static_key key = STATIC_KEY_INIT_FALSE; 6 | 7 | int main (int argc, char *argv[]) 8 | { 9 | jump_label_init(); 10 | 11 | 12 | 13 | if (static_key_false(&key)) 14 | printf("...\n"); 15 | else 16 | printf("%d\n", argc); 17 | 18 | static_key_slow_inc(&key); 19 | 20 | if (static_key_false(&key)) 21 | printf("...\n"); 22 | else 23 | printf("%d\n", argc); 24 | 25 | static_key_slow_dec(&key); 26 | static_key_slow_dec(&key); 27 | 28 | if (static_key_false(&key)) 29 | printf("...\n"); 30 | else 31 | printf("%d\n", argc); 32 | 33 | return 0; 34 | } 35 | 36 | 37 | -------------------------------------------------------------------------------- /run_tests.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | for i in test/*.c; do 4 | BNAME=$(basename $i .c) 5 | cc -Iinclude -O1 ./jump*.c $i -o ${BNAME} &&\ 6 | ./${BNAME} 7 | done 8 | -------------------------------------------------------------------------------- /test/basic.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | struct static_key key = STATIC_KEY_INIT_FALSE; 6 | 7 | int main (int argc, char *argv[]) 8 | { 9 | jump_label_init(); 10 | 11 | 12 | 13 | if (static_key_false(&key)) 14 | printf("...\n"); 15 | else 16 | printf("%d\n", argc); 17 | 18 | static_key_slow_inc(&key); 19 | 20 | if (static_key_false(&key)) 21 | printf("...\n"); 22 | else 23 | printf("%d\n", argc); 24 | 25 | static_key_slow_dec(&key); 26 | static_key_slow_dec(&key); 27 | 28 | if (static_key_false(&key)) 29 | printf("...\n"); 30 | else 31 | printf("%d\n", argc); 32 | 33 | return 0; 34 | } 35 | 36 | 37 | -------------------------------------------------------------------------------- /test/basic_f.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | struct static_key key = STATIC_KEY_INIT_FALSE; 6 | 7 | int main (int argc, char *argv[]) 8 | { 9 | jump_label_init(); 10 | 11 | if (static_key_false(&key)) { 12 | printf("%s\t\tFAIL\n", __FILE__); 13 | return 1; 14 | } 15 | else { 16 | printf("%s\t\tOK\n", __FILE__); 17 | return 0; 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /test/basic_t.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | struct static_key key = STATIC_KEY_INIT_TRUE; 6 | 7 | int main (int argc, char *argv[]) 8 | { 9 | jump_label_init(); 10 | 11 | if (static_key_true(&key)) { 12 | printf("%s\t\tOK\n", __FILE__); 13 | return 0; 14 | } 15 | else { 16 | printf("%s\t\tFAIL\n", __FILE__); 17 | return 1; 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /test/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | struct static_key key = STATIC_KEY_INIT_FALSE; 6 | 7 | int main (int argc, char *argv[]) 8 | { 9 | jump_label_init(); 10 | 11 | 12 | 13 | if (static_key_false(&key)) 14 | printf("...\n"); 15 | else 16 | printf("%d\n", argc); 17 | 18 | static_key_slow_inc(&key); 19 | 20 | if (static_key_false(&key)) 21 | printf("...\n"); 22 | else 23 | printf("%d\n", argc); 24 | 25 | static_key_slow_dec(&key); 26 | static_key_slow_dec(&key); 27 | 28 | if (static_key_false(&key)) 29 | printf("...\n"); 30 | else 31 | printf("%d\n", argc); 32 | 33 | return 0; 34 | } 35 | 36 | 37 | -------------------------------------------------------------------------------- /test/multi.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | struct static_key key = STATIC_KEY_INIT_FALSE; 6 | 7 | int main (int argc, char *argv[]) 8 | { 9 | int ret = 0; 10 | jump_label_init(); 11 | 12 | if (static_key_false(&key)) 13 | ++ret; 14 | else 15 | ret += 0; 16 | 17 | static_key_slow_inc(&key); 18 | 19 | if (static_key_false(&key)) 20 | ret += 0; 21 | else 22 | ++ret; 23 | 24 | if (ret) 25 | printf("%s\t\tFAIL\n", __FILE__); 26 | else 27 | printf("%s\t\tOK\n", __FILE__); 28 | 29 | return 0; 30 | } 31 | 32 | 33 | --------------------------------------------------------------------------------