├── src ├── LocalMakefile ├── config.h.in ├── util.h ├── Makefile.in ├── symbol.h ├── timestats.h ├── l1i.h ├── l1.h ├── vlist.h ├── l3.h ├── util.c ├── fr.h ├── timestats.c ├── symbol.c ├── pda.h ├── vlist.c ├── low.h ├── pda.c ├── l1i.c ├── symbol_mach.c ├── l1.c ├── ff.h ├── ff.c ├── symbol_bfd.c ├── fr.c └── l3.c ├── VERSION ├── demo ├── rungnupg.sh ├── debuginfo.sh ├── disas.sh ├── L1-rattle.c ├── Makefile.in ├── FR-1-file-access.c ├── FR-function-call-nodelay.c ├── FR-function-call.c ├── FR-2-file-access.c ├── FR-flush.c ├── L3-scan.c ├── L3-capture.c ├── L3-capturecount.c ├── FF-gnupg-1.4.13.c ├── L1-capture.c ├── FR-gnupg-1.4.13.c ├── FR-threshold.c └── FR-trace.c ├── Makefile.in ├── .gitignore ├── README.md ├── configure.ac └── COPYING /src/LocalMakefile: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /VERSION: -------------------------------------------------------------------------------- 1 | 0.02 Aye Aye Cap'n 2 | -------------------------------------------------------------------------------- /demo/rungnupg.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | while true; do ./gpg-1.4.13 --yes -b echo.c; done 4 | -------------------------------------------------------------------------------- /demo/debuginfo.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | 4 | usage() { 5 | echo "Usage $1 " >&2 6 | exit 1 7 | } 8 | 9 | case $# in 10 | 2);; 11 | *) usage $0;; 12 | esac 13 | 14 | gdb -batch -ex "file $1" -ex "info line $2" 15 | -------------------------------------------------------------------------------- /demo/disas.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | 4 | usage() { 5 | echo "Usage $1 " >&2 6 | exit 1 7 | } 8 | 9 | case $# in 10 | 2);; 11 | *) usage $0;; 12 | esac 13 | 14 | gdb -batch -ex "file $1" -ex "disassemble $2" 15 | -------------------------------------------------------------------------------- /src/config.h.in: -------------------------------------------------------------------------------- 1 | #undef PACKAGE_VERSION 2 | 3 | #undef HAVE_DWARF 4 | #undef HAVE_LIBDWARF_H 5 | #undef HAVE_LIBDWARF_LIBDWARF_H 6 | #undef HAVE_BFD_H 7 | #undef HAVE_SYS_PRCTL_H 8 | #undef HAVE_SCHED_H 9 | #undef HAVE_MMAP64 10 | 11 | #undef HAVE_SCHED_SETAFFINITY 12 | 13 | #undef HAVE_SYMBOLS 14 | -------------------------------------------------------------------------------- /demo/L1-rattle.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | volatile char buffer[4096]; 6 | 7 | int main(int ac, char **av) { 8 | for (;;) { 9 | for (int i = 0; i < 64000; i++) 10 | buffer[800] += i; 11 | for (int i = 0; i < 64000; i++) 12 | buffer[1800] += i; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /demo/Makefile.in: -------------------------------------------------------------------------------- 1 | FILES=$(wildcard *.c) 2 | TARGETS=$(FILES:.c=) 3 | OBJS=${FILES:.c=.o} 4 | CFLAGS=-g -std=gnu99 -I../src 5 | LDFLAGS=-L../src/ -g 6 | LDLIBS=-lmastik @LIBS@ 7 | 8 | 9 | all: ${TARGETS} 10 | 11 | ${TARGETS}: %: %.o 12 | ${CC} ${LDFLAGS} -o $@ $@.o ${LDLIBS} 13 | 14 | 15 | clean: 16 | rm -f ${TARGETS} ${OBJS} *.sig out 17 | 18 | distclean: clean 19 | rm Makefile 20 | -------------------------------------------------------------------------------- /Makefile.in: -------------------------------------------------------------------------------- 1 | SUBDIRS=src demo 2 | 3 | all: 4 | for f in ${SUBDIRS}; do ${MAKE} -C $$f || break ; done 5 | 6 | clean: cleansubdirs localclean 7 | 8 | cleansubdirs: 9 | for f in ${SUBDIRS}; do ${MAKE} -C $$f clean; done 10 | 11 | localclean: 12 | 13 | distclean: localclean 14 | for f in ${SUBDIRS}; do ${MAKE} -C $$f distclean; done 15 | rm Makefile 16 | rm config.status config.log 17 | 18 | maintainerclean: versionclean 19 | rm configure 20 | 21 | versionclean: distclean 22 | rm -rf autom4te.cache 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Object files 5 | *.o 6 | *.ko 7 | *.obj 8 | *.elf 9 | 10 | # Linker output 11 | *.ilk 12 | *.map 13 | *.exp 14 | 15 | # Precompiled Headers 16 | *.gch 17 | *.pch 18 | 19 | # Libraries 20 | *.lib 21 | *.a 22 | *.la 23 | *.lo 24 | 25 | # Shared objects (inc. Windows DLLs) 26 | *.dll 27 | *.so 28 | *.so.* 29 | *.dylib 30 | 31 | # Executables 32 | *.exe 33 | *.out 34 | *.app 35 | *.i*86 36 | *.x86_64 37 | *.hex 38 | 39 | # Debug files 40 | *.dSYM/ 41 | *.su 42 | *.idb 43 | *.pdb 44 | 45 | # Kernel Module Compile Results 46 | *.mod* 47 | *.cmd 48 | .tmp_versions/ 49 | modules.order 50 | Module.symvers 51 | Mkfile.old 52 | dkms.conf 53 | -------------------------------------------------------------------------------- /src/util.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 CSIRO 3 | * 4 | * This file is part of Mastik. 5 | * 6 | * Mastik is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * Mastik is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with Mastik. If not, see . 18 | */ 19 | 20 | #ifndef __UTIL_H__ 21 | #define __UTIL_H__ 1 22 | 23 | const char *mastik_version(); 24 | 25 | void *map_offset(const char *file, uint64_t offset); 26 | void unmap_offset(void *address); 27 | 28 | void delayloop(uint32_t cycles); 29 | 30 | void setaffinity(int cpu); 31 | 32 | #endif // __UTIL_H__ 33 | -------------------------------------------------------------------------------- /src/Makefile.in: -------------------------------------------------------------------------------- 1 | LIB=libmastik.a 2 | LIBSRCS=vlist.c l3.c timestats.c l1.c l1i.c fr.c util.c pda.c symbol.c ff.c @SYMBOL_SRCS@ 3 | LIBOBJS=${LIBSRCS:.c=.o} 4 | VERSION=$(shell cut -f 1 -d \ ../VERSION) 5 | 6 | all: ${LIB} ${TARGETS} 7 | 8 | include LocalMakefile 9 | 10 | 11 | 12 | CFLAGS=-g -std=gnu99 -O2 -DNDEBUG -DNONBLOCK_TRACE -DNONBLOCK_TSC ${EXTRA_CFLAGS} 13 | 14 | 15 | 16 | 17 | 18 | ${LIB}:${LIBOBJS} 19 | ar crv ${LIB} ${LIBOBJS} 20 | ranlib ${LIB} 21 | 22 | l3.o: l3.h vlist.h timestats.h low.h config.h 23 | 24 | vlist.o: vlist.h config.h 25 | 26 | timestats.o: timestats.h config.h 27 | 28 | 29 | #pp.o: vlist.h pp.h low.h 30 | 31 | l1.o: l1.h low.h config.h 32 | 33 | l1i.o: l1i.h low.h config.h 34 | 35 | btb.o: btb.h low.h config.h 36 | 37 | ff.o: ff.h low.h vlist.h timestats.h config.h 38 | 39 | fr.o: fr.h low.h vlist.h config.h 40 | 41 | pda.o: pda.h low.h vlist.h config.h 42 | 43 | 44 | symbol.o: symbol.h util.h config.h 45 | 46 | LocalMakefile: 47 | touch LocalMakefile 48 | 49 | clean: 50 | rm -f ${LIB} ${LIBOBJS} 51 | 52 | distclean: clean 53 | rm Makefile config.h 54 | -------------------------------------------------------------------------------- /src/symbol.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 CSIRO 3 | * 4 | * This file is part of Mastik. 5 | * 6 | * Mastik is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * Mastik is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with Mastik. If not, see . 18 | */ 19 | 20 | #ifndef __SYMBOL_H__ 21 | #define __SYMBOL_H__ 1 22 | 23 | uint64_t sym_getsymboloffset(const char *file, const char *symbol); 24 | 25 | uint64_t sym_loadersymboloffset(const char *file, const char *symbol); 26 | uint64_t sym_debuglineoffset(const char *file, const char *src, int lineno); 27 | uint64_t sym_addresstooffset(const char *file, uint64_t address); 28 | 29 | #endif // __SYMBOL_H__ 30 | -------------------------------------------------------------------------------- /demo/FR-1-file-access.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 CSIRO 3 | * 4 | * This file is part of Mastik. 5 | * 6 | * Mastik is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * Mastik is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with Mastik. If not, see . 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | int main(int ac, char **av) { 27 | fr_t fr = fr_prepare(); 28 | 29 | void *ptr = map_offset("FR-1-file-access.c", 0); 30 | fr_monitor(fr, ptr); 31 | 32 | uint16_t res[1]; 33 | fr_probe(fr, res); 34 | 35 | for (;;) { 36 | fr_probe(fr, res); 37 | if (res[0] < 100) 38 | printf("FR-1-file-access.c accessed\n"); 39 | delayloop(10000); 40 | } 41 | } 42 | 43 | -------------------------------------------------------------------------------- /src/timestats.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 CSIRO 3 | * 4 | * This file is part of Mastik. 5 | * 6 | * Mastik is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * Mastik is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with Mastik. If not, see . 18 | */ 19 | 20 | #ifndef __TIMESTATS_H__ 21 | #define __TIMESTATS_H__ 1 22 | 23 | #define TIME_MAX 1024 24 | 25 | typedef struct ts *ts_t; 26 | 27 | ts_t ts_alloc(); 28 | void ts_free(ts_t ts); 29 | 30 | void ts_clear(ts_t ts); 31 | 32 | // tm > 0 33 | void ts_add(ts_t ts, int tm); 34 | 35 | // tm > 0 36 | uint32_t ts_get(ts_t ts, int tm); 37 | 38 | uint32_t ts_outliers(ts_t ts); 39 | 40 | int ts_median(ts_t ts); 41 | 42 | int ts_max(ts_t ts); 43 | int ts_percentile(ts_t ts, int percentile); 44 | 45 | int ts_mean(ts_t ts, int scale); 46 | 47 | #endif // __TIMESTATS_H__ 48 | -------------------------------------------------------------------------------- /demo/FR-function-call-nodelay.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 CSIRO 3 | * 4 | * This file is part of Mastik. 5 | * 6 | * Mastik is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * Mastik is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with Mastik. If not, see . 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | int main(int ac, char **av) { 27 | void *ptr = map_offset("/usr/lib64/libc-2.17.so", 0x6d300); 28 | if (ptr == NULL) 29 | exit(0); 30 | 31 | fr_t fr = fr_prepare(); 32 | fr_monitor(fr, ptr); 33 | 34 | uint16_t res[1]; 35 | fr_probe(fr, res); 36 | 37 | int lines=0; 38 | for (;;) { 39 | fr_probe(fr, res); 40 | if (res[0] < 100) 41 | printf("%4d: %s", ++lines, "Call to puts\n"); 42 | } 43 | } 44 | 45 | -------------------------------------------------------------------------------- /demo/FR-function-call.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 CSIRO 3 | * 4 | * This file is part of Mastik. 5 | * 6 | * Mastik is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * Mastik is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with Mastik. If not, see . 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | int main(int ac, char **av) { 27 | void *ptr = map_offset("/usr/lib64/libc-2.17.so", 0x6d300); 28 | if (ptr == NULL) 29 | exit(0); 30 | 31 | fr_t fr = fr_prepare(); 32 | fr_monitor(fr, ptr); 33 | 34 | uint16_t res[1]; 35 | fr_probe(fr, res); 36 | 37 | int lines=0; 38 | for (;;) { 39 | fr_probe(fr, res); 40 | if (res[0] < 100) 41 | printf("%4d: %s", ++lines, "Call to puts\n"); 42 | delayloop(10000); 43 | } 44 | } 45 | 46 | -------------------------------------------------------------------------------- /demo/FR-2-file-access.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 CSIRO 3 | * 4 | * This file is part of Mastik. 5 | * 6 | * Mastik is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * Mastik is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with Mastik. If not, see . 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | int main(int ac, char **av) { 27 | fr_t fr = fr_prepare(); 28 | 29 | void *ptr = map_offset("FR-1-file-access.c", 0); 30 | fr_monitor(fr, ptr); 31 | ptr = map_offset("FR-2-file-access.c", 0); 32 | fr_monitor(fr, ptr); 33 | 34 | uint16_t res[2]; 35 | fr_probe(fr, res); 36 | 37 | for (;;) { 38 | fr_probe(fr, res); 39 | if (res[0] < 100) 40 | printf("FR-1-file-access.c accessed\n"); 41 | if (res[1] < 100) 42 | printf("FR-2-file-access.c accessed\n"); 43 | delayloop(10000); 44 | } 45 | } 46 | 47 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Mastik: : A Micro-Architectural Side-Channel Toolkit 2 | 3 | ## About 4 | 5 | Micro-architectural side-channel attacks exploit contention on internal components 6 | of the processor to leak information between processes. 7 | While in theory such attacks are straightforward, 8 | practical implementations tend to be finicky and 9 | require significant understanding of poorly documented processor features 10 | and other domain-specific arcane knowledge. 11 | Consequently, there is a barrier to entry into work on 12 | micro-architectural side-channel attacks, 13 | which hinders the development of the area and the analysis of the 14 | resilience of existing software against such attacks. 15 | 16 | This repository contains Mastik, a toolkit for experimenting with micro-architectural side-channel attacks. Mastik aims to provide implementations of published attack and analysis techniques. 17 | Currently, Mastik supports six side-channel attack techniques on the Intel x86-64 architecture: 18 | 19 | - Prime+Probe on the L1 data cache 20 | - Prime+Probe on the L1 instruction cache 21 | - Prime+Probe on the Last Level Cache 22 | - Flush+Reload 23 | - Flush+Flush 24 | - Performance-degradation attack 25 | 26 | 27 | ## Usage 28 | 29 | For example of usage look at the demo folder. 30 | 31 | Additionally go to the Mastik homepage for documentation. 32 | 33 | ## Links 34 | 35 | The Mastik Home page can be found [HERE](http://cs.adelaide.edu.au/~yval/Mastik/). 36 | -------------------------------------------------------------------------------- /src/l1i.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 CSIRO 3 | * 4 | * This file is part of Mastik. 5 | * 6 | * Mastik is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * Mastik is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with Mastik. If not, see . 18 | */ 19 | 20 | #ifndef __L1I_H__ 21 | #define __L1I_H__ 1 22 | 23 | #define L1I_SETS 64 24 | 25 | typedef struct l1ipp *l1ipp_t; 26 | 27 | l1ipp_t l1i_prepare(); 28 | void l1i_release(l1ipp_t l1i); 29 | 30 | int l1i_monitor(l1ipp_t l1i, int line); 31 | int l1i_unmonitor(l1ipp_t l1i, int line); 32 | void l1i_monitorall(l1ipp_t l1i); 33 | void l1i_unmonitorall(l1ipp_t l1i); 34 | int l1i_getmonitoredset(l1ipp_t l1i, int *lines, int nlines); 35 | void l1i_randomise(l1ipp_t l1); 36 | 37 | void l1i_probe(l1ipp_t l1, uint16_t *results); 38 | 39 | // Slot is currently not implemented 40 | int l1i_repeatedprobe(l1ipp_t l1, int nrecords, uint16_t *results, int slot); 41 | 42 | 43 | 44 | #endif // __L1I_H__ 45 | 46 | -------------------------------------------------------------------------------- /src/l1.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 CSIRO 3 | * 4 | * This file is part of Mastik. 5 | * 6 | * Mastik is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * Mastik is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with Mastik. If not, see . 18 | */ 19 | 20 | #ifndef __L1_H__ 21 | #define __L1_H__ 1 22 | 23 | #define L1_SETS 64 24 | 25 | typedef struct l1pp *l1pp_t; 26 | 27 | l1pp_t l1_prepare(void); 28 | void l1_release(l1pp_t l1); 29 | 30 | int l1_monitor(l1pp_t l1, int line); 31 | int l1_unmonitor(l1pp_t l1, int line); 32 | void l1_monitorall(l1pp_t l1); 33 | void l1_unmonitorall(l1pp_t l1); 34 | int l1_getmonitoredset(l1pp_t l1, int *lines, int nlines); 35 | void l1_randomise(l1pp_t l1); 36 | 37 | void l1_probe(l1pp_t l1, uint16_t *results); 38 | void l1_bprobe(l1pp_t l1, uint16_t *results); 39 | 40 | // Slot is currently not implemented 41 | int l1_repeatedprobe(l1pp_t l1, int nrecords, uint16_t *results, int slot); 42 | 43 | 44 | 45 | #endif // __L1_H__ 46 | 47 | -------------------------------------------------------------------------------- /demo/FR-flush.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 CSIRO 3 | * 4 | * This file is part of Mastik. 5 | * 6 | * Mastik is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * Mastik is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with Mastik. If not, see . 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | #include 28 | #include 29 | 30 | #define BINARY "gpg-1.4.13" 31 | 32 | int main(int ac, char **av) { 33 | pda_t pda = pda_prepare(); 34 | 35 | void *ptr = map_offset(BINARY, sym_getsymboloffset(BINARY, "mpihelp_sub_n")); 36 | void *ptr2 = map_offset(BINARY, sym_getsymboloffset(BINARY, "mpihelp_sub_n+64")); 37 | if (ptr == NULL || ptr2==NULL) { 38 | printf("Bad symbol translation\n"); 39 | exit(1); 40 | } 41 | pda_target(pda, ptr); 42 | pda_target(pda, ptr2); 43 | 44 | pda_activate(pda); 45 | wait(NULL); 46 | } 47 | 48 | -------------------------------------------------------------------------------- /demo/L3-scan.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 CSIRO 3 | * 4 | * This file is part of Mastik. 5 | * 6 | * Mastik is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * Mastik is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with Mastik. If not, see . 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | 24 | #include 25 | #include 26 | 27 | #define SAMPLES 200 28 | 29 | 30 | int main(int ac, char **av) { 31 | delayloop(3000000000U); 32 | 33 | l3pp_t l3 = l3_prepare(NULL); 34 | 35 | int nsets = l3_getSets(l3); 36 | 37 | uint16_t *res = calloc(SAMPLES, sizeof(uint16_t)); 38 | for (int i = 0; i < SAMPLES; i+= 4096/sizeof(uint16_t)) 39 | res[i] = 1; 40 | 41 | for (int i = 0; i < nsets; i++) { 42 | l3_unmonitorall(l3); 43 | l3_monitor(l3, i); 44 | 45 | l3_repeatedprobecount(l3, SAMPLES, res, 2000); 46 | 47 | for (int j = 0; j < SAMPLES; j++) { 48 | printf("%4d ", (int16_t)res[j]); 49 | } 50 | putchar('\n'); 51 | } 52 | 53 | free(res); 54 | l3_release(l3); 55 | } 56 | -------------------------------------------------------------------------------- /demo/L3-capture.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 CSIRO 3 | * 4 | * This file is part of Mastik. 5 | * 6 | * Mastik is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * Mastik is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with Mastik. If not, see . 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | 24 | #include 25 | #include 26 | 27 | #define SAMPLES 1000 28 | 29 | 30 | int main(int ac, char **av) { 31 | delayloop(3000000000U); 32 | 33 | l3pp_t l3 = l3_prepare(NULL); 34 | 35 | int nsets = l3_getSets(l3); 36 | int nmonitored = nsets/64; 37 | 38 | for (int i = 17; i < nsets; i += 64) 39 | l3_monitor(l3, i); 40 | 41 | 42 | uint16_t *res = calloc(SAMPLES * nmonitored, sizeof(uint16_t)); 43 | for (int i = 0; i < SAMPLES * nmonitored; i+= 4096/sizeof(uint16_t)) 44 | res[i] = 1; 45 | 46 | l3_repeatedprobe(l3, SAMPLES, res, 0); 47 | 48 | 49 | for (int i = 0; i < SAMPLES; i++) { 50 | for (int j = 0; j < nmonitored; j++) { 51 | printf("%4d ", res[i*nmonitored + j]); 52 | } 53 | putchar('\n'); 54 | } 55 | 56 | free(res); 57 | l3_release(l3); 58 | } 59 | -------------------------------------------------------------------------------- /demo/L3-capturecount.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 CSIRO 3 | * 4 | * This file is part of Mastik. 5 | * 6 | * Mastik is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * Mastik is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with Mastik. If not, see . 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | 24 | #include 25 | #include 26 | 27 | #define SAMPLES 1000 28 | 29 | 30 | int main(int ac, char **av) { 31 | delayloop(3000000000U); 32 | 33 | l3pp_t l3 = l3_prepare(NULL); 34 | 35 | int nsets = l3_getSets(l3); 36 | int nmonitored = nsets/64; 37 | 38 | for (int i = 17; i < nsets; i += 64) 39 | l3_monitor(l3, i); 40 | 41 | 42 | uint16_t *res = calloc(SAMPLES * nmonitored, sizeof(uint16_t)); 43 | for (int i = 0; i < SAMPLES * nmonitored; i+= 4096/sizeof(uint16_t)) 44 | res[i] = 1; 45 | 46 | l3_repeatedprobecount(l3, SAMPLES, res, 0); 47 | 48 | for (int i = 0; i < SAMPLES; i++) { 49 | for (int j = 0; j < nmonitored; j++) { 50 | printf("%4d ", res[i*nmonitored + j]); 51 | } 52 | putchar('\n'); 53 | } 54 | 55 | free(res); 56 | l3_release(l3); 57 | } 58 | -------------------------------------------------------------------------------- /src/vlist.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 CSIRO 3 | * 4 | * This file is part of Mastik. 5 | * 6 | * Mastik is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * Mastik is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with Mastik. If not, see . 18 | */ 19 | 20 | #ifndef __VLIST_H__ 21 | #define __VLIST_H__ 22 | 23 | typedef struct vlist *vlist_t; 24 | 25 | vlist_t vl_new(); 26 | void vl_free(vlist_t vl); 27 | 28 | inline void *vl_get(vlist_t vl, int ind); 29 | void vl_set(vlist_t vl, int ind, void *dat); 30 | int vl_push(vlist_t vl, void *dat); 31 | void *vl_pop(vlist_t vl); 32 | void *vl_poprand(vlist_t vl); 33 | void *vl_del(vlist_t vl, int ind); 34 | inline int vl_len(vlist_t vl); 35 | void vl_insert(vlist_t vl, int ind, void *dat); 36 | int vl_find(vlist_t vl, void *dat); 37 | 38 | 39 | //--------------------------------------------- 40 | // Implementation details 41 | //--------------------------------------------- 42 | 43 | struct vlist { 44 | int size; 45 | int len; 46 | void **data; 47 | }; 48 | 49 | inline void *vl_get(vlist_t vl, int ind) { 50 | assert(vl != NULL); 51 | assert(ind < vl->len); 52 | return vl->data[ind]; 53 | } 54 | 55 | inline int vl_len(vlist_t vl) { 56 | assert(vl != NULL); 57 | return vl->len; 58 | } 59 | 60 | 61 | 62 | #endif // __VLIST_H__ 63 | -------------------------------------------------------------------------------- /configure.ac: -------------------------------------------------------------------------------- 1 | # -*- Autoconf -*- 2 | # Process this file with autoconf to produce a configure script. 3 | 4 | AC_PREREQ([2.69]) 5 | AC_INIT([mastik], [0.02], [yval@cs.adelaide.edu.au]) 6 | AC_CONFIG_SRCDIR([src/symbol.c]) 7 | AC_CONFIG_HEADERS([src/config.h]) 8 | 9 | # Checks for programs. 10 | AC_PROG_CC 11 | 12 | # Checks for libraries. 13 | AC_CHECK_LIB([bfd], [bfd_init]) 14 | AC_CHECK_LIB([elf], [elf_version]) 15 | AC_CHECK_LIB([dwarf], [dwarf_init]) 16 | 17 | 18 | 19 | # Checks for header files. 20 | AC_CHECK_HEADERS([fcntl.h stdint.h stdlib.h string.h strings.h unistd.h sched.h sys/prctl.h]) 21 | AC_CHECK_HEADERS([bfd.h]) 22 | 23 | mastik_have_dwarf="no" 24 | if test "x$ac_cv_header_bfd_h" = x"yes" ; then 25 | SYMBOL_SRCS=symbol_bfd.c 26 | AC_DEFINE([HAVE_SYMBOLS], [1], [Have support for parsing loader symbol table]) 27 | AC_CHECK_HEADERS([libdwarf.h libdwarf/libdwarf.h], [mastik_have_dwarf="yes"]) 28 | if test x"$mastik_have_dwarf" = x"yes" ; then 29 | AC_DEFINE([HAVE_DWARF], [1], [Have support for dwarf]) 30 | fi 31 | else 32 | AC_CHECK_HEADERS([mach-o/loader.h]) 33 | if test x"$ac_cv_header_mach_o_loader_h" = x"yes"; then 34 | SYMBOL_SRCS=symbol_mach.c 35 | AC_DEFINE([HAVE_SYMBOLS], [1], [Have support for parsing loader symbol table]) 36 | fi 37 | fi 38 | 39 | # Checks for typedefs, structures, and compiler characteristics. 40 | AC_C_INLINE 41 | AC_TYPE_INT16_T 42 | AC_TYPE_PID_T 43 | AC_TYPE_SIZE_T 44 | AC_TYPE_UINT16_T 45 | AC_TYPE_UINT32_T 46 | AC_TYPE_UINT64_T 47 | AC_TYPE_UINT8_T 48 | 49 | # Checks for library functions. 50 | AC_FUNC_FORK 51 | AC_FUNC_MALLOC 52 | AC_FUNC_MMAP 53 | AC_FUNC_REALLOC 54 | AC_CHECK_FUNCS([bzero memset munmap strchr strdup strtoul strtoull uname]) 55 | AC_CHECK_FUNCS([mmap64 sched_setaffinity]) 56 | 57 | AC_SUBST(SYMBOL_SRCS, ["$SYMBOL_SRCS"]) 58 | 59 | AC_CONFIG_FILES([Makefile]) 60 | AC_CONFIG_FILES([src/Makefile]) 61 | AC_CONFIG_FILES([demo/Makefile]) 62 | AC_OUTPUT 63 | -------------------------------------------------------------------------------- /demo/FF-gnupg-1.4.13.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 CSIRO 3 | * 4 | * This file is part of Mastik. 5 | * 6 | * Mastik is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * Mastik is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with Mastik. If not, see . 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | #define SAMPLES 100000 29 | #define SLOT 2000 30 | #define THRESHOLD 100 31 | 32 | #define BINARY "gpg-1.4.13" 33 | 34 | char *monitor[] = { 35 | "mpih-mul.c:85", 36 | "mpih-mul.c:271", 37 | "mpih-div.c:356" 38 | }; 39 | int nmonitor = sizeof(monitor)/sizeof(monitor[0]); 40 | 41 | int main(int ac, char **av) { 42 | delayloop(2000000000); 43 | ff_t ff = ff_prepare(); 44 | for (int i = 0; i < nmonitor; i++) { 45 | uint64_t offset = sym_getsymboloffset(BINARY, monitor[i]); 46 | if (offset == ~0ULL) { 47 | fprintf(stderr, "Cannot find %s in %s\n", monitor[i], BINARY); 48 | exit(1); 49 | } 50 | ff_monitor(ff, map_offset(BINARY, offset)); 51 | } 52 | 53 | uint16_t *res = malloc(SAMPLES * nmonitor * sizeof(uint16_t)); 54 | for (int i = 0; i < SAMPLES * nmonitor ; i+= 4096/sizeof(uint16_t)) 55 | res[i] = 1; 56 | ff_probe(ff, res); 57 | 58 | int l; 59 | do { 60 | l = ff_trace(ff, SAMPLES, res, SLOT, THRESHOLD, 500); 61 | } while (l < 10000); 62 | for (int i = 0; i < l; i++) { 63 | for (int j = 0; j < nmonitor; j++) 64 | printf("%d ", res[i * nmonitor + j]); 65 | putchar('\n'); 66 | } 67 | 68 | free(res); 69 | ff_release(ff); 70 | } 71 | -------------------------------------------------------------------------------- /demo/L1-capture.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 CSIRO 3 | * 4 | * This file is part of Mastik. 5 | * 6 | * Mastik is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * Mastik is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with Mastik. If not, see . 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | 24 | #include 25 | #include 26 | 27 | #define MAX_SAMPLES 100000 28 | 29 | void usage(const char *prog) { 30 | fprintf(stderr, "Usage: %s \n", prog); 31 | exit(1); 32 | } 33 | 34 | int main(int ac, char **av) { 35 | int samples = 0; 36 | 37 | if (av[1] == NULL) 38 | usage(av[0]); 39 | samples = atoi(av[1]); 40 | if (samples < 0) 41 | usage(av[0]); 42 | if (samples > MAX_SAMPLES) 43 | samples = MAX_SAMPLES; 44 | l1pp_t l1 = l1_prepare(); 45 | 46 | int nsets = l1_getmonitoredset(l1, NULL, 0); 47 | 48 | int *map = calloc(nsets, sizeof(int)); 49 | l1_getmonitoredset(l1, map, nsets); 50 | 51 | int rmap[L1_SETS]; 52 | for (int i = 0; i < L1_SETS; i++) 53 | rmap[i] = -1; 54 | for (int i = 0; i < nsets; i++) 55 | rmap[map[i]] = i; 56 | 57 | 58 | uint16_t *res = calloc(samples * nsets, sizeof(uint16_t)); 59 | for (int i = 0; i < samples * nsets; i+= 4096/sizeof(uint16_t)) 60 | res[i] = 1; 61 | 62 | delayloop(3000000000U); 63 | l1_repeatedprobe(l1, samples, res, 0); 64 | 65 | 66 | 67 | for (int i = 0; i < samples; i++) { 68 | for (int j = 0; j < L1_SETS; j++) { 69 | if (rmap[j] == -1) 70 | printf(" 0 "); 71 | else 72 | printf("%3d ", res[i*nsets + rmap[j]]); 73 | } 74 | putchar('\n'); 75 | } 76 | 77 | free(map); 78 | free(res); 79 | l1_release(l1); 80 | } 81 | -------------------------------------------------------------------------------- /demo/FR-gnupg-1.4.13.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 CSIRO 3 | * 4 | * This file is part of Mastik. 5 | * 6 | * Mastik is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * Mastik is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with Mastik. If not, see . 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | #define SAMPLES 100000 29 | #define SLOT 2000 30 | #define THRESHOLD 100 31 | 32 | char *monitor[] = { 33 | "mpih-mul.c:85", 34 | "mpih-mul.c:271", 35 | "mpih-div.c:356" 36 | }; 37 | int nmonitor = sizeof(monitor)/sizeof(monitor[0]); 38 | 39 | void usage(const char *prog) { 40 | fprintf(stderr, "Usage: %s \n", prog); 41 | exit(1); 42 | } 43 | 44 | 45 | int main(int ac, char **av) { 46 | char *binary = av[1]; 47 | if (binary == NULL) 48 | usage(av[0]); 49 | 50 | fr_t fr = fr_prepare(); 51 | for (int i = 0; i < nmonitor; i++) { 52 | uint64_t offset = sym_getsymboloffset(binary, monitor[i]); 53 | if (offset == ~0ULL) { 54 | fprintf(stderr, "Cannot find %s in %s\n", monitor[i], binary); 55 | exit(1); 56 | } 57 | fr_monitor(fr, map_offset(binary, offset)); 58 | } 59 | 60 | uint16_t *res = malloc(SAMPLES * nmonitor * sizeof(uint16_t)); 61 | for (int i = 0; i < SAMPLES * nmonitor ; i+= 4096/sizeof(uint16_t)) 62 | res[i] = 1; 63 | fr_probe(fr, res); 64 | 65 | int l = fr_trace(fr, SAMPLES, res, SLOT, THRESHOLD, 500); 66 | for (int i = 0; i < l; i++) { 67 | for (int j = 0; j < nmonitor; j++) 68 | printf("%d ", res[i * nmonitor + j]); 69 | putchar('\n'); 70 | } 71 | 72 | free(res); 73 | fr_release(fr); 74 | } 75 | -------------------------------------------------------------------------------- /src/l3.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 CSIRO 3 | * 4 | * This file is part of Mastik. 5 | * 6 | * Mastik is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * Mastik is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with Mastik. If not, see . 18 | */ 19 | 20 | #ifndef __L3_H__ 21 | #define __L3_H__ 1 22 | 23 | typedef void (*l3progressNotification_t)(int count, int est, void *data); 24 | struct l3info { 25 | int associativity; 26 | int slices; 27 | int setsperslice; 28 | int bufsize; 29 | int flags; 30 | l3progressNotification_t progressNotification; 31 | void *progressNotificationData; 32 | }; 33 | 34 | typedef struct l3pp *l3pp_t; 35 | typedef struct l3info *l3info_t; 36 | 37 | 38 | #define L3FLAG_NOHUGEPAGES 0x01 39 | #define L3FLAG_USEPTE 0x02 40 | #define L3FLAG_NOPROBE 0x04 41 | 42 | 43 | l3pp_t l3_prepare(l3info_t l3info); 44 | void l3_release(l3pp_t l3); 45 | 46 | // Returns the number of probed sets in the LLC 47 | int l3_getSets(l3pp_t l3); 48 | 49 | // Returns the number slices 50 | int l3_getSlices(l3pp_t l3); 51 | 52 | // Returns the LLC associativity 53 | int l3_getAssociativity(l3pp_t l3); 54 | 55 | int l3_monitor(l3pp_t l3, int line); 56 | void l3_unmonitorall(l3pp_t l3); 57 | int l3_unmonitor(l3pp_t l3, int line); 58 | int l3_getmonitoredset(l3pp_t l3, int *lines, int nlines); 59 | 60 | void l3_randomise(l3pp_t l3); 61 | 62 | void l3_probe(l3pp_t l3, uint16_t *results); 63 | void l3_bprobe(l3pp_t l3, uint16_t *results); 64 | void l3_probecount(l3pp_t l3, uint16_t *results); 65 | void l3_bprobecount(l3pp_t l3, uint16_t *results); 66 | 67 | int l3_repeatedprobe(l3pp_t l3, int nrecords, uint16_t *results, int slot); 68 | int l3_repeatedprobecount(l3pp_t l3, int nrecords, uint16_t *results, int slot); 69 | 70 | 71 | #endif // __L3_H__ 72 | 73 | -------------------------------------------------------------------------------- /src/util.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 CSIRO 3 | * 4 | * This file is part of Mastik. 5 | * 6 | * Mastik is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * Mastik is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with Mastik. If not, see . 18 | */ 19 | 20 | #include "config.h" 21 | #define _GNU_SOURCE 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #ifdef HAVE_SCHED_H 28 | #include 29 | #endif 30 | 31 | #include "low.h" 32 | 33 | const char *mastik_version() { 34 | return PACKAGE_VERSION; 35 | } 36 | 37 | void *map_offset(const char *file, uint64_t offset) { 38 | int fd = open(file, O_RDONLY); 39 | if (fd < 0) 40 | return NULL; 41 | 42 | char *mapaddress = MAP_FAILED; 43 | #ifdef HAVE_MMAP64 44 | mapaddress = mmap(0, sysconf(_SC_PAGE_SIZE), PROT_READ, MAP_PRIVATE, fd, offset & ~(sysconf(_SC_PAGE_SIZE) -1)); 45 | #else 46 | mapaddress = mmap(0, sysconf(_SC_PAGE_SIZE), PROT_READ, MAP_PRIVATE, fd, ((off_t)offset) & ~(sysconf(_SC_PAGE_SIZE) -1)); 47 | #endif 48 | close(fd); 49 | if (mapaddress == MAP_FAILED) 50 | return NULL; 51 | return (void *)(mapaddress+(offset & (sysconf(_SC_PAGE_SIZE) -1))); 52 | } 53 | 54 | 55 | void unmap_offset(void *address) { 56 | munmap((char *)(((uintptr_t)address) & ~(sysconf(_SC_PAGE_SIZE) -1)), 57 | sysconf(_SC_PAGE_SIZE)); 58 | } 59 | 60 | 61 | void delayloop(uint32_t cycles) { 62 | uint64_t start = rdtscp64(); 63 | while ((rdtscp64()-start) < cycles) 64 | ; 65 | } 66 | 67 | int setaffinity(int cpu) { 68 | #ifdef HAVE_SCHED_SETAFFINITY 69 | cpu_set_t cs; 70 | CPU_ZERO(&cs); 71 | CPU_SET(cpu, &cs); 72 | if (sched_setaffinity(0, sizeof(cs), &cs) < 0) 73 | return -1; 74 | return 0; 75 | #else 76 | return -1; 77 | #endif 78 | } 79 | 80 | -------------------------------------------------------------------------------- /src/fr.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 CSIRO 3 | * 4 | * This file is part of Mastik. 5 | * 6 | * Mastik is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * Mastik is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with Mastik. If not, see . 18 | */ 19 | 20 | #ifndef __FR_H__ 21 | #define __FR_H__ 1 22 | 23 | typedef struct fr *fr_t; 24 | 25 | 26 | 27 | fr_t fr_prepare(); 28 | void fr_release(fr_t fr); 29 | 30 | int fr_monitor(fr_t fr, void *adrs); 31 | int fr_unmonitor(fr_t fr, void *adrs); 32 | int fr_getmonitoredset(fr_t fr, void **adrss, int nlines); 33 | void fr_randomise(fr_t fr); 34 | 35 | int fr_evict(fr_t fr, void *adrs); 36 | int fr_unevict(fr_t fr, void *adrs); 37 | int fr_getevictedset(fr_t fr, void **adrss, int nlines); 38 | 39 | int fr_probethreshold(void); 40 | 41 | 42 | void fr_probe(fr_t fr, uint16_t *results); 43 | 44 | 45 | /* 46 | * Performs repeated calls to fr_probe. Prameters are: 47 | * fr - The fr descriptor 48 | * max_records - The number of records to collect 49 | * results - An array for results of size fr_len(fr) * max_records 50 | * slot - The length of a time-slot in cycles. Use 0 to have flexible slot size 51 | * threshold - Activity threshold. If >0, capture starts when one of the results is above the threshold 52 | * max_idle - Stop capture when read more than max_idle idle records. 53 | * 54 | * Capture record starts when a probe is above the threshold. Hence threshold=0 means that capture 55 | * starts immediately. 56 | * Capture stops when either max_idle records have no value above the threshold or when max_records 57 | * are captured. max_idle is ignored if =0. 58 | * 59 | * Output is the reload times for each of the monitored addresses in each time slot. 60 | * Missed slots are indicted using time 0. 61 | * The return value is the number of records captured. 62 | */ 63 | int fr_trace(fr_t fr, int max_records, uint16_t *results, int slot, int threshold, int max_idle); 64 | int fr_trtsc(fr_t fr, int max_records, uint64_t *starttsc, uint16_t *results, int slot, int threshold, int max_idle); 65 | 66 | int fr_repeatedprobe(fr_t fr, int max_records, uint16_t *results, int slot); 67 | 68 | 69 | 70 | #endif // __FR_H__ 71 | 72 | -------------------------------------------------------------------------------- /demo/FR-threshold.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 CSIRO 3 | * 4 | * This file is part of Mastik. 5 | * 6 | * Mastik is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * Mastik is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with Mastik. If not, see . 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | #define SAMPLES 100000 30 | 31 | int forkslave(void *ptr); 32 | int compare(const void *p1, const void *p2); 33 | 34 | 35 | int main(int ac, char **av) { 36 | fr_t fr = fr_prepare(); 37 | 38 | void *ptr = map_offset("FR-threshold.c", 0); 39 | fr_monitor(fr, ptr); 40 | 41 | uint16_t *memory = malloc(SAMPLES * sizeof(uint16_t)); 42 | memset(memory, 1, SAMPLES * sizeof(uint16_t)); 43 | 44 | for (int i = 0; i < SAMPLES; i++) { 45 | fr_probe(fr, memory + i); 46 | delayloop(10000); 47 | } 48 | 49 | pid_t pid = forkslave(ptr); 50 | if (pid == -1) { 51 | perror("fork"); 52 | exit(1); 53 | } 54 | 55 | uint16_t *cache = malloc(SAMPLES * sizeof(uint16_t)); 56 | memset(cache, 1, SAMPLES * sizeof(uint16_t)); 57 | 58 | for (int i = 0; i < SAMPLES; i++) { 59 | fr_probe(fr, cache + i); 60 | delayloop(10000); 61 | } 62 | 63 | kill(pid, SIGKILL); 64 | 65 | qsort(memory, SAMPLES, sizeof(uint16_t), compare); 66 | qsort(cache, SAMPLES, sizeof(uint16_t), compare); 67 | printf(" : Mem Cache\n"); 68 | printf("Minimum : %5d %5d\n", memory[0], cache[0]); 69 | printf("Bottom decile: %5d %5d\n", memory[SAMPLES/10], cache[SAMPLES/10]); 70 | printf("Median : %5d %5d\n", memory[SAMPLES/2], cache[SAMPLES/2]); 71 | printf("Top decile : %5d %5d\n", memory[(SAMPLES * 9)/10], cache[(SAMPLES * 9)/10]); 72 | printf("Maximum : %5d %5d\n", memory[SAMPLES-1], cache[SAMPLES-1]); 73 | 74 | free(memory); 75 | free(cache); 76 | fr_release(fr); 77 | } 78 | 79 | volatile int a; 80 | 81 | int forkslave(void *ptr) { 82 | pid_t pid = fork(); 83 | if (pid != 0) 84 | return pid; 85 | for(;;) 86 | a += *(char *)ptr; 87 | } 88 | 89 | int compare(const void *p1, const void *p2) { 90 | uint16_t u1 = *(uint16_t *)p1; 91 | uint16_t u2 = *(uint16_t *)p2; 92 | 93 | return (int)u1 - (int)u2; 94 | } 95 | 96 | -------------------------------------------------------------------------------- /src/timestats.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 CSIRO 3 | * 4 | * This file is part of Mastik. 5 | * 6 | * Mastik is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * Mastik is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with Mastik. If not, see . 18 | */ 19 | 20 | #include "config.h" 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | #include "timestats.h" 28 | 29 | 30 | 31 | struct ts { 32 | uint32_t data[TIME_MAX]; 33 | }; 34 | 35 | 36 | static ts_t lastfree = NULL; 37 | 38 | ts_t ts_alloc() { 39 | ts_t rv = lastfree; 40 | if (rv == NULL) 41 | rv = (ts_t)malloc(sizeof(struct ts)); 42 | else 43 | lastfree = NULL; 44 | ts_clear(rv); 45 | return rv; 46 | } 47 | 48 | void ts_free(ts_t ts) { 49 | if (lastfree == NULL) 50 | lastfree = ts; 51 | else 52 | free(ts); 53 | } 54 | 55 | void ts_clear(ts_t ts) { 56 | bzero(ts, sizeof(struct ts)); 57 | } 58 | 59 | void ts_add(ts_t ts, int tm) { 60 | if (tm < TIME_MAX && tm >= 0) 61 | ts->data[tm]++; 62 | else 63 | ts->data[0]++; 64 | } 65 | 66 | uint32_t ts_get(ts_t ts, int tm) { 67 | return tm < TIME_MAX && tm > 0 ? ts->data[tm] : 0; 68 | } 69 | 70 | uint32_t ts_outliers(ts_t ts) { 71 | return ts->data[0]; 72 | } 73 | 74 | 75 | int ts_median(ts_t ts) { 76 | int c = 0; 77 | for (int i = 0; i < TIME_MAX; i++) 78 | c += ts->data[i]; 79 | c = (c + 1) / 2; 80 | for (int i = 1; i < TIME_MAX; i++) 81 | if ((c -= ts->data[i]) < 0) 82 | return i; 83 | return 0; 84 | } 85 | 86 | int ts_max(ts_t ts) { 87 | for (int i = TIME_MAX; --i; ) 88 | if (ts->data[i] != 0) 89 | return i; 90 | return 0; 91 | } 92 | 93 | int ts_percentile(ts_t ts, int percentile) { 94 | int c = 0; 95 | for (int i = 0; i < TIME_MAX; i++) 96 | c += ts->data[i]; 97 | c = (c * percentile + 50) / 100; 98 | for (int i = 1; i < TIME_MAX; i++) 99 | if ((c -= ts->data[i]) < 0) 100 | return i; 101 | return ts_max(ts); 102 | } 103 | 104 | 105 | int ts_mean(ts_t ts, int scale) { 106 | uint64_t sum = 0; 107 | int count = 0; 108 | for (int i = 0; i < TIME_MAX; i++) { 109 | count += ts->data[i]; 110 | sum += i* (uint64_t)ts->data[i]; 111 | } 112 | return (int)((sum * scale)/count); 113 | } 114 | -------------------------------------------------------------------------------- /src/symbol.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 CSIRO 3 | * 4 | * This file is part of Mastik. 5 | * 6 | * Mastik is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * Mastik is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with Mastik. If not, see . 18 | */ 19 | 20 | #include "config.h" 21 | #define _GNU_SOURCE 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | #include "symbol.h" 31 | 32 | 33 | /* 34 | * Convert a symbolic reference to a file offset. Accepted formats are: 35 | * - offset to file 36 | * @
- virtual address of a location 37 | * - symbolic name, usually a function 38 | * : - Start of a source line 39 | * All optionally followed by [+-] 40 | */ 41 | uint64_t sym_getsymboloffset(const char *file, const char *symbol) { 42 | char *symcopy = strdup(symbol); 43 | uint64_t rv = ~0ULL; 44 | char *srcfile = NULL; 45 | uint32_t lineno = 0; 46 | int offset = 0; 47 | 48 | char *ptr = strchr(symcopy, ':'); 49 | if (ptr != NULL) { 50 | *ptr = '\0'; 51 | srcfile = symcopy; 52 | ptr++; 53 | } else { 54 | ptr = symcopy; 55 | } 56 | 57 | char *tmp = strchr(ptr, '+'); 58 | if (tmp != NULL) { 59 | *tmp = '\0'; 60 | tmp++; 61 | offset = strtoul(tmp, NULL, 0); 62 | } else { 63 | tmp = strchr(ptr, '-'); 64 | if (tmp != NULL) { 65 | *tmp = '\0'; 66 | tmp++; 67 | offset = -strtoul(tmp, NULL, 0); 68 | } 69 | } 70 | 71 | if (srcfile) { 72 | lineno = strtoul(ptr, NULL, 0); 73 | rv = sym_debuglineoffset(file, srcfile, lineno); 74 | } else if (*ptr == '@') { 75 | rv = sym_addresstooffset(file, strtoull(ptr+1, NULL, 0)); 76 | } else if (isdigit(*ptr)) { 77 | rv = strtoull(ptr, NULL, 0); 78 | } else { 79 | rv = sym_loadersymboloffset(file, ptr); 80 | } 81 | 82 | if (rv != ~0ULL) 83 | rv += offset; 84 | 85 | out: 86 | if (symcopy) 87 | free(symcopy); 88 | return rv; 89 | } 90 | 91 | 92 | #ifndef HAVE_SYMBOLS 93 | uint64_t sym_loadersymboloffset(const char *file, const char *name) { 94 | return ~0ULL; 95 | } 96 | 97 | uint64_t sym_addresstooffset(const char *file, uint64_t address) { 98 | return ~0ULL; 99 | } 100 | 101 | uint64_t sym_debuglineoffset(const char *file, const char *src, int lineno) { 102 | return ~0ULL; 103 | } 104 | #endif 105 | 106 | -------------------------------------------------------------------------------- /src/pda.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 CSIRO 3 | * 4 | * This file is part of Mastik. 5 | * 6 | * Mastik is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * Mastik is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with Mastik. If not, see . 18 | */ 19 | 20 | #ifndef __PDA_H__ 21 | #define __PDA_H__ 1 22 | 23 | 24 | /* 25 | * Perfoms a performance degradation attack. 26 | * See: * T. Allan, B.B. Brumley, K. Falkner, J. van de Pol and 27 | * Y. Yarom, "Amplifying Side Channels Through Performance Degradation," 28 | * ACSAC 2016 (eprint 2015/1141) 29 | * 30 | * pda_t abstracts over a single attacking thread (currently implemented as 31 | * a child process). Each attack targets a set of addresses which it continuosly 32 | * evicts from the cache. 33 | * To ensure attack effectiveness, the target set cannot be changed while the 34 | * attack is active. Any changes to the target set becomes effective in the 35 | * activation following the change. 36 | */ 37 | 38 | typedef struct pda *pda_t; 39 | 40 | 41 | 42 | /* 43 | * Prepares a new instance of a performance degradation attack 44 | */ 45 | pda_t pda_prepare(); 46 | 47 | 48 | /* 49 | * Releases all resources associated with a performace degradation attack 50 | */ 51 | void pda_release(pda_t pda); 52 | 53 | /* 54 | * Adds an address to be targeted in a performance degradation attack 55 | * The address will be targeted from the next activation of the attack. 56 | * Does not affect an active attack. 57 | */ 58 | int pda_target(pda_t pda, void *adrs); 59 | 60 | /* 61 | * Removes an address from a performance degradation attack 62 | * The address will be removed from the next activation of the attack. 63 | * Does not affect an active attack. 64 | */ 65 | int pda_untarget(pda_t pda, void *adrs); 66 | 67 | /* 68 | * Returns the list of addresses to be targeted in the next activation of the attack. 69 | */ 70 | int pda_gettargetset(pda_t pda, void **adrss, int nlines); 71 | 72 | /* 73 | * Randomises the order of accessing targeted addresses in the attack. Effective 74 | * starting the next activation. 75 | */ 76 | void pda_randomise(pda_t pda); 77 | 78 | /* 79 | * Activates a performance degradation attack 80 | * 81 | * If executed on an active attack, the attack may be stopped and restarted to accommodate 82 | * any changes to the target set. 83 | */ 84 | void pda_activate(pda_t pda); 85 | 86 | /* 87 | * Deactivates a performance degradation attack. 88 | */ 89 | void pda_deactivate(pda_t pda); 90 | 91 | /* 92 | * Returns true if the attack is currently active. 93 | */ 94 | int pda_isactive(pda_t pda); 95 | 96 | 97 | #endif // __PDA_H__ 98 | 99 | -------------------------------------------------------------------------------- /src/vlist.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 CSIRO 3 | * 4 | * This file is part of Mastik. 5 | * 6 | * Mastik is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * Mastik is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with Mastik. If not, see . 18 | */ 19 | 20 | #include "config.h" 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | #include "vlist.h" 27 | 28 | extern void *vl_get(vlist_t vl, int ind); 29 | extern int vl_len(vlist_t vl); 30 | 31 | #define VLIST_DEF_SIZE 16 32 | 33 | vlist_t vl_new() { 34 | vlist_t vl = (vlist_t)malloc(sizeof(struct vlist)); 35 | vl->size = VLIST_DEF_SIZE; 36 | vl->data = (void **)calloc(VLIST_DEF_SIZE, sizeof(void *)); 37 | vl->len = 0; 38 | return vl; 39 | } 40 | 41 | void vl_free(vlist_t vl) { 42 | assert(vl != NULL); 43 | free(vl->data); 44 | bzero(vl, sizeof(struct vlist)); 45 | free(vl); 46 | } 47 | 48 | void vl_set(vlist_t vl, int ind, void *dat) { 49 | assert(vl != NULL); 50 | assert(dat != NULL); 51 | assert(ind < vl->len); 52 | vl->data[ind] = dat; 53 | } 54 | 55 | static void vl_setsize(vlist_t vl, int size) { 56 | assert(vl != NULL); 57 | assert(size >= vl->len); 58 | void **old = vl->data; 59 | vl->data = (void **)realloc(old, size * sizeof(void *)); 60 | vl->size = size; 61 | } 62 | 63 | 64 | int vl_push(vlist_t vl, void *dat) { 65 | assert(vl != NULL); 66 | assert(dat != NULL); 67 | if (vl->len == vl->size) 68 | vl_setsize(vl, vl->size * 2); 69 | assert(vl->len < vl->size); 70 | vl->data[vl->len++] = dat; 71 | return vl->len - 1; 72 | } 73 | 74 | void *vl_pop(vlist_t vl) { 75 | assert(vl != NULL); 76 | if (vl->len == 0) 77 | return NULL; 78 | return vl->data[--vl->len]; 79 | } 80 | 81 | void *vl_del(vlist_t vl, int ind) { 82 | assert(vl != NULL); 83 | assert(ind < vl->len); 84 | void * rv = vl->data[ind]; 85 | vl->data[ind] = vl->data[--vl->len]; 86 | return rv; 87 | } 88 | 89 | void *vl_poprand(vlist_t vl) { 90 | assert(vl != NULL); 91 | if (vl->len == 0) 92 | return NULL; 93 | int ind = random() % vl->len; 94 | void *rv = vl->data[ind]; 95 | vl_del(vl, ind); 96 | return rv; 97 | } 98 | 99 | void vl_insert(vlist_t vl, int ind, void *dat) { 100 | assert(vl != NULL); 101 | assert(dat != NULL); 102 | assert(ind <= vl->len); 103 | if (ind == vl->len) { 104 | vl_push(vl, dat); 105 | } else { 106 | vl_push(vl, vl->data[ind]); 107 | vl->data[ind] = dat; 108 | } 109 | } 110 | 111 | int vl_find(vlist_t vl, void *dat) { 112 | assert(vl != NULL); 113 | assert(dat != NULL); 114 | for (int i = 0; i < vl->len; i++) 115 | if (vl->data[i] == dat) 116 | return i; 117 | return -1; 118 | } 119 | 120 | -------------------------------------------------------------------------------- /src/low.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 CSIRO 3 | * 4 | * This file is part of Mastik. 5 | * 6 | * Mastik is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * Mastik is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with Mastik. If not, see . 18 | */ 19 | 20 | #ifndef __LOW_H__ 21 | #define __LOW_H__ 22 | 23 | 24 | 25 | #define L3_THRESHOLD 140 26 | 27 | #ifdef PAGE_SIZE 28 | #undef PAGE_SIZE 29 | #endif 30 | #define PAGE_SIZE 4096 31 | 32 | 33 | static inline int memaccess(void *v) { 34 | int rv; 35 | asm volatile("mov (%1), %0": "+r" (rv): "r" (v):); 36 | return rv; 37 | } 38 | 39 | static inline uint32_t memaccesstime(void *v) { 40 | uint32_t rv; 41 | asm volatile ( 42 | "mfence\n" 43 | "lfence\n" 44 | "rdtscp\n" 45 | "mov %%eax, %%esi\n" 46 | "mov (%1), %%eax\n" 47 | "rdtscp\n" 48 | "sub %%esi, %%eax\n" 49 | : "=&a" (rv): "r" (v): "ecx", "edx", "esi"); 50 | return rv; 51 | } 52 | 53 | static inline void clflush(void *v) { 54 | asm volatile ("clflush 0(%0)": : "r" (v):); 55 | } 56 | 57 | static inline uint32_t rdtscp() { 58 | uint32_t rv; 59 | asm volatile ("rdtscp": "=a" (rv) :: "edx", "ecx"); 60 | return rv; 61 | } 62 | 63 | static inline uint64_t rdtscp64() { 64 | uint32_t low, high; 65 | asm volatile ("rdtscp": "=a" (low), "=d" (high) :: "ecx"); 66 | return (((uint64_t)high) << 32) | low; 67 | } 68 | 69 | static inline void mfence() { 70 | asm volatile("mfence"); 71 | } 72 | 73 | 74 | static inline void walk(void *p, int count) { 75 | if (p == NULL) 76 | return; 77 | asm volatile( 78 | "movq %0, %%rsi\n" 79 | "1:\n" 80 | "movq (%0), %0\n" 81 | "cmpq %0, %%rsi\n" 82 | "jnz 1b\n" 83 | "decl %1\n" 84 | "jnz 1b\n" 85 | : "+r" (p), "+r" (count)::"rsi"); 86 | } 87 | 88 | 89 | struct cpuidRegs { 90 | uint32_t eax; 91 | uint32_t ebx; 92 | uint32_t ecx; 93 | uint32_t edx; 94 | }; 95 | 96 | #define CPUID_CACHEINFO 4 97 | 98 | 99 | #define CACHETYPE_NULL 0 100 | #define CACHETYPE_DATA 1 101 | #define CACHETYPE_INSTRUCTION 2 102 | #define CACHETYPE_UNIFIED 3 103 | 104 | struct cpuidCacheInfo { 105 | uint32_t type:5; 106 | uint32_t level:3; 107 | uint32_t selfInitializing:1; 108 | uint32_t fullyAssociative:1; 109 | uint32_t reserved1:4; 110 | uint32_t logIds:12; 111 | uint32_t phyIds:6; 112 | 113 | uint32_t lineSize:12; 114 | uint32_t partitions:10; 115 | uint32_t associativity:10; 116 | 117 | uint32_t sets:32; 118 | 119 | uint32_t wbinvd:1; 120 | uint32_t inclusive:1; 121 | uint32_t complexIndex:1; 122 | uint32_t reserved2:29; 123 | }; 124 | 125 | union cpuid { 126 | struct cpuidRegs regs; 127 | struct cpuidCacheInfo cacheInfo; 128 | }; 129 | 130 | inline void cpuid(union cpuid *c) { 131 | asm volatile ("cpuid": "+a" (c->regs.eax), "+b" (c->regs.ebx), "+c" (c->regs.ecx), "+d" (c->regs.edx)); 132 | } 133 | 134 | static inline int slotwait(uint64_t slotend) { 135 | if (rdtscp64() > slotend) 136 | return 1; 137 | while (rdtscp64() < slotend) 138 | ; 139 | return 0; 140 | } 141 | 142 | 143 | 144 | #endif //__LOW_H__ 145 | -------------------------------------------------------------------------------- /src/pda.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 CSIRO 3 | * 4 | * This file is part of Mastik. 5 | * 6 | * Mastik is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * Mastik is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with Mastik. If not, see . 18 | */ 19 | 20 | #include "config.h" 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #ifdef HAVE_SYS_PRCTL_H 29 | #include 30 | #endif 31 | 32 | #include "low.h" 33 | #include "vlist.h" 34 | #include "pda.h" 35 | 36 | struct pda { 37 | vlist_t vl; 38 | pid_t child; 39 | int modified; 40 | int active; 41 | }; 42 | 43 | 44 | 45 | pda_t pda_prepare() { 46 | pda_t rv = malloc(sizeof(struct pda)); 47 | rv->vl = vl_new(); 48 | rv->child = -1; 49 | rv->modified = 0; 50 | rv->active = 0; 51 | return rv; 52 | } 53 | 54 | void pda_release(pda_t pda) { 55 | pda_deactivate(pda); 56 | vl_free(pda->vl); 57 | pda->vl = NULL; 58 | free(pda); 59 | } 60 | 61 | 62 | int pda_target(pda_t pda, void *adrs) { 63 | assert(pda != NULL); 64 | assert(adrs != NULL); 65 | vl_push(pda->vl, adrs); 66 | pda->modified = 1; 67 | return 1; 68 | } 69 | 70 | 71 | int pda_untarget(pda_t pda, void *adrs) { 72 | assert(pda != NULL); 73 | assert(adrs != NULL); 74 | int i; 75 | int count = 0; 76 | while ((i = vl_find(pda->vl, adrs)) >= 0) { 77 | vl_del(pda->vl, i); 78 | count++; 79 | } 80 | pda->modified = count != 0; 81 | return count; 82 | } 83 | 84 | 85 | int pda_gettargetedset(pda_t pda, void **adrss, int nlines) { 86 | assert(pda != NULL); 87 | 88 | if (adrss != NULL) { 89 | int l = vl_len(pda->vl); 90 | if (l > nlines) 91 | l = nlines; 92 | for (int i = 0; i < l; i++) 93 | adrss[i] = vl_get(pda->vl, i); 94 | } 95 | return vl_len(pda->vl); 96 | } 97 | 98 | void pda_randomise(pda_t pda) { 99 | assert(pda != NULL); 100 | assert(0); 101 | 102 | pda->modified = 1; 103 | } 104 | 105 | 106 | 107 | static void pda_flush(pda_t pda) { 108 | void *p1,*p2, *p3, *p4; 109 | vlist_t vl = pda->vl; 110 | int len = vl_len(vl); 111 | 112 | switch (len) { 113 | case 0: return; 114 | case 1: 115 | p1 = vl_get(pda->vl, 0); 116 | for (;;) { 117 | clflush(p1); 118 | } 119 | case 2: 120 | p1 = vl_get(pda->vl, 0); 121 | p2 = vl_get(pda->vl, 1); 122 | for (;;) { 123 | clflush(p1); 124 | clflush(p2); 125 | } 126 | case 3: 127 | p1 = vl_get(pda->vl, 0); 128 | p2 = vl_get(pda->vl, 1); 129 | p3 = vl_get(pda->vl, 1); 130 | for (;;) { 131 | clflush(p1); 132 | clflush(p2); 133 | clflush(p3); 134 | } 135 | case 4: 136 | p1 = vl_get(pda->vl, 0); 137 | p2 = vl_get(pda->vl, 1); 138 | p3 = vl_get(pda->vl, 1); 139 | p4 = vl_get(pda->vl, 1); 140 | for (;;) { 141 | clflush(p1); 142 | clflush(p2); 143 | clflush(p3); 144 | clflush(p4); 145 | } 146 | default: 147 | vl = pda->vl; 148 | for (;;) { 149 | for (int i = 0; i < len; i++) 150 | clflush(vl_get(vl, i)); 151 | } 152 | } 153 | } 154 | 155 | static void setautodeath() { 156 | #ifdef HAVE_SYS_PRCTL_H 157 | prctl(PR_SET_PDEATHSIG, SIGHUP); 158 | #endif 159 | } 160 | 161 | void pda_activate(pda_t pda) { 162 | if (pda->active) { 163 | if (!pda->modified) 164 | return; 165 | pda_deactivate(pda); 166 | } 167 | 168 | if (vl_len(pda->vl) == 0) 169 | return; 170 | 171 | pda->child = fork(); 172 | switch (pda->child) { 173 | case -1: 174 | return; 175 | case 0: 176 | setautodeath(); 177 | pda_flush(pda); 178 | // unreached 179 | default: 180 | pda->active = 1; 181 | pda->modified = 0; 182 | return; 183 | } 184 | } 185 | 186 | void pda_deactivate(pda_t pda) { 187 | if (!pda->active) 188 | return; 189 | if (pda->child > 0) { 190 | kill(pda->child, SIGKILL); 191 | wait4(pda->child, NULL, 0, NULL); 192 | } 193 | pda->child = -1; 194 | pda->active = 0; 195 | return; 196 | } 197 | 198 | int pda_isactive(pda_t pda) { 199 | return pda->active; 200 | } 201 | 202 | 203 | -------------------------------------------------------------------------------- /src/l1i.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 CSIRO 3 | * 4 | * This file is part of Mastik. 5 | * 6 | * Mastik is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * Mastik is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with Mastik. If not, see . 18 | */ 19 | 20 | #include "config.h" 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | #include "low.h" 29 | #include "l1i.h" 30 | 31 | #define L1I_ASSOCIATIVITY 8 32 | #define L1I_CACHELINE 64 33 | 34 | #define JMP_OFFSET (PAGE_SIZE - 5) 35 | #define JMP_OPCODE 0xE9 36 | #define RET_OPCODE 0xC3 37 | 38 | #define SET(page, set) (((uint8_t *)l1->memory) + PAGE_SIZE * (page) + L1I_CACHELINE * (set)) 39 | 40 | 41 | struct l1ipp{ 42 | void *memory; 43 | uint8_t monitored[L1I_SETS]; 44 | void *sets[L1I_SETS]; 45 | int nsets; 46 | }; 47 | 48 | 49 | 50 | l1ipp_t l1i_prepare(void) { 51 | static uint8_t jmp[] = { JMP_OPCODE, 52 | JMP_OFFSET & 0xff, 53 | (JMP_OFFSET >>8) & 0xff, 54 | (JMP_OFFSET >> 16) & 0xff, 55 | (JMP_OFFSET >> 24) & 0xff}; 56 | 57 | 58 | l1ipp_t l1 = (l1ipp_t)malloc(sizeof(struct l1ipp)); 59 | l1->memory = mmap(0, PAGE_SIZE * L1I_ASSOCIATIVITY, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANON, -1, 0); 60 | for (int i = 0; i < L1I_SETS; i++) { 61 | for (int j = 0; j < L1I_ASSOCIATIVITY - 1; j++) { 62 | uint8_t *p = SET(j, i); 63 | *p++ = JMP_OPCODE; 64 | *p++ = JMP_OFFSET & 0xff; 65 | *p++ = (JMP_OFFSET >>8) & 0xff; 66 | *p++ = (JMP_OFFSET >> 16) & 0xff; 67 | *p++ = (JMP_OFFSET >> 24) & 0xff; 68 | } 69 | *SET(L1I_ASSOCIATIVITY - 1, i) = RET_OPCODE; 70 | } 71 | l1i_monitorall(l1); 72 | return l1; 73 | } 74 | 75 | void l1i_release(l1ipp_t l1i) { 76 | munmap(l1i->memory, PAGE_SIZE * L1I_ASSOCIATIVITY); 77 | bzero(l1i, sizeof(struct l1ipp)); 78 | free(l1i); 79 | } 80 | 81 | int l1i_monitor(l1ipp_t l1i, int line) { 82 | for (int i = 0; i < l1i->nsets; i++) 83 | if (l1i->monitored[i] == line) 84 | return 0; 85 | l1i->monitored[l1i->nsets++] = line; 86 | return 1; 87 | } 88 | 89 | int l1i_unmonitor(l1ipp_t l1i, int line) { 90 | for (int i = 0; i < l1i->nsets; i++) 91 | if (l1i->monitored[i] == line) { 92 | l1i->monitored[i] = l1i->monitored[l1i->nsets--]; 93 | return 1; 94 | } 95 | return 0; 96 | } 97 | 98 | void l1i_monitorall(l1ipp_t l1i) { 99 | for (int i = 0; i < L1I_SETS; i++) 100 | l1i->monitored[i] = i; 101 | l1i->nsets = L1I_SETS; 102 | l1i_randomise(l1i); 103 | } 104 | 105 | void l1i_unmonitorall(l1ipp_t l1i) { 106 | l1i->nsets = 0; 107 | } 108 | 109 | 110 | int l1i_getmonitoredset(l1ipp_t l1i, int *lines, int nlines) { 111 | if (lines != NULL) { 112 | if (nlines > l1i->nsets) 113 | nlines = l1i->nsets; 114 | for (int i = 0; i < nlines; i++) 115 | lines[i] = l1i->monitored[i]; 116 | } 117 | return l1i->nsets; 118 | } 119 | 120 | 121 | int l1i_nsets(l1ipp_t l1i) { 122 | return l1i->nsets; 123 | } 124 | 125 | void l1i_randomise(l1ipp_t l1) { 126 | char *mem = (char *)l1->memory; 127 | for (int i = 0; i < l1->nsets; i++) { 128 | int p = random() % (l1->nsets - i) + i; 129 | uint8_t t = l1->monitored[p]; 130 | l1->monitored[p] = l1->monitored[i]; 131 | l1->monitored[i] = t; 132 | } 133 | } 134 | 135 | typedef void (*fptr)(void); 136 | void l1i_probe(l1ipp_t l1, uint16_t *results) { 137 | for (int i = 0; i < l1->nsets; i++) { 138 | uint32_t start = rdtscp(); 139 | // Using assembly because I am not sure I can trust the compiler 140 | //asm volatile ("callq %0": : "r" (SET(0, l1->monitored[i])):); 141 | (*((fptr)SET(0, l1->monitored[i])))(); 142 | uint32_t res = rdtscp() - start; 143 | results[i] = res > UINT16_MAX ? UINT16_MAX : res; 144 | } 145 | } 146 | 147 | 148 | int l1i_repeatedprobe(l1ipp_t l1, int nrecords, uint16_t *results, int slot) { 149 | assert(l1 != NULL); 150 | assert(results != NULL); 151 | 152 | if (nrecords == 0) 153 | return 0; 154 | 155 | int len = l1->nsets; 156 | 157 | for (int i = 0; i < nrecords; /* Increment inside */) { 158 | l1i_probe(l1, results); 159 | results += len; 160 | i++; 161 | } 162 | return nrecords; 163 | } 164 | 165 | -------------------------------------------------------------------------------- /src/symbol_mach.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | #ifndef __APPLE__ 14 | #error No use compiling a Mac OS X file here 15 | #endif 16 | 17 | 18 | static void *map_file(const char *name, off_t *size) { 19 | struct stat sb; 20 | int fd = open(name, O_RDONLY); 21 | if (fd < 0) 22 | return MAP_FAILED; 23 | if (fstat(fd, &sb) < 0) { 24 | close(fd); 25 | return MAP_FAILED; 26 | } 27 | 28 | *size = sb.st_size; 29 | void *rv = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); 30 | close(fd); 31 | return rv; 32 | } 33 | 34 | static void *handle_fat(void *adrs) { 35 | if (OSSwapBigToHostInt32(*(uint32_t *)adrs) != FAT_MAGIC) 36 | return adrs; 37 | 38 | struct host_basic_info basic_info; 39 | unsigned int count = sizeof(basic_info)/sizeof(int); 40 | if (host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)(&basic_info), &count) != KERN_SUCCESS) 41 | return 0; 42 | 43 | struct fat_header *fh = (struct fat_header *)adrs; 44 | uint32_t narch = OSSwapBigToHostInt32(fh->nfat_arch); 45 | 46 | struct fat_arch *archs = (struct fat_arch *)(fh + 1); 47 | int index = -1; 48 | for (int i = 0; i < narch; i++) { 49 | uint32_t fatcputype = OSSwapBigToHostInt32(archs[i].cputype); 50 | 51 | // We choose the 64-bit ABI if exists. 52 | if (fatcputype == (basic_info.cpu_type | CPU_ARCH_ABI64)) 53 | index = i; 54 | if (fatcputype == basic_info.cpu_type && index != -1) 55 | index = i; 56 | } 57 | 58 | if (index == -1) 59 | return NULL; 60 | return (void *)((uintptr_t)adrs + OSSwapBigToHostInt32(archs[index].offset)); 61 | } 62 | 63 | 64 | static uint64_t addrstooffset64(void *base, uint64_t vaddr) { 65 | struct mach_header_64 *mh = (struct mach_header_64 *)base; 66 | struct load_command *lc = (struct load_command *)(mh + 1); 67 | int count = mh->ncmds; 68 | 69 | while (--count) { 70 | switch (lc->cmd) { 71 | case LC_SEGMENT: // Can this even happen? 72 | break; 73 | case LC_SEGMENT_64: 74 | { 75 | struct segment_command_64 *sc = (struct segment_command_64 *)lc; 76 | if (vaddr >= sc->vmaddr && (vaddr - sc->vmaddr) < sc->filesize) 77 | return vaddr - sc->vmaddr + sc->fileoff; 78 | break; 79 | } 80 | } 81 | lc = (struct load_command *)((uintptr_t)lc + lc->cmdsize); 82 | } 83 | 84 | return ~0ULL; 85 | } 86 | 87 | static uint64_t getsymoffset64(void *base, const char *name) { 88 | struct mach_header_64 *mh = (struct mach_header_64 *)base; 89 | struct load_command *lc = (struct load_command *)(mh + 1); 90 | int count = mh->ncmds; 91 | 92 | while (--count) { 93 | switch (lc->cmd) { 94 | case LC_SYMTAB: 95 | { 96 | struct symtab_command *sc = (struct symtab_command *)lc; 97 | 98 | struct nlist_64 *symbols = (struct nlist_64 *)((uintptr_t)base + sc->symoff); 99 | for (int i = 0; i < sc->nsyms; i++) { 100 | char *p = (char *)base + sc->stroff + symbols[i].n_un.n_strx; 101 | if ((symbols[i].n_type & N_TYPE) != N_SECT) 102 | continue; 103 | if (*p == '_') 104 | p++; 105 | if (strcmp(p, name)) 106 | continue; 107 | return addrstooffset64(base, symbols[i].n_value); 108 | } 109 | } 110 | break; 111 | } 112 | 113 | lc = (struct load_command *)((uintptr_t)lc + lc->cmdsize); 114 | } 115 | 116 | return ~0ULL; 117 | } 118 | 119 | uint64_t sym_loadersymboloffset(const char *file, const char *name) { 120 | uint64_t rv = ~0ULL; 121 | off_t size = 0; 122 | void *adrs = map_file(file, &size); 123 | void *base = handle_fat(adrs); 124 | if (base == NULL) 125 | goto out; 126 | switch (*(uint32_t *)base) { 127 | /* 128 | case MH_MAGIC: 129 | rv = getsymoffset32(base, name); 130 | break; 131 | */ 132 | case MH_MAGIC_64: 133 | rv = getsymoffset64(base, name); 134 | break; 135 | } 136 | if (rv != ~0ULL) 137 | rv += (uintptr_t)base - (uintptr_t)adrs; 138 | out: 139 | if (adrs != MAP_FAILED) 140 | munmap(adrs, size); 141 | return rv; 142 | } 143 | 144 | 145 | uint64_t sym_addresstooffset(const char *file, uint64_t address) { 146 | uint64_t rv = ~0ULL; 147 | off_t size = 0; 148 | void *adrs = map_file(file, &size); 149 | 150 | // Later we might want to support fat files here... 151 | 152 | switch (*(uint32_t *)adrs) { 153 | /* 154 | case MH_MAGIC: 155 | rv = addrstooffset32(adrs, address); 156 | break; 157 | */ 158 | case MH_MAGIC_64: 159 | rv = addrstooffset64(adrs, address); 160 | break; 161 | } 162 | munmap(adrs, size); 163 | return rv; 164 | } 165 | 166 | 167 | uint64_t sym_debuglineoffset(const char *file, const char *src, int lineno) { 168 | return ~0ULL; 169 | } 170 | 171 | 172 | 173 | 174 | 175 | -------------------------------------------------------------------------------- /src/l1.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 CSIRO 3 | * 4 | * This file is part of Mastik. 5 | * 6 | * Mastik is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * Mastik is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with Mastik. If not, see . 18 | */ 19 | 20 | #include "config.h" 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | #include "low.h" 29 | #include "l1.h" 30 | 31 | #define L1_ASSOCIATIVITY 8 32 | #define L1_CACHELINE 64 33 | #define L1_STRIDE (L1_CACHELINE * L1_SETS) 34 | 35 | 36 | #define PTR(set, way, ptr) (void *)(((uintptr_t)l1->memory) + ((set) * L1_CACHELINE) + ((way) * L1_STRIDE) + ((ptr)*sizeof(void *))) 37 | #define LNEXT(p) (*(void **)(p)) 38 | 39 | 40 | 41 | 42 | 43 | struct l1pp{ 44 | void *memory; 45 | void *fwdlist; 46 | void *bkwlist; 47 | uint8_t monitored[L1_SETS]; 48 | int nsets; 49 | }; 50 | 51 | 52 | static void rebuild(l1pp_t l1) { 53 | if (l1->nsets == 0) { 54 | l1->fwdlist = l1->bkwlist = NULL; 55 | return; 56 | } 57 | for (int i = 0; i < l1->nsets - 1; i++) { 58 | LNEXT(PTR(l1->monitored[i], L1_ASSOCIATIVITY - 1, 0)) = PTR(l1->monitored[i+1], 0, 0); 59 | LNEXT(PTR(l1->monitored[i], 0, 1)) = PTR(l1->monitored[i+1], L1_ASSOCIATIVITY - 1, 1); 60 | } 61 | l1->fwdlist = LNEXT(PTR(l1->monitored[l1->nsets - 1], L1_ASSOCIATIVITY - 1, 0)) = PTR(l1->monitored[0], 0, 0); 62 | l1->bkwlist = LNEXT(PTR(l1->monitored[l1->nsets - 1], 0, 1)) = PTR(l1->monitored[0], L1_ASSOCIATIVITY - 1, 1); 63 | } 64 | 65 | 66 | 67 | static void probelist(void *p, int segments, int seglen, uint16_t *results) { 68 | while (segments--) { 69 | uint32_t s = rdtscp(); 70 | for (int i = seglen; i--; ) { 71 | asm volatile (""::"r" (p):); 72 | p = LNEXT(p); 73 | } 74 | uint32_t res = rdtscp() - s; 75 | *results = res > UINT16_MAX ? UINT16_MAX : res; 76 | results++; 77 | } 78 | } 79 | 80 | l1pp_t l1_prepare(void) { 81 | l1pp_t l1 = (l1pp_t)malloc(sizeof(struct l1pp)); 82 | l1->memory = mmap(0, PAGE_SIZE * L1_ASSOCIATIVITY, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0); 83 | l1->fwdlist = NULL; 84 | l1->bkwlist = NULL; 85 | for (int set = 0; set < L1_SETS; set++) { 86 | for (int way = 0; way < L1_ASSOCIATIVITY - 1; way++) { 87 | LNEXT(PTR(set, way, 0)) = PTR(set, way+1, 0); 88 | LNEXT(PTR(set, way+1, 1)) = PTR(set, way, 1); 89 | } 90 | } 91 | l1_monitorall(l1); 92 | return l1; 93 | } 94 | 95 | void l1_release(l1pp_t l1) { 96 | munmap(l1->memory, PAGE_SIZE * L1_ASSOCIATIVITY); 97 | bzero(l1, sizeof(struct l1pp)); 98 | free(l1); 99 | } 100 | 101 | 102 | int l1_monitor(l1pp_t l1, int line) { 103 | for (int i = 0; i < l1->nsets; i++) 104 | if (l1->monitored[i] == line) 105 | return 0; 106 | l1->monitored[l1->nsets++] = line; 107 | rebuild(l1); 108 | return 1; 109 | } 110 | 111 | int l1_unmonitor(l1pp_t l1, int line) { 112 | for (int i = 0; i < l1->nsets; i++) 113 | if (l1->monitored[i] == line) { 114 | l1->monitored[i] = l1->monitored[l1->nsets--]; 115 | rebuild(l1); 116 | return 1; 117 | } 118 | return 0; 119 | } 120 | 121 | void l1_monitorall(l1pp_t l1) { 122 | for (int i = 0; i < L1_SETS; i++) 123 | l1->monitored[i] = i; 124 | l1->nsets = L1_SETS; 125 | l1_randomise(l1); 126 | } 127 | 128 | void l1_unmonitorall(l1pp_t l1) { 129 | l1->nsets = 0; 130 | rebuild(l1); 131 | } 132 | 133 | 134 | int l1_getmonitoredset(l1pp_t l1, int *lines, int nlines) { 135 | if (lines != NULL) { 136 | if (nlines > l1->nsets) 137 | nlines = l1->nsets; 138 | for (int i = 0; i < nlines; i++) 139 | lines[i] = l1->monitored[i]; 140 | } 141 | return l1->nsets; 142 | } 143 | 144 | 145 | int l1_nsets(l1pp_t l1) { 146 | return l1->nsets; 147 | } 148 | 149 | 150 | void l1_randomise(l1pp_t l1) { 151 | for (int i = 0; i < l1->nsets; i++) { 152 | int p = random() % (l1->nsets - i) + i; 153 | uint8_t t = l1->monitored[p]; 154 | l1->monitored[p] = l1->monitored[i]; 155 | l1->monitored[i] = t; 156 | } 157 | rebuild(l1); 158 | } 159 | 160 | void l1_probe(l1pp_t l1, uint16_t *results) { 161 | probelist(l1->fwdlist, l1->nsets, L1_ASSOCIATIVITY, results); 162 | } 163 | 164 | void l1_bprobe(l1pp_t l1, uint16_t *results) { 165 | probelist(l1->bkwlist, l1->nsets, L1_ASSOCIATIVITY, results); 166 | } 167 | 168 | 169 | 170 | int l1_repeatedprobe(l1pp_t l1, int nrecords, uint16_t *results, int slot) { 171 | assert(l1 != NULL); 172 | assert(results != NULL); 173 | 174 | if (nrecords == 0) 175 | return 0; 176 | 177 | int len = l1->nsets; 178 | 179 | for (int i = 0; i < nrecords; /* Increment inside */) { 180 | l1_probe(l1, results); 181 | results += len; 182 | i++; 183 | l1_bprobe(l1, results); 184 | results += len; 185 | i++; 186 | } 187 | return nrecords; 188 | } 189 | 190 | -------------------------------------------------------------------------------- /src/ff.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 CSIRO 3 | * 4 | * This file is part of Mastik. 5 | * 6 | * Mastik is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * Mastik is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with Mastik. If not, see . 18 | */ 19 | 20 | #ifndef __FF_H__ 21 | #define __FF_H__ 1 22 | 23 | 24 | /* 25 | * Performs the Flush+Flush [1] attack. 26 | * The attack exploits the timing variations in the clflush, which takes 27 | * a longer time to ensure the flushed memory line is evicted from 28 | * all cache levels if the line is cached. 29 | * 30 | * The attack can only target memory addresses that are shared between 31 | * the spy and the victim. Typically, such sharing results from mmaping 32 | * a binary the victim use. For cross-VM attacks, the hypervisor 33 | * must support page deduplication. The most common attack target is 34 | * shared code. 35 | * 36 | * The spatial resolution of the attack is one cache line (64 bytes). The 37 | * attack is likely to be unaffected by cache optimisations such as 38 | * the Spatial Prefetcher that pair consecutive cache lines and any streamers 39 | * that try to identify access patterns to the cache. 40 | * 41 | * False positives are relatively common. 42 | * 43 | * The attack is sensitive to migration between processor cores. clflush timing 44 | * depends on both the core the instruction executes on and the cache slice 45 | * the data is in. No single threshold is useful for distinguishin results on 46 | * all combinations of cores and slices. It is recommended to control the spy 47 | * placement using taskset(1) or the supplied setaffinity. (See util.h.) 48 | * 49 | * [1] D. Gruss, C. Maurice, K. Wagner, and S. Mangard, "Flush+Flush: 50 | * A Fast and Stealthy Cahce Attack", DIMVA 2016 51 | */ 52 | 53 | 54 | /* 55 | * An opaque reference to an attack instance 56 | */ 57 | typedef struct ff *ff_t; 58 | 59 | 60 | 61 | /* 62 | * Creates and initialises an attack instance 63 | */ 64 | ff_t ff_prepare(); 65 | 66 | 67 | /* 68 | * Disposes of all resources acquired for an attack instance 69 | */ 70 | void ff_release(ff_t ff); 71 | 72 | 73 | /* 74 | * Adds a memory address to the list of addresses monitored by 75 | * an attack instance 76 | */ 77 | int ff_monitor(ff_t ff, void *adrs); 78 | 79 | 80 | /* 81 | * Removes a memory address from the list of addresses monitored 82 | * by an attack instance. 83 | */ 84 | int ff_unmonitor(ff_t ff, void *adrs); 85 | 86 | 87 | /* 88 | * Retrieve the list of addresses monitored by an attack instance. Parameters: 89 | * ff - Attack instance 90 | * adrss - A buffer for storing the list. Set to NULL to retrieve the number 91 | * of monitored addresses. 92 | * nlines - Buffer size 93 | * Returns the number of addresses monitored. 94 | */ 95 | int ff_getmonitoredset(ff_t ff, void **adrss, int nlines); 96 | 97 | 98 | /* 99 | * Calculate the thresholds between flushing a cached and a non-cached line. 100 | * ff_trace (see below) usually keeps track of the thresholds, but 101 | * explicit calculation is required when the spy migrates between cores or 102 | * to avoid delays when calling ff_trace. 103 | */ 104 | void ff_setthresholds(ff_t ff); 105 | 106 | /* 107 | * Not currently implemented 108 | */ 109 | void ff_randomise(ff_t ff); 110 | 111 | 112 | 113 | 114 | 115 | /* 116 | * Performs a single probe round on each of the monitored addresses. 117 | * results points to a buffer for storing the timing to clflush each 118 | * each of the monitored addresses. 119 | * Addresses are stored in the order returned by ff_getmonitoredset. 120 | * This is guaranteed to be the order addresses are added using ff_monitor 121 | * as long as ff_unmonitor is not used. 122 | */ 123 | void ff_probe(ff_t ff, uint16_t *results); 124 | 125 | 126 | /* 127 | * Performs repeated calls to ff_probe. Prameters are: 128 | * ff - The ff descriptor 129 | * max_records - The number of records to collect 130 | * results - An array for results of size ff_len(ff) * max_records 131 | * slot - The length of a time-slot in cycles. Use 0 to have flexible slot size 132 | * threshold - True if should calculate activity thresholds and use them. 133 | * max_idle - Stop capture when read more than max_idle idle records. 134 | * 135 | * If threshold is set, capture record starts when a probe is active, i.e. 136 | * above its threshold. Otherwise capture starts immediately. 137 | * Capture stops when either max_idle records with no active address or when max_records 138 | * are captured. max_idle is ignored if =0. 139 | * 140 | * Output is the reload times for each of the monitored addresses in each time slot. 141 | * Missed slots are indicted using time 0. 142 | * The return value is the number of records captured. 143 | */ 144 | int ff_trace(ff_t ff, int max_records, uint16_t *results, int slot, int threshold, int max_idle); 145 | 146 | 147 | /* 148 | * Repeat ff_probe max_record times. Equivalent to: 149 | * ff_trace(ff, max_records, results, slot, 0, max_records); 150 | */ 151 | int ff_repeatedprobe(ff_t ff, int max_records, uint16_t *results, int slot); 152 | 153 | 154 | #endif // __FF_H__ 155 | 156 | -------------------------------------------------------------------------------- /src/ff.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 CSIRO 3 | * 4 | * This file is part of Mastik. 5 | * 6 | * Mastik is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * Mastik is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with Mastik. If not, see . 18 | */ 19 | 20 | #include "config.h" 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include "low.h" 27 | #include "vlist.h" 28 | #include "timestats.h" 29 | #include "ff.h" 30 | #include "util.h" 31 | 32 | 33 | #define DEFAULT_THRESHOLD_CAPACITY 16 34 | 35 | #define THRESHOLD_SAMPLES 1000 36 | 37 | struct ff { 38 | vlist_t vl; 39 | int modified; 40 | uint16_t *thresholds; 41 | int thresholdcap; 42 | }; 43 | 44 | 45 | static inline void ensurecapacity(ff_t ff) { 46 | if (ff->thresholdcap >= vl_len(ff->vl)) 47 | return; 48 | int newcap = ff->thresholdcap + vl_len(ff->vl); 49 | ff->thresholds = realloc(ff->thresholds, newcap * sizeof(uint16_t)); 50 | bzero(ff->thresholds + ff->thresholdcap, vl_len(ff->vl) * sizeof(uint16_t)); 51 | } 52 | 53 | ff_t ff_prepare() { 54 | ff_t rv = malloc(sizeof(struct ff)); 55 | rv->vl = vl_new(); 56 | rv->modified = 0; 57 | rv->thresholds = calloc(DEFAULT_THRESHOLD_CAPACITY, sizeof(uint16_t)); 58 | rv->thresholdcap = DEFAULT_THRESHOLD_CAPACITY; 59 | return rv; 60 | } 61 | 62 | void ff_release(ff_t ff) { 63 | vl_free(ff->vl); 64 | ff->vl = NULL; 65 | free(ff->thresholds); 66 | ff->thresholds = NULL; 67 | free(ff); 68 | } 69 | 70 | 71 | int ff_monitor(ff_t ff, void *adrs) { 72 | assert(ff != NULL); 73 | assert(adrs != NULL); 74 | if (vl_find(ff->vl, adrs) >= 0) 75 | return 0; 76 | int ind = vl_push(ff->vl, adrs); 77 | if (!ff->modified) { 78 | ensurecapacity(ff); 79 | ff->thresholds[ind] = 0; 80 | } 81 | return 1; 82 | } 83 | 84 | 85 | int ff_unmonitor(ff_t ff, void *adrs) { 86 | assert(ff != NULL); 87 | assert(adrs != NULL); 88 | int i = vl_find(ff->vl, adrs); 89 | if (i < 0) 90 | return 0; 91 | vl_del(ff->vl, i); 92 | ff->modified = 1; 93 | return 1; 94 | } 95 | 96 | 97 | int ff_getmonitoredset(ff_t ff, void **adrss, int nlines) { 98 | assert(ff != NULL); 99 | 100 | if (adrss != NULL) { 101 | int l = vl_len(ff->vl); 102 | if (l > nlines) 103 | l = nlines; 104 | for (int i = 0; i < l; i++) 105 | adrss[i] = vl_get(ff->vl, i); 106 | } 107 | return vl_len(ff->vl); 108 | } 109 | 110 | void ff_randomise(ff_t ff) { 111 | assert(ff != NULL); 112 | assert(0); 113 | } 114 | 115 | static uint16_t inline probeaddr(void *addr) { 116 | uint32_t start = rdtscp(); 117 | mfence(); 118 | clflush(addr); 119 | mfence(); 120 | uint32_t res = rdtscp() - start; 121 | return res > UINT16_MAX ? UINT16_MAX : res; 122 | } 123 | 124 | void ff_probe(ff_t fr, uint16_t *results) { 125 | assert(fr != NULL); 126 | assert(results != NULL); 127 | int l = vl_len(fr->vl); 128 | for (int i = 0; i < l; i++) 129 | results[i] = probeaddr(vl_get(fr->vl, i)); 130 | } 131 | 132 | static void setthresholds(ff_t ff, int force) { 133 | ensurecapacity(ff); 134 | int l = vl_len(ff->vl); 135 | if (ff->modified || force) 136 | bzero(ff->thresholds, l * sizeof(uint16_t)); 137 | ts_t samples = NULL; 138 | for (int i = 0; i < l; i++) { 139 | if (ff->thresholds[i] != 0) 140 | continue; 141 | if (samples == NULL) 142 | samples = ts_alloc(); 143 | ts_clear(samples); 144 | void *addr = vl_get(ff->vl, i); 145 | for (int j = 0; j < THRESHOLD_SAMPLES; j++) { 146 | ts_add(samples, probeaddr(addr)); 147 | delayloop(1000); 148 | } 149 | ff->thresholds[i] = ts_percentile(samples, 99) + 6; 150 | } 151 | if (samples != NULL) 152 | ts_free(samples); 153 | ff->modified = 0; 154 | } 155 | 156 | void ff_setthresholds(ff_t ff) { 157 | setthresholds(ff, 1); 158 | } 159 | 160 | inline int is_active(uint16_t *results, int len, uint16_t *thresholds) { 161 | if (thresholds == NULL) 162 | return 1; 163 | for (int i = 0; i < len; i++) 164 | if (results[i] < thresholds[i] * 2 && results[i] > thresholds[i]) 165 | return 1; 166 | return 0; 167 | } 168 | 169 | int ff_repeatedprobe(ff_t ff, int max_records, uint16_t *results, int slot) { 170 | return ff_trace(ff, max_records, results, slot, 0, max_records); 171 | } 172 | 173 | 174 | int ff_trace(ff_t ff, int max_records, uint16_t *results, int slot, int threshold, int max_idle) { 175 | assert(ff != NULL); 176 | assert(results != NULL); 177 | 178 | if (max_records == 0) 179 | return 0; 180 | if (max_idle == 0) 181 | max_idle = max_records; 182 | 183 | uint16_t *thresholds = NULL; 184 | if (threshold) { 185 | setthresholds(ff, 0); 186 | thresholds = ff->thresholds; 187 | } 188 | 189 | int len = vl_len(ff->vl); 190 | 191 | // Wait to hit threshold 192 | uint64_t prev_time = rdtscp64(); 193 | ff_probe(ff, results); 194 | do { 195 | if (slot > 0) { 196 | do { 197 | prev_time += slot; 198 | } while (slotwait(prev_time)); 199 | } 200 | ff_probe(ff, results); 201 | } while (!is_active(results, len, thresholds)); 202 | 203 | int count = 1; 204 | int idle_count = 0; 205 | int missed = 0; 206 | 207 | while (idle_count < max_idle && count < max_records) { 208 | idle_count++; 209 | results += len; 210 | count++; 211 | if (missed) { 212 | for (int i = 0; i < len; i++) 213 | results[i] = 0; 214 | } else { 215 | ff_probe(ff, results); 216 | if (is_active(results, len, thresholds)) 217 | idle_count = 0; 218 | } 219 | prev_time += slot; 220 | missed = slotwait(prev_time); 221 | } 222 | return count; 223 | } 224 | 225 | 226 | -------------------------------------------------------------------------------- /src/symbol_bfd.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 CSIRO 3 | * 4 | * This file is part of Mastik. 5 | * 6 | * Mastik is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * Mastik is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with Mastik. If not, see . 18 | */ 19 | 20 | 21 | #include "config.h" 22 | #ifndef HAVE_BFD_H 23 | #error Must have bfd to compile this 24 | #endif 25 | 26 | #define _GNU_SOURCE 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | #if defined(HAVE_LIBDWARF_H) 36 | #include 37 | #elif defined(HAVE_LIBDWARF_LIBDWARF_H) 38 | #include 39 | #endif 40 | 41 | #include "symbol.h" 42 | 43 | static void initialise() { 44 | static int init = 0; 45 | if (init) 46 | return; 47 | 48 | bfd_init(); 49 | init = 1; 50 | } 51 | 52 | 53 | 54 | 55 | 56 | 57 | uint64_t sym_loadersymboloffset(const char *file, const char *name) { 58 | initialise(); 59 | 60 | bfd *abfd = NULL; 61 | asymbol **symbol_table = NULL; 62 | uint64_t rv = ~0ULL; 63 | 64 | abfd = bfd_openr(file, "default"); 65 | if (abfd == NULL) 66 | goto out; 67 | 68 | 69 | 70 | if (!bfd_check_format(abfd, bfd_object)) { 71 | if (bfd_get_error () != bfd_error_file_ambiguously_recognized) 72 | goto out; 73 | } 74 | 75 | long storage_needed = bfd_get_symtab_upper_bound (abfd); 76 | if (storage_needed <= 0) 77 | goto out; 78 | 79 | symbol_table = (asymbol **) malloc (storage_needed); 80 | 81 | long number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table); 82 | if (number_of_symbols < 0) 83 | goto out; 84 | 85 | for (long i = 0; i < number_of_symbols; i++) { 86 | if (!strcmp(name, symbol_table[i]->name)) { 87 | struct bfd_section *section = symbol_table[i]->section; 88 | rv = symbol_table[i]->value + section->filepos; 89 | goto out; 90 | } 91 | } 92 | 93 | 94 | out: 95 | if (symbol_table != NULL) 96 | free(symbol_table); 97 | if (abfd != NULL) 98 | bfd_close(abfd); 99 | return rv; 100 | } 101 | 102 | uint64_t sym_addresstooffset(const char *file, uint64_t address) { 103 | initialise(); 104 | 105 | bfd *abfd = NULL; 106 | uint64_t rv = ~0ULL; 107 | 108 | abfd = bfd_openr(file, "default"); 109 | if (abfd == NULL) 110 | goto out; 111 | 112 | if (!bfd_check_format(abfd, bfd_object)) { 113 | if (bfd_get_error () != bfd_error_file_ambiguously_recognized) 114 | goto out; 115 | } 116 | 117 | for (asection *s = abfd->sections; s; s = s->next) { 118 | if (bfd_get_section_flags (abfd, s) & (SEC_LOAD)) { 119 | uint64_t vma = bfd_section_vma(abfd, s); 120 | uint64_t size = bfd_section_size(abfd, s); 121 | if (address >= vma && address - vma < size) { 122 | rv = address - vma + s->filepos; 123 | break; 124 | } 125 | } 126 | } 127 | 128 | out: 129 | if (abfd != NULL) 130 | bfd_close(abfd); 131 | return rv; 132 | } 133 | 134 | 135 | 136 | #ifdef HAVE_DWARF 137 | static void dwarf_handler(Dwarf_Error error, Dwarf_Ptr errarg) { 138 | Dwarf_Debug dbg = *(Dwarf_Debug *)errarg; 139 | dwarf_dealloc(dbg, error, DW_DLA_ERROR); 140 | } 141 | #endif 142 | 143 | uint64_t sym_debuglineoffset(const char *file, const char *src, int lineno) { 144 | #ifdef HAVE_DWARF 145 | Dwarf_Debug dbg = NULL; 146 | int res = DW_DLV_ERROR; 147 | Dwarf_Error error = NULL; 148 | Dwarf_Unsigned next_cu_header; 149 | Dwarf_Die die = NULL; 150 | char *name = NULL; 151 | Dwarf_Line *linebuf = NULL; 152 | Dwarf_Signed linecount; 153 | Dwarf_Unsigned line; 154 | Dwarf_Addr lineaddr; 155 | int fd = -1; 156 | uint64_t rv = -1; 157 | 158 | fd = open(file, O_RDONLY); 159 | if(fd < 0) 160 | goto out; 161 | 162 | res = dwarf_init(fd, DW_DLC_READ, dwarf_handler, (Dwarf_Ptr)&dbg, &dbg, &error); 163 | if (res != DW_DLV_OK) { 164 | if (error) 165 | free(error); 166 | goto out; 167 | } 168 | 169 | for (;;) { 170 | res = dwarf_next_cu_header(dbg, NULL, NULL, NULL, NULL, &next_cu_header, NULL); 171 | if (res != DW_DLV_OK) 172 | goto out; 173 | 174 | // The first sibling is a DW_TAG_compile_unit 175 | res = dwarf_siblingof(dbg, NULL, &die, NULL); 176 | if (res != DW_DLV_OK) 177 | goto out; 178 | 179 | res = dwarf_diename(die, &name, NULL); 180 | if (res != DW_DLV_OK) 181 | goto out; 182 | 183 | int found = !strcmp(name, src); 184 | 185 | dwarf_dealloc(dbg, name, DW_DLA_STRING); 186 | name = NULL; 187 | if (found) 188 | break; 189 | 190 | dwarf_dealloc(dbg, die, DW_DLA_DIE); 191 | die = NULL; 192 | } 193 | 194 | res = dwarf_srclines(die, &linebuf, &linecount, &error); 195 | if(res != DW_DLV_OK) 196 | goto out; 197 | int nextline = 0; 198 | uint64_t nextaddr = ~0ULL; 199 | for (int i = 0; i < linecount; i++) { 200 | dwarf_lineno(linebuf[i], &line, &error); 201 | dwarf_lineaddr(linebuf[i], &lineaddr, &error); 202 | if (rv == ~0ULL) { 203 | if (lineno == line) 204 | rv = lineaddr; 205 | else if (line > lineno) { 206 | if (nextline == 0 || nextline > line) { 207 | nextline = line; 208 | nextaddr = lineaddr; 209 | } 210 | } 211 | } 212 | dwarf_dealloc(dbg, linebuf[i], DW_DLA_LINE); 213 | } 214 | 215 | dwarf_dealloc(dbg, linebuf, DW_DLA_LIST); 216 | 217 | if (rv == ~0ULL) 218 | rv = nextaddr; 219 | if (rv != ~0ULL) 220 | rv = sym_addresstooffset(file, rv); 221 | 222 | 223 | out: 224 | if (name != NULL) 225 | dwarf_dealloc(dbg, name, DW_DLA_STRING); 226 | if (die != NULL) 227 | dwarf_dealloc(dbg, die, DW_DLA_DIE); 228 | if (dbg != NULL) 229 | dwarf_finish(dbg, NULL); 230 | if (fd >=0) 231 | close(fd); 232 | return rv; 233 | #else 234 | return ~0ULL; 235 | #endif 236 | } 237 | 238 | 239 | -------------------------------------------------------------------------------- /demo/FR-trace.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 CSIRO 3 | * 4 | * This file is part of Mastik. 5 | * 6 | * Mastik is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * Mastik is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with Mastik. If not, see . 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | #define SAMPLES 100000 33 | #define SLOT 10000 34 | #define IDLE 500 35 | #define THRESHOLD 100 36 | 37 | #define MAX_MONITORED 100 38 | #define MAX_EVICTED 100 39 | #define MAX_PDA_TARGETS 10 40 | 41 | #define MAX_PDAS 8 42 | 43 | void usage(char *p) { 44 | fprintf(stderr, "Usage: %s [-s ] [-c ] [-h ] [-i ]\n" 45 | " [-p ] [-H] [-f ] \n" 46 | " [-m ] [-e ] [-t ] ...\n", p); 47 | exit(1); 48 | } 49 | 50 | struct map_entry { 51 | char *file; 52 | char *adrsspec; 53 | uint64_t offset; 54 | void *map_address; 55 | }; 56 | 57 | struct config { 58 | char *progname; 59 | int samples; 60 | int slot; 61 | int threshold; 62 | int idle; 63 | int pdacount; 64 | struct map_entry monitored[MAX_MONITORED]; 65 | int nmonitored; 66 | struct map_entry evicted[MAX_EVICTED]; 67 | int nevicted; 68 | struct map_entry pda_targets[MAX_PDA_TARGETS]; 69 | int npdatargets; 70 | int printheader; 71 | }; 72 | 73 | 74 | void fill_map_entry(struct map_entry *e) { 75 | if (e->file == NULL) { 76 | fprintf(stderr, "No filename\n"); 77 | exit(1); 78 | } 79 | e->offset = sym_getsymboloffset(e->file, e->adrsspec); 80 | if (e->offset == ~0ULL) { 81 | fprintf(stderr, "Cannot find %s in %s\n", e->adrsspec, e->file); 82 | exit(1); 83 | } 84 | 85 | e->map_address = map_offset(e->file, e->offset); 86 | if (e->map_address == NULL) { 87 | perror(e->file); 88 | exit(1); 89 | } 90 | } 91 | 92 | 93 | 94 | static void printmapentries(struct map_entry *entries, int count, char *name) { 95 | for (int i = 0; i < count; i++) { 96 | printf("# %s%d=%s %s 0x%llx\n", name, i, entries[i].file, entries[i].adrsspec, entries[i].offset); 97 | } 98 | } 99 | 100 | static void printuname() { 101 | struct utsname name; 102 | uname(&name); 103 | printf("# sysname=%s\n", name.sysname); 104 | printf("# nodename=%s\n", name.nodename); 105 | printf("# release=%s\n", name.release); 106 | printf("# version=%s\n", name.version); 107 | printf("# machine=%s\n", name.machine); 108 | } 109 | 110 | 111 | void readargs(struct config *c, int ac, char **av) { 112 | char *file = NULL; 113 | int ch; 114 | 115 | c->samples = SAMPLES; 116 | c->slot = SLOT; 117 | c->threshold = THRESHOLD; 118 | c->idle = IDLE; 119 | c->pdacount = 0; 120 | c->npdatargets = 0; 121 | c->nmonitored = 0; 122 | c->nevicted = 0; 123 | c->progname = av[0]; 124 | c->printheader = 0; 125 | 126 | while ((ch = getopt(ac, av, "Hf:s:c:h:i:p:t:m:e:")) != -1) { 127 | switch (ch) { 128 | case 'H': 129 | c->printheader = 1; 130 | break; 131 | case 's': 132 | c->slot = atoi(optarg); 133 | break; 134 | case 'c': 135 | c->samples = atoi(optarg); 136 | break; 137 | case 'h': 138 | c->threshold = atoi(optarg); 139 | break; 140 | case 'i': 141 | c->idle = atoi(optarg); 142 | break; 143 | case 'p': 144 | c->pdacount = atoi(optarg); 145 | break; 146 | case 'f': 147 | file = optarg; 148 | break; 149 | case 't': 150 | if (c->npdatargets >= MAX_PDA_TARGETS) { 151 | fprintf(stderr, "Too many pda targets (Max %d)\n", MAX_PDA_TARGETS); 152 | exit(1); 153 | } 154 | c->pda_targets[c->npdatargets].file = file; 155 | c->pda_targets[c->npdatargets].adrsspec = optarg; 156 | fill_map_entry(&c->pda_targets[c->npdatargets]); 157 | c->npdatargets++; 158 | break; 159 | case 'm': 160 | if (c->nmonitored >= MAX_MONITORED) { 161 | fprintf(stderr, "Too many monitored locations(Max %d)\n", MAX_MONITORED); 162 | exit(1); 163 | } 164 | c->monitored[c->nmonitored].file = file; 165 | c->monitored[c->nmonitored].adrsspec = optarg; 166 | fill_map_entry(&c->monitored[c->nmonitored]); 167 | c->nmonitored++; 168 | break; 169 | case 'e': 170 | if (c->nevicted >= MAX_EVICTED) { 171 | fprintf(stderr, "Too many evicted locations(Max %d)\n", MAX_EVICTED); 172 | exit(1); 173 | } 174 | c->evicted[c->nevicted].file = file; 175 | c->evicted[c->nevicted].adrsspec = optarg; 176 | fill_map_entry(&c->evicted[c->nevicted]); 177 | c->nevicted++; 178 | break; 179 | default: usage(av[0]); 180 | } 181 | } 182 | 183 | if (c->nmonitored == 0) 184 | usage(av[0]); 185 | 186 | if (c->pdacount > MAX_PDAS) { 187 | fprintf(stderr, "Too many performance degradation attack threads. (Max %d)\n", MAX_PDAS); 188 | exit(1); 189 | } 190 | 191 | } 192 | 193 | void printheader(struct config *c) { 194 | time_t now = time(NULL); 195 | printf("# %s starting at %.24s\n", c->progname, ctime(&now)); 196 | printf("################# CONFIG #################\n"); 197 | printf("# slot=%d\n", c->slot); 198 | printf("# samples=%d\n", c->samples); 199 | printf("# threshold=%d\n", c->threshold); 200 | printf("# idle=%d\n", c->idle); 201 | printf("# pdathreads=%d\n", c->pdacount); 202 | printf("# nmonitored=%d\n", c->nmonitored); 203 | printmapentries(c->monitored, c->nmonitored, "monitor"); 204 | printf("# nevicted=%d\n", c->nevicted); 205 | printmapentries(c->evicted, c->nevicted, "evict"); 206 | printf("# npdatargets=%d\n", c->npdatargets); 207 | printmapentries(c->pda_targets, c->npdatargets, "target"); 208 | printf("############## SYSTEM INFO ###############\n"); 209 | printf("# mastik_version=%s\n", mastik_version()); 210 | printuname(); 211 | printf("################## DATA ##################\n"); 212 | } 213 | 214 | int main(int ac, char **av) { 215 | struct config c; 216 | pda_t *pdas = NULL; 217 | fr_t fr = fr_prepare(); 218 | 219 | readargs(&c, ac, av); 220 | 221 | if (c.printheader) 222 | printheader(&c); 223 | 224 | for (int i = 0; i < c.nmonitored; i++) 225 | fr_monitor(fr, c.monitored[i].map_address); 226 | 227 | for (int i = 0; i < c.nevicted; i++) 228 | fr_evict(fr, c.evicted[i].map_address); 229 | 230 | 231 | 232 | uint16_t *res = malloc(c.samples * c.nmonitored * sizeof(uint16_t)); 233 | for (int i = 0; i < c.samples * c.nmonitored ; i+= 4096/sizeof(uint16_t)) 234 | res[i] = 1; 235 | fr_probe(fr, res); 236 | 237 | if (c.pdacount > 0) { 238 | pdas = calloc(c.pdacount, sizeof(pda_t)); 239 | for (int i = 0; i < c.pdacount; i++) { 240 | pdas[i] = pda_prepare(); 241 | for (int j = 0; j < c.npdatargets; j++) 242 | pda_target(pdas[i], c.pda_targets[j].map_address); 243 | pda_activate(pdas[i]); 244 | } 245 | } 246 | 247 | int l = fr_trace(fr, c.samples, res, c.slot, c.threshold, c.idle); 248 | 249 | if (c.pdacount > 0) { 250 | for (int i = 0; i < c.pdacount; i++) 251 | pda_release(pdas[i]); 252 | } 253 | 254 | for (int i = 0; i < l; i++) { 255 | for (int j = 0; j < c.nmonitored; j++) 256 | printf("%d ", res[i * c.nmonitored + j]); 257 | putchar('\n'); 258 | } 259 | 260 | free(res); 261 | fr_release(fr); 262 | } 263 | -------------------------------------------------------------------------------- /src/fr.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 CSIRO 3 | * 4 | * This file is part of Mastik. 5 | * 6 | * Mastik is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * Mastik is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with Mastik. If not, see . 18 | */ 19 | 20 | #include "config.h" 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include "low.h" 26 | #include "vlist.h" 27 | #include "timestats.h" 28 | #include "fr.h" 29 | 30 | struct fr { 31 | vlist_t vl; 32 | vlist_t evict; 33 | }; 34 | 35 | 36 | 37 | fr_t fr_prepare() { 38 | fr_t rv = malloc(sizeof(struct fr)); 39 | rv->vl = vl_new(); 40 | rv->evict = vl_new(); 41 | return rv; 42 | } 43 | 44 | void fr_release(fr_t fr) { 45 | vl_free(fr->vl); 46 | fr->vl = NULL; 47 | vl_free(fr->evict); 48 | fr->evict = NULL; 49 | free(fr); 50 | } 51 | 52 | 53 | int fr_monitor(fr_t fr, void *adrs) { 54 | assert(fr != NULL); 55 | assert(adrs != NULL); 56 | if (vl_find(fr->vl, adrs) >= 0) 57 | return 0; 58 | vl_push(fr->vl, adrs); 59 | return 1; 60 | } 61 | 62 | 63 | int fr_unmonitor(fr_t fr, void *adrs) { 64 | assert(fr != NULL); 65 | assert(adrs != NULL); 66 | int i = vl_find(fr->vl, adrs); 67 | if (i < 0) 68 | return 0; 69 | vl_del(fr->vl, i); 70 | return 1; 71 | } 72 | 73 | 74 | int fr_getmonitoredset(fr_t fr, void **adrss, int nlines) { 75 | assert(fr != NULL); 76 | 77 | if (adrss != NULL) { 78 | int l = vl_len(fr->vl); 79 | if (l > nlines) 80 | l = nlines; 81 | for (int i = 0; i < l; i++) 82 | adrss[i] = vl_get(fr->vl, i); 83 | } 84 | return vl_len(fr->vl); 85 | } 86 | 87 | int fr_evict(fr_t fr, void *adrs) { 88 | assert(fr != NULL); 89 | assert(adrs != NULL); 90 | if (vl_find(fr->evict, adrs) >= 0) 91 | return 0; 92 | vl_push(fr->evict, adrs); 93 | return 1; 94 | } 95 | 96 | 97 | int fr_unevict(fr_t fr, void *adrs) { 98 | assert(fr != NULL); 99 | assert(adrs != NULL); 100 | int i = vl_find(fr->evict, adrs); 101 | if (i < 0) 102 | return 0; 103 | vl_del(fr->evict, i); 104 | return 1; 105 | } 106 | 107 | 108 | int fr_getevictedset(fr_t fr, void **adrss, int nlines) { 109 | assert(fr != NULL); 110 | 111 | if (adrss != NULL) { 112 | int l = vl_len(fr->evict); 113 | if (l > nlines) 114 | l = nlines; 115 | for (int i = 0; i < l; i++) 116 | adrss[i] = vl_get(fr->evict, i); 117 | } 118 | return vl_len(fr->evict); 119 | } 120 | 121 | void fr_randomise(fr_t fr) { 122 | assert(fr != NULL); 123 | assert(0); 124 | } 125 | 126 | 127 | int fr_probethreshold() { 128 | static char dummy; 129 | 130 | ts_t ts = ts_alloc(); 131 | for (int i = 0; i < 100000; i++) { 132 | clflush(&dummy); 133 | ts_add(ts, memaccesstime(&dummy)); 134 | } 135 | int res = ts_percentile(ts, 1); 136 | ts_free(ts); 137 | return res < 100 ? res - 10 : res * 9 / 10; 138 | } 139 | 140 | 141 | 142 | 143 | 144 | 145 | void fr_probe(fr_t fr, uint16_t *results) { 146 | assert(fr != NULL); 147 | assert(results != NULL); 148 | int l = vl_len(fr->vl); 149 | for (int i = 0; i < l; i++) { 150 | void *adrs = vl_get(fr->vl, i); 151 | int res = memaccesstime(adrs); 152 | results[i] = res > UINT16_MAX ? UINT16_MAX : res; 153 | clflush(adrs); 154 | } 155 | l = vl_len(fr->evict); 156 | for (int i = 0; i < l; i++) 157 | clflush(vl_get(fr->evict, i)); 158 | } 159 | 160 | inline int is_active(uint16_t *results, int len, int threshold) { 161 | for (int i = 0; i < len; i++) 162 | if (results[i] < threshold) 163 | return 1; 164 | return 0; 165 | } 166 | 167 | #ifdef NONBLOCK_TRACE 168 | /* 169 | * Note: this version of 'fr_trace' does not wait until the function hits a thredhold 170 | */ 171 | int fr_trace(fr_t fr, int max_records, uint16_t *results, int slot, int threshold, int max_idle) { 172 | assert(fr != NULL); 173 | assert(results != NULL); 174 | 175 | if (max_records == 0) 176 | return 0; 177 | if (max_idle == 0) 178 | max_idle = max_records; 179 | 180 | int len = vl_len(fr->vl); 181 | 182 | // Initialize variables for looping 183 | int count = 1; 184 | int idle_count = 0; 185 | int missed = 0; 186 | 187 | // Record the initial time before the start 188 | uint64_t prev_time = rdtscp64(); 189 | 190 | while (idle_count < max_idle && count < max_records) { 191 | idle_count++; 192 | results += len; 193 | count++; 194 | if (missed) { 195 | for (int i = 0; i < len; i++) 196 | results[i] = 0; 197 | } else { 198 | fr_probe(fr, results); 199 | if (is_active(results, len, threshold)) 200 | idle_count = 0; 201 | } 202 | prev_time += slot; 203 | missed = slotwait(prev_time); 204 | } 205 | return count; 206 | } 207 | #else 208 | int fr_trace(fr_t fr, int max_records, uint16_t *results, int slot, int threshold, int max_idle) { 209 | assert(fr != NULL); 210 | assert(results != NULL); 211 | 212 | if (max_records == 0) 213 | return 0; 214 | if (max_idle == 0) 215 | max_idle = max_records; 216 | 217 | int len = vl_len(fr->vl); 218 | 219 | // Wait to hit threshold 220 | uint64_t prev_time = rdtscp64(); 221 | fr_probe(fr, results); 222 | do { 223 | if (slot > 0) { 224 | do { 225 | prev_time += slot; 226 | } while (slotwait(prev_time)); 227 | } 228 | fr_probe(fr, results); 229 | } while (!is_active(results, len, threshold)); 230 | 231 | int count = 1; 232 | int idle_count = 0; 233 | int missed = 0; 234 | 235 | while (idle_count < max_idle && count < max_records) { 236 | idle_count++; 237 | results += len; 238 | count++; 239 | if (missed) { 240 | for (int i = 0; i < len; i++) 241 | results[i] = 0; 242 | } else { 243 | fr_probe(fr, results); 244 | if (is_active(results, len, threshold)) 245 | idle_count = 0; 246 | } 247 | prev_time += slot; 248 | missed = slotwait(prev_time); 249 | } 250 | return count; 251 | } 252 | #endif 253 | 254 | #ifdef NONBLOCK_TSC 255 | /* 256 | * Note: this version of 'fr_trtsc' does not wait until the function hits a thredhold 257 | * + it also returns 'starttsc' which includes the time when this analysis has started 258 | */ 259 | int fr_trtsc(fr_t fr, int max_records, uint64_t *starttsc, uint16_t *results, int slot, int threshold, int max_idle) { 260 | assert(fr != NULL); 261 | assert(results != NULL); 262 | 263 | if (max_records == 0) 264 | return 0; 265 | if (max_idle == 0) 266 | max_idle = max_records; 267 | 268 | int len = vl_len(fr->vl); 269 | 270 | // Initialize variables for looping 271 | int count = 1; 272 | int idle_count = 0; 273 | int missed = 0; 274 | 275 | // Record the initial time before the start 276 | uint64_t prev_time = rdtscp64(); 277 | 278 | // Store when the analysis was started 279 | *starttsc = prev_time; 280 | 281 | while (idle_count < max_idle && count < max_records) { 282 | idle_count++; 283 | results += len; 284 | count++; 285 | if (missed) { 286 | for (int i = 0; i < len; i++) 287 | results[i] = 0; 288 | } else { 289 | fr_probe(fr, results); 290 | if (is_active(results, len, threshold)) 291 | idle_count = 0; 292 | } 293 | prev_time += slot; 294 | missed = slotwait(prev_time); 295 | } 296 | return count; 297 | } 298 | #endif 299 | 300 | int fr_repeatedprobe(fr_t fr, int max_records, uint16_t *results, int slot) { 301 | return fr_trace(fr, max_records, results, slot, 0, max_records); 302 | } 303 | 304 | -------------------------------------------------------------------------------- /src/l3.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 CSIRO 3 | * 4 | * This file is part of Mastik. 5 | * 6 | * Mastik is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * Mastik is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with Mastik. If not, see . 18 | */ 19 | 20 | #include "config.h" 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #ifdef __APPLE__ 30 | #include 31 | #endif 32 | 33 | #include "vlist.h" 34 | #include "l3.h" 35 | #include "timestats.h" 36 | #include "low.h" 37 | 38 | #define CHECKTIMES 16 39 | 40 | /* 41 | * Intel documentation still mentiones one slice per core, but 42 | * experience shows that at least some Skylake models have two 43 | * smaller slices per core. 44 | * When probing the cache, we can use the smaller size - this will 45 | * increase the probe time but for huge pages, where we use 46 | * the slice size, the probe is fast and the increase is not too 47 | * significant. 48 | * When using the PTE maps, we need to know the correct size and 49 | * the correct number of slices. This means that, currently and 50 | * without user input, PTE is not guaranteed to work. 51 | * So, on a practical note, L3_GROUPSIZE_FOR_HUGEPAGES is the 52 | * smallest slice size we have seen; L3_SETS_PER_SLICE is the 53 | * default for the more common size. If we learn how to probe 54 | * the slice size we can get rid of this mess. 55 | */ 56 | #define L3_SETS_PER_SLICE 2048 57 | #define L3_GROUPSIZE_FOR_HUGEPAGES 1024 58 | 59 | // The number of cache sets in each page 60 | #define L3_SETS_PER_PAGE 64 61 | 62 | #define L3_CACHELINE 64 63 | 64 | #define LNEXT(t) (*(void **)(t)) 65 | #define OFFSET(p, o) ((void *)((uintptr_t)(p) + (o))) 66 | #define NEXTPTR(p) (OFFSET((p), sizeof(void *))) 67 | 68 | #define IS_MONITORED(monitored, setno) ((monitored)[(setno)>>5] & (1 << ((setno)&0x1f))) 69 | #define SET_MONITORED(monitored, setno) ((monitored)[(setno)>>5] |= (1 << ((setno)&0x1f))) 70 | #define UNSET_MONITORED(monitored, setno) ((monitored)[(setno)>>5] &= ~(1 << ((setno)&0x1f))) 71 | 72 | #ifdef MAP_HUGETLB 73 | #define HUGEPAGES MAP_HUGETLB 74 | #endif 75 | #ifdef VM_FLAGS_SUPERPAGE_SIZE_2MB 76 | #define HUGEPAGES VM_FLAGS_SUPERPAGE_SIZE_2MB 77 | #endif 78 | 79 | #ifdef HUGEPAGES 80 | #define HUGEPAGEBITS 21 81 | #define HUGEPAGESIZE (1<> 1; 112 | v ^= v >> 2; 113 | v = (v & 0x1111111111111111UL) * 0x1111111111111111UL; 114 | return (v >> 60) & 1; 115 | } 116 | 117 | 118 | static int addr2slice_linear(uintptr_t addr, int slices) { 119 | int bit0 = parity(addr & SLICE_MASK_0); 120 | int bit1 = parity(addr & SLICE_MASK_1); 121 | int bit2 = parity(addr & SLICE_MASK_2); 122 | return ((bit2 << 2) | (bit1 << 1) | bit0) & (slices - 1); 123 | } 124 | 125 | static uintptr_t getphysaddr(void *p) { 126 | #ifdef __linux__ 127 | static int fd = -1; 128 | 129 | if (fd < 0) { 130 | fd = open("/proc/self/pagemap", O_RDONLY); 131 | if (fd < 0) 132 | return 0; 133 | } 134 | uint64_t buf; 135 | memaccess(p); 136 | uintptr_t intp = (uintptr_t)p; 137 | int r = pread(fd, &buf, sizeof(buf), ((uintptr_t)p)/4096 * sizeof(buf)); 138 | 139 | return (buf & ((1ULL << 54) - 1)) << 12 | ((uintptr_t)p & 0x3ff); 140 | #else 141 | return 0; 142 | #endif 143 | } 144 | 145 | 146 | static int loadL3cpuidInfo(l3pp_t l3) { 147 | 148 | for (int i = 0; ; i++) { 149 | l3->cpuidInfo.regs.eax = CPUID_CACHEINFO; 150 | l3->cpuidInfo.regs.ecx = i; 151 | cpuid(&l3->cpuidInfo); 152 | if (l3->cpuidInfo.cacheInfo.type == 0) 153 | return 0; 154 | if (l3->cpuidInfo.cacheInfo.level == 3) 155 | return 1; 156 | } 157 | } 158 | 159 | static void fillL3Info(l3pp_t l3) { 160 | loadL3cpuidInfo(l3); 161 | if (l3->l3info.associativity == 0) 162 | l3->l3info.associativity = l3->cpuidInfo.cacheInfo.associativity + 1; 163 | if (l3->l3info.slices == 0) { 164 | if (l3->l3info.setsperslice == 0) 165 | l3->l3info.setsperslice = L3_SETS_PER_SLICE; 166 | l3->l3info.slices = (l3->cpuidInfo.cacheInfo.sets + 1)/ l3->l3info.setsperslice; 167 | } 168 | if (l3->l3info.setsperslice == 0) 169 | l3->l3info.setsperslice = (l3->cpuidInfo.cacheInfo.sets + 1)/l3->l3info.slices; 170 | if (l3->l3info.bufsize == 0) { 171 | l3->l3info.bufsize = l3->l3info.associativity * l3->l3info.slices * l3->l3info.setsperslice * L3_CACHELINE * 2; 172 | if (l3->l3info.bufsize < 10 * 1024 * 1024) 173 | l3->l3info.bufsize = 10 * 1024 * 1024; 174 | } 175 | } 176 | 177 | #if 0 178 | void printL3Info() { 179 | struct l3info l3Info; 180 | loadL3cpuidInfo(&l3Info); 181 | printf("type : %u\n", l3Info.cpuidInfo.cacheInfo.type); 182 | printf("level : %u\n", l3Info.cpuidInfo.cacheInfo.level); 183 | printf("selfInitializing: %u\n", l3Info.cpuidInfo.cacheInfo.selfInitializing); 184 | printf("fullyAssociative: %u\n", l3Info.cpuidInfo.cacheInfo.fullyAssociative); 185 | printf("logIds : %u\n", l3Info.cpuidInfo.cacheInfo.logIds + 1); 186 | printf("phyIds : %u\n", l3Info.cpuidInfo.cacheInfo.phyIds + 1); 187 | printf("lineSize : %u\n", l3Info.cpuidInfo.cacheInfo.lineSize + 1); 188 | printf("partitions : %u\n", l3Info.cpuidInfo.cacheInfo.partitions + 1); 189 | printf("associativity : %u\n", l3Info.cpuidInfo.cacheInfo.associativity + 1); 190 | printf("sets : %u\n", l3Info.cpuidInfo.cacheInfo.sets + 1); 191 | printf("wbinvd : %u\n", l3Info.cpuidInfo.cacheInfo.wbinvd); 192 | printf("inclusive : %u\n", l3Info.cpuidInfo.cacheInfo.inclusive); 193 | printf("complexIndex : %u\n", l3Info.cpuidInfo.cacheInfo.complexIndex); 194 | exit(0); 195 | } 196 | #endif 197 | 198 | 199 | 200 | 201 | void *sethead(l3pp_t l3, int set) { //vlist_t list, int count, int offset) { 202 | vlist_t list = l3->groups[set / l3->groupsize]; 203 | 204 | int count = l3->l3info.associativity; 205 | if (count == 0 || vl_len(list) < count) 206 | count = vl_len(list); 207 | 208 | int offset = (set % l3->groupsize) * L3_CACHELINE; 209 | 210 | for (int i = 0; i < count; i++) { 211 | LNEXT(OFFSET(vl_get(list, i), offset)) = OFFSET(vl_get(list, (i + 1) % count), offset); 212 | LNEXT(OFFSET(vl_get(list, i), offset+sizeof(void*))) = OFFSET(vl_get(list, (i + count - 1) % count), offset+sizeof(void *)); 213 | } 214 | 215 | return OFFSET(vl_get(list, 0), offset); 216 | } 217 | 218 | void prime(void *pp, int reps) { 219 | walk((void *)pp, reps); 220 | } 221 | 222 | #define str(x) #x 223 | #define xstr(x) str(x) 224 | 225 | int probetime(void *pp) { 226 | if (pp == NULL) 227 | return 0; 228 | int rv = 0; 229 | void *p = (void *)pp; 230 | uint32_t s = rdtscp(); 231 | do { 232 | p = LNEXT(p); 233 | } while (p != (void *) pp); 234 | return rdtscp()-s; 235 | } 236 | 237 | int bprobetime(void *pp) { 238 | if (pp == NULL) 239 | return 0; 240 | return probetime(NEXTPTR(pp)); 241 | } 242 | 243 | 244 | int probecount(void *pp) { 245 | if (pp == NULL) 246 | return 0; 247 | int rv = 0; 248 | void *p = (void *)pp; 249 | do { 250 | uint32_t s = rdtscp(); 251 | p = LNEXT(p); 252 | s = rdtscp() - s; 253 | if (s > L3_THRESHOLD) 254 | rv++; 255 | } while (p != (void *) pp); 256 | return rv; 257 | } 258 | 259 | int bprobecount(void *pp) { 260 | if (pp == NULL) 261 | return 0; 262 | return probecount(NEXTPTR(pp)); 263 | } 264 | 265 | 266 | static int timedwalk(void *list, register void *candidate) { 267 | #ifdef DEBUG 268 | static int debug = 100; 269 | static int debugl = 1000; 270 | #else 271 | #define debug 0 272 | #endif //DEBUG 273 | if (list == NULL) 274 | return 0; 275 | if (LNEXT(list) == NULL) 276 | return 0; 277 | void *start = list; 278 | ts_t ts = ts_alloc(); 279 | void *c2 = (void *)((uintptr_t)candidate ^ 0x200); 280 | LNEXT(c2) = candidate; 281 | clflush(c2); 282 | memaccess(candidate); 283 | for (int i = 0; i < CHECKTIMES * (debug ? 20 : 1); i++) { 284 | walk(list, 20); 285 | void *p = LNEXT(c2); 286 | uint32_t time = memaccesstime(p); 287 | ts_add(ts, time); 288 | } 289 | int rv = ts_median(ts); 290 | #ifdef DEBUG 291 | if (!--debugl) { 292 | debugl=1000; 293 | debug++; 294 | } 295 | if (debug) { 296 | printf("--------------------\n"); 297 | for (int i = 0; i < TIME_MAX; i++) 298 | if (ts_get(ts, i) != 0) 299 | printf("++ %4d: %4d\n", i, ts_get(ts, i)); 300 | debug--; 301 | } 302 | #endif //DEBUG 303 | ts_free(ts); 304 | return rv; 305 | } 306 | 307 | static int checkevict(vlist_t es, void *candidate) { 308 | if (vl_len(es) == 0) 309 | return 0; 310 | for (int i = 0; i < vl_len(es); i++) 311 | LNEXT(vl_get(es, i)) = vl_get(es, (i + 1) % vl_len(es)); 312 | int timecur = timedwalk(vl_get(es, 0), candidate); 313 | return timecur > L3_THRESHOLD; 314 | } 315 | 316 | 317 | static void *expand(vlist_t es, vlist_t candidates) { 318 | while (vl_len(candidates) > 0) { 319 | void *current = vl_poprand(candidates); 320 | if (checkevict(es, current)) 321 | return current; 322 | vl_push(es, current); 323 | } 324 | return NULL; 325 | } 326 | 327 | static void contract(vlist_t es, vlist_t candidates, void *current) { 328 | for (int i = 0; i < vl_len(es);) { 329 | void *cand = vl_get(es, i); 330 | vl_del(es, i); 331 | clflush(current); 332 | if (checkevict(es, current)) 333 | vl_push(candidates, cand); 334 | else { 335 | vl_insert(es, i, cand); 336 | i++; 337 | } 338 | } 339 | } 340 | 341 | static void collect(vlist_t es, vlist_t candidates, vlist_t set) { 342 | for (int i = vl_len(candidates); i--; ) { 343 | void *p = vl_del(candidates, i); 344 | if (checkevict(es, p)) 345 | vl_push(set, p); 346 | else 347 | vl_push(candidates, p); 348 | } 349 | } 350 | 351 | 352 | static vlist_t map(l3pp_t l3, vlist_t lines) { 353 | #ifdef DEBUG 354 | printf("%d lines\n", vl_len(lines)); 355 | #endif // DEBUG 356 | vlist_t groups = vl_new(); 357 | vlist_t es = vl_new(); 358 | int nlines = vl_len(lines); 359 | int fail = 0; 360 | while (vl_len(lines)) { 361 | assert(vl_len(es) == 0); 362 | #ifdef DEBUG 363 | int d_l1 = vl_len(lines); 364 | #endif // DEBUG 365 | if (fail > 5) 366 | break; 367 | void *c = expand(es, lines); 368 | #ifdef DEBUG 369 | int d_l2 = vl_len(es); 370 | #endif //DEBUG 371 | if (c == NULL) { 372 | while (vl_len(es)) 373 | vl_push(lines, vl_del(es, 0)); 374 | #ifdef DEBUG 375 | printf("set %3d: lines: %4d expanded: %4d c=NULL\n", vl_len(groups), d_l1, d_l2); 376 | #endif // DEBUG 377 | fail++; 378 | continue; 379 | } 380 | contract(es, lines, c); 381 | contract(es, lines, c); 382 | contract(es, lines, c); 383 | #ifdef DEBUG 384 | int d_l3 = vl_len(es); 385 | #endif //DEBUG 386 | if (vl_len(es) > l3->l3info.associativity || vl_len(es) < l3->l3info.associativity - 3) { 387 | while (vl_len(es)) 388 | vl_push(lines, vl_del(es, 0)); 389 | #ifdef DEBUG 390 | printf("set %3d: lines: %4d expanded: %4d contracted: %2d failed\n", vl_len(groups), d_l1, d_l2, d_l3); 391 | #endif // DEBUG 392 | fail++; 393 | continue; 394 | } 395 | fail = 0; 396 | vlist_t set = vl_new(); 397 | vl_push(set, c); 398 | collect(es, lines, set); 399 | while (vl_len(es)) 400 | vl_push(set, vl_del(es, 0)); 401 | #ifdef DEBUG 402 | printf("set %3d: lines: %4d expanded: %4d contracted: %2d collected: %d\n", vl_len(groups), d_l1, d_l2, d_l3, vl_len(set)); 403 | #endif // DEBUG 404 | vl_push(groups, set); 405 | if (l3->l3info.progressNotification) 406 | (*l3->l3info.progressNotification)(nlines - vl_len(lines), nlines, l3->l3info.progressNotificationData); 407 | } 408 | 409 | vl_free(es); 410 | return groups; 411 | } 412 | 413 | static int probemap(l3pp_t l3) { 414 | if ((l3->l3info.flags & L3FLAG_NOPROBE) != 0) 415 | return 0; 416 | vlist_t pages = vl_new(); 417 | for (int i = 0; i < l3->l3info.bufsize; i+= l3->groupsize * L3_CACHELINE) 418 | vl_push(pages, l3->buffer + i); 419 | vlist_t groups = map(l3, pages); 420 | 421 | //Store map results 422 | l3->ngroups = vl_len(groups); 423 | l3->groups = (vlist_t *)calloc(l3->ngroups, sizeof(vlist_t)); 424 | for (int i = 0; i < vl_len(groups); i++) 425 | l3->groups[i] = vl_get(groups, i); 426 | vl_free(groups); 427 | vl_free(pages); 428 | return 1; 429 | } 430 | 431 | static int ptemap(l3pp_t l3) { 432 | if ((l3->l3info.flags & L3FLAG_USEPTE) == 0) 433 | return 0; 434 | if (getphysaddr(l3->buffer) == 0) 435 | return 0; 436 | if (l3->l3info.slices & (l3->l3info.slices - 1)) // Cannot do non-linear for now 437 | return 0; 438 | l3->ngroups = l3->l3info.setsperslice * l3->l3info.slices / l3->groupsize; 439 | l3->groups = (vlist_t *)calloc(l3->ngroups, sizeof(vlist_t)); 440 | for (int i = 0; i < l3->ngroups; i++) 441 | l3->groups[i] = vl_new(); 442 | 443 | for (int i = 0; i < l3->l3info.bufsize; i+= l3->groupsize * L3_CACHELINE) { 444 | uintptr_t phys = getphysaddr(l3->buffer + i); 445 | int slice = addr2slice_linear(phys, l3->l3info.slices); 446 | int cacheindex = ((phys / L3_CACHELINE) & (l3->l3info.setsperslice - 1)); 447 | vlist_t list = l3->groups[slice * (l3->l3info.setsperslice/l3->groupsize) + cacheindex/l3->groupsize]; 448 | vl_push(list, l3->buffer + i); 449 | } 450 | return 1; 451 | } 452 | 453 | 454 | 455 | 456 | l3pp_t l3_prepare(l3info_t l3info) { 457 | // Setup 458 | l3pp_t l3 = (l3pp_t)malloc(sizeof(struct l3pp)); 459 | bzero(l3, sizeof(struct l3pp)); 460 | if (l3info != NULL) 461 | bcopy(l3info, &l3->l3info, sizeof(struct l3info)); 462 | fillL3Info(l3); 463 | 464 | // Allocate cache buffer 465 | int bufsize; 466 | char *buffer = MAP_FAILED; 467 | #ifdef HUGEPAGES 468 | if ((l3->l3info.flags & L3FLAG_NOHUGEPAGES) == 0) { 469 | bufsize = (l3->l3info.bufsize + HUGEPAGESIZE - 1) & ~HUGEPAGEMASK; 470 | l3->groupsize = L3_GROUPSIZE_FOR_HUGEPAGES; 471 | buffer = mmap(NULL, bufsize, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE|HUGEPAGES, -1, 0); 472 | } 473 | #endif 474 | 475 | if (buffer == MAP_FAILED) { 476 | bufsize = l3->l3info.bufsize; 477 | l3->groupsize = L3_SETS_PER_PAGE; 478 | buffer = mmap(NULL, bufsize, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0); 479 | } 480 | if (buffer == MAP_FAILED) { 481 | free(l3); 482 | return NULL; 483 | } 484 | l3->buffer = buffer; 485 | l3->l3info.bufsize = bufsize; 486 | 487 | // Create the cache map 488 | if (!ptemap(l3)) { 489 | if (!probemap(l3)) { 490 | free(l3->buffer); 491 | free(l3); 492 | return NULL; 493 | } 494 | } 495 | 496 | // Allocate monitored set info 497 | l3->monitoredbitmap = (uint32_t *)calloc(l3->ngroups*l3->groupsize/32, sizeof(uint32_t)); 498 | l3->monitoredset = (int *)malloc(l3->ngroups * l3->groupsize * sizeof(int)); 499 | l3->monitoredhead = (void **)malloc(l3->ngroups * l3->groupsize * sizeof(void *)); 500 | l3->nmonitored = 0; 501 | 502 | return l3; 503 | 504 | } 505 | 506 | void l3_release(l3pp_t l3) { 507 | munmap(l3->buffer, l3->l3info.bufsize); 508 | free(l3->monitoredbitmap); 509 | free(l3->monitoredset); 510 | free(l3->monitoredhead); 511 | bzero(l3, sizeof(struct l3pp)); 512 | free(l3); 513 | } 514 | 515 | 516 | 517 | int l3_monitor(l3pp_t l3, int line) { 518 | if (line < 0 || line >= l3->ngroups * l3->groupsize) 519 | return 0; 520 | if (IS_MONITORED(l3->monitoredbitmap, line)) 521 | return 0; 522 | SET_MONITORED(l3->monitoredbitmap, line); 523 | l3->monitoredset[l3->nmonitored] = line; 524 | l3->monitoredhead[l3->nmonitored] = sethead(l3, line); 525 | l3->nmonitored++; 526 | return 1; 527 | } 528 | 529 | int l3_unmonitor(l3pp_t l3, int line) { 530 | if (line < 0 || line >= l3->ngroups * l3->groupsize) 531 | return 0; 532 | if (!IS_MONITORED(l3->monitoredbitmap, line)) 533 | return 0; 534 | UNSET_MONITORED(l3->monitoredbitmap, line); 535 | for (int i = 0; i < l3->nmonitored; i++) 536 | if (l3->monitoredset[i] == line) { 537 | --l3->nmonitored; 538 | l3->monitoredset[i] = l3->monitoredset[l3->nmonitored]; 539 | l3->monitoredhead[i] = l3->monitoredhead[l3->nmonitored]; 540 | break; 541 | } 542 | return 1; 543 | } 544 | 545 | void l3_unmonitorall(l3pp_t l3) { 546 | l3->nmonitored = 0; 547 | for (int i = 0; i < l3->ngroups * l3->groupsize / 32; i++) 548 | l3->monitoredset[i] = 0; 549 | } 550 | 551 | int l3_getmonitoredset(l3pp_t l3, int *lines, int nlines) { 552 | if (lines == NULL || nlines == 0) 553 | return l3->nmonitored; 554 | if (nlines > l3->nmonitored) 555 | nlines = l3->nmonitored; 556 | bcopy(l3->monitoredset, lines, nlines * sizeof(int)); 557 | return l3->nmonitored; 558 | } 559 | 560 | void l3_randomise(l3pp_t l3) { 561 | for (int i = 0; i < l3->nmonitored; i++) { 562 | int p = random() % (l3->nmonitored - i) + i; 563 | int t = l3->monitoredset[p]; 564 | l3->monitoredset[p] = l3->monitoredset[i]; 565 | l3->monitoredset[i] = t; 566 | void *vt = l3->monitoredhead[p]; 567 | l3->monitoredhead[p] = l3->monitoredhead[i]; 568 | l3->monitoredhead[i] = vt; 569 | } 570 | } 571 | 572 | void l3_probe(l3pp_t l3, uint16_t *results) { 573 | for (int i = 0; i < l3->nmonitored; i++) { 574 | int t = probetime(l3->monitoredhead[i]); 575 | results[i] = t > UINT16_MAX ? UINT16_MAX : t; 576 | } 577 | } 578 | 579 | void l3_bprobe(l3pp_t l3, uint16_t *results) { 580 | for (int i = 0; i < l3->nmonitored; i++) { 581 | int t = bprobetime(l3->monitoredhead[i]); 582 | results[i] = t > UINT16_MAX ? UINT16_MAX : t; 583 | } 584 | } 585 | 586 | 587 | void l3_probecount(l3pp_t l3, uint16_t *results) { 588 | for (int i = 0; i < l3->nmonitored; i++) 589 | results[i] = probecount(l3->monitoredhead[i]); 590 | } 591 | 592 | void l3_bprobecount(l3pp_t l3, uint16_t *results) { 593 | for (int i = 0; i < l3->nmonitored; i++) 594 | results[i] = bprobecount(l3->monitoredhead[i]); 595 | } 596 | 597 | 598 | // Returns the number of probed sets in the LLC 599 | int l3_getSets(l3pp_t l3) { 600 | return l3->ngroups * l3->groupsize; 601 | } 602 | 603 | // Returns the number slices 604 | int l3_getSlices(l3pp_t l3) { 605 | return l3->l3info.slices; 606 | } 607 | 608 | // Returns the LLC associativity 609 | int l3_getAssociativity(l3pp_t l3) { 610 | return l3->l3info.associativity; 611 | } 612 | 613 | 614 | int l3_repeatedprobe(l3pp_t l3, int nrecords, uint16_t *results, int slot) { 615 | assert(l3 != NULL); 616 | assert(results != NULL); 617 | 618 | if (nrecords == 0) 619 | return 0; 620 | 621 | int len = l3->nmonitored; 622 | 623 | int even = 1; 624 | int missed = 0; 625 | uint64_t prev_time = rdtscp64(); 626 | for (int i = 0; i < nrecords; i++, results+=len) { 627 | if (missed) { 628 | for (int j = 0; j < len; j++) 629 | results[j] = 0; 630 | } else { 631 | if (even) 632 | l3_probe(l3, results); 633 | else 634 | l3_bprobe(l3, results); 635 | even = !even; 636 | } 637 | if (slot > 0) { 638 | prev_time += slot; 639 | missed = slotwait(prev_time); 640 | } 641 | } 642 | return nrecords; 643 | } 644 | 645 | 646 | int l3_repeatedprobecount(l3pp_t l3, int nrecords, uint16_t *results, int slot) { 647 | assert(l3 != NULL); 648 | assert(results != NULL); 649 | 650 | if (nrecords == 0) 651 | return 0; 652 | 653 | int len = l3->nmonitored; 654 | 655 | int even = 1; 656 | int missed = 0; 657 | uint64_t prev_time = rdtscp64(); 658 | for (int i = 0; i < nrecords; i++, results+=len) { 659 | if (missed) { 660 | for (int j = 0; j < len; j++) 661 | results[j] = -1; 662 | } else { 663 | if (even) 664 | l3_probecount(l3, results); 665 | else 666 | l3_bprobecount(l3, results); 667 | even = !even; 668 | } 669 | if (slot > 0) { 670 | prev_time += slot; 671 | missed = slotwait(prev_time); 672 | } 673 | } 674 | return nrecords; 675 | } 676 | 677 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------