From 22e59459176ccaba9d61ae66f96a6d6608e66737 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Mon, 5 Dec 2016 13:52:23 -0800 Subject: [PATCH 001/109] core-image-efi-initramfs: remove unneeded components for UMIP This is a special flavor of LUV for UMIP. Remove all the unneeded packages. Signed-off-by: Ricardo Neri --- .../images/core-image-efi-initramfs.bb | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/meta-luv/recipes-core/images/core-image-efi-initramfs.bb b/meta-luv/recipes-core/images/core-image-efi-initramfs.bb index 82a9cb881c1..2267ca7a7fd 100644 --- a/meta-luv/recipes-core/images/core-image-efi-initramfs.bb +++ b/meta-luv/recipes-core/images/core-image-efi-initramfs.bb @@ -3,19 +3,18 @@ DESCRIPTION = "Small image capable of booting a device and running the suite of EFI tests." IMAGE_INSTALL = "\ - base-files base-passwd netbase udev systemd luv-test-manager \ - luv-test-crash luv-test-netconsole luv-test keymaps \ - kernel-image fwts bash coreutils gawk grep util-linux-agetty \ - util-linux-mount util-linux-umount kmod sed tar net-tools \ - shadow util-linux procps efivarfs-test \ - plymouth plymouth-set-default-theme kernel-efi-warnings linux-firmware kexec \ + base-files base-passwd udev systemd \ + keymaps \ + kernel-image bash coreutils grep util-linux-agetty \ + util-linux-mount util-linux-umount kmod \ + shadow util-linux procps \ + plymouth plymouth-set-default-theme \ " -X86_ADDITIONS = "chipsec python-codecs python-subprocess vmcore-dmesg bits \ - kernel-modules telemetrics" +X86_ADDITIONS = "" IMAGE_INSTALL_append_qemux86 = "${X86_ADDITIONS}" -IMAGE_INSTALL_append_qemux86-64 = "${X86_ADDITIONS} ndctl" +IMAGE_INSTALL_append_qemux86-64 = "${X86_ADDITIONS}" export IMAGE_BASENAME = "core-image-efi-initramfs" From 4697ab2b35d418d399ce20a5647579ac3a5aae40 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Wed, 7 Dec 2016 12:18:53 -0800 Subject: [PATCH 002/109] live-vm-common: hack: bloat boot partition with a useless file I need this file to increase the size of the boot partition, whose size is calculated by the files it contains. The extra space created by this file makes it easy to add/replace files that are bigger than the originals. Signed-off-by: Ricardo Neri --- meta/classes/live-vm-common.bbclass | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/meta/classes/live-vm-common.bbclass b/meta/classes/live-vm-common.bbclass index 734697f9e62..a390c245b23 100644 --- a/meta/classes/live-vm-common.bbclass +++ b/meta/classes/live-vm-common.bbclass @@ -58,5 +58,9 @@ populate_kernel() { done chmod 0644 $dest/initrd fi + + # bloat the boot partition with a big file with newlines. + echo dest is $dest + yes '' | head -c 52428800 > $dest/empty.space } From 201ea39969e4e83aab5599be481221552e49472e Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Mon, 5 Dec 2016 15:38:53 -0800 Subject: [PATCH 003/109] meta-luv: add recipe for User-Mode Instruction Prevention User-Mode Instruction Prevention (UMIP) is a security feature present in new Intel Processors. If enabled, it prevents the execution of certain instructions if the Current Privilege Level (CPL) is greater than 0. If these instructions were executed while in CPL > 0, user space applications could have access to system-wide settings such as the global and local descriptor tables, the task register and the interrupt descriptor table. These are the instructions covered by UMIP: * SGDT - Store Global Descriptor Table * SIDT - Store Interrupt Descriptor Table * SLDT - Store Local Descriptor Table * SMSW - Store Machine Status Word * STR - Store Task Register If any of these instructions is executed with CPL > 0, a general protection exception is issued when UMIP is enbled. This recipe adds two sample user-space programs that exercises all the possible combinations of memory and register operands for the aforementioned instructions. This helps to validate the emulation code for such instuctions within the Linux kernel. Signed-off-by: Ricardo Neri --- .../images/core-image-efi-initramfs.bb | 4 +- meta-luv/recipes-core/umip/files/COPYING | 339 +++++++++ meta-luv/recipes-core/umip/files/Makefile | 39 ++ meta-luv/recipes-core/umip/files/UMIP_README | 32 + meta-luv/recipes-core/umip/files/normal_pf.c | 111 +++ .../recipes-core/umip/files/umip_ldt_16.c | 364 ++++++++++ .../recipes-core/umip/files/umip_ldt_32.c | 278 ++++++++ .../recipes-core/umip/files/umip_ldt_64.c | 190 +++++ meta-luv/recipes-core/umip/files/umip_test.c | 274 ++++++++ meta-luv/recipes-core/umip/files/umip_test2.c | 335 +++++++++ .../recipes-core/umip/files/umip_test_defs.h | 50 ++ .../umip/files/umip_test_gen_16.py | 408 +++++++++++ .../umip/files/umip_test_gen_32.py | 645 +++++++++++++++++ .../umip/files/umip_test_gen_64.py | 655 ++++++++++++++++++ meta-luv/recipes-core/umip/umip-tests_0.1.bb | 60 ++ 15 files changed, 3782 insertions(+), 2 deletions(-) create mode 100644 meta-luv/recipes-core/umip/files/COPYING create mode 100644 meta-luv/recipes-core/umip/files/Makefile create mode 100644 meta-luv/recipes-core/umip/files/UMIP_README create mode 100644 meta-luv/recipes-core/umip/files/normal_pf.c create mode 100644 meta-luv/recipes-core/umip/files/umip_ldt_16.c create mode 100644 meta-luv/recipes-core/umip/files/umip_ldt_32.c create mode 100644 meta-luv/recipes-core/umip/files/umip_ldt_64.c create mode 100644 meta-luv/recipes-core/umip/files/umip_test.c create mode 100644 meta-luv/recipes-core/umip/files/umip_test2.c create mode 100644 meta-luv/recipes-core/umip/files/umip_test_defs.h create mode 100644 meta-luv/recipes-core/umip/files/umip_test_gen_16.py create mode 100644 meta-luv/recipes-core/umip/files/umip_test_gen_32.py create mode 100644 meta-luv/recipes-core/umip/files/umip_test_gen_64.py create mode 100644 meta-luv/recipes-core/umip/umip-tests_0.1.bb diff --git a/meta-luv/recipes-core/images/core-image-efi-initramfs.bb b/meta-luv/recipes-core/images/core-image-efi-initramfs.bb index 2267ca7a7fd..8d811bfb9e7 100644 --- a/meta-luv/recipes-core/images/core-image-efi-initramfs.bb +++ b/meta-luv/recipes-core/images/core-image-efi-initramfs.bb @@ -13,8 +13,8 @@ IMAGE_INSTALL = "\ X86_ADDITIONS = "" -IMAGE_INSTALL_append_qemux86 = "${X86_ADDITIONS}" -IMAGE_INSTALL_append_qemux86-64 = "${X86_ADDITIONS}" +IMAGE_INSTALL_append_qemux86 = "${X86_ADDITIONS} umip-tests" +IMAGE_INSTALL_append_qemux86-64 = "${X86_ADDITIONS} umip-tests lib32-umip-tests" export IMAGE_BASENAME = "core-image-efi-initramfs" diff --git a/meta-luv/recipes-core/umip/files/COPYING b/meta-luv/recipes-core/umip/files/COPYING new file mode 100644 index 00000000000..d511905c164 --- /dev/null +++ b/meta-luv/recipes-core/umip/files/COPYING @@ -0,0 +1,339 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Lesser General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) year name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. diff --git a/meta-luv/recipes-core/umip/files/Makefile b/meta-luv/recipes-core/umip/files/Makefile new file mode 100644 index 00000000000..daea0702445 --- /dev/null +++ b/meta-luv/recipes-core/umip/files/Makefile @@ -0,0 +1,39 @@ +x86_64: + $(CC) -o umip_test2_64 umip_test2.c + $(CC) -o umip_test_64 umip_test.c + $(CC) -o umip_pf_64 normal_pf.c + + python umip_test_gen_64.py + $(CC) -c test_umip_ldt_64.c + $(CC) -c umip_ldt_64.c + $(CC) -o umip_ldt_64 test_umip_ldt_64.o umip_ldt_64.o + +i586: i686 + +i686: + # fo 32-bit builds, use -m32 if building independently + $(CC) -o umip_test2_32 umip_test2.c + $(CC) -o umip_test_32 umip_test.c + $(CC) -o umip_pf_32 normal_pf.c + + python umip_test_gen_32.py + $(CC) -c test_umip_ldt_32.c + $(CC) -c umip_ldt_32.c + $(CC) -o umip_ldt_32 test_umip_ldt_32.o umip_ldt_32.o + + python umip_test_gen_16.py + $(CC) -c test_umip_ldt_16.c + $(CC) -c umip_ldt_16.c + $(CC) -o umip_ldt_16 test_umip_ldt_16.o umip_ldt_16.o + + +.PHONY: clean +clean: + rm -f *.o + rm -f umip_test + rm -f umip_test2_64 + rm -f umip_test2_32 + rm -f umip_pf + rm -f umip_ldt_64 + rm -f umip_ldt_32 + rm -f umip_ldt_16 diff --git a/meta-luv/recipes-core/umip/files/UMIP_README b/meta-luv/recipes-core/umip/files/UMIP_README new file mode 100644 index 00000000000..ac28d240914 --- /dev/null +++ b/meta-luv/recipes-core/umip/files/UMIP_README @@ -0,0 +1,32 @@ +A quick guide for UMIP testing + +* Use umip_test for a very basic test that exercises the instructions smsw, + str, sidt, sldt and sgdt from protected mode using the global descriptor + table. +* Use test2_32 for a collection of tests that exercises the instructions + str, sldt and smsw for register and memory operands. For registers, + operand sizes are of 16 and 32 bits. Memory operands are always 16-bits + long. Addresses in these tests are computed using only the ModRM + byte. Also, these tests use the normal __USER_DS segment with a base + address of zero. +* DEPRECATED. Now UMIP is made to support only 32-bit builds of the kernel. + Use test2_64 for a collection of tests that exercises the instructions + str, sldt and smsw for register and memory operands. For registers, + operand sizes are of 16, 32 and 64 bits. Memory operands are always + 16-bit long. Addresses in these tests are computed using only the ModRM + byte. Also, these tests use the normal __USER_DS segment with a base + address of zero. +* Use umip_pf to verify that a page fault is correctly issued when UMIP- + emulated code tries to write in an invalid memory address of the user + space memory. +* Use umip_ldt to verify all the possible 32-bit address encodings in the + UMIP-emulated instructions. This includes all the ModRM, and SiB displacement + combinations. Furthermore, this test configure a Local Descriptor Table + whose segments have base addresses that are different from the __USER_DS + segment. +* Use umip_ldt_16 to verify all the possible 16-bit address encodings in the + UMIP-emulated instructions. This includes all the ModRM and displacement + combinations. Furthermore, this test configure a Local Descriptor Table + whose segments have base addresses that are different from the __USER_DS + segment. Also, the code segment is configured for 16-bit addresses. + diff --git a/meta-luv/recipes-core/umip/files/normal_pf.c b/meta-luv/recipes-core/umip/files/normal_pf.c new file mode 100644 index 00000000000..2678b845f44 --- /dev/null +++ b/meta-luv/recipes-core/umip/files/normal_pf.c @@ -0,0 +1,111 @@ +/* + * normal_pf.c - tests UMIP emulation code when a page fault should be + * generated (i.e., the requested memory access is not mapped. No code + * is included to test cases in which Memory Protection Keys are used. + * Copyright (c) 2016 Intel Corporation + * + * GPL v2. + */ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include "umip_test_defs.h" + +static sig_atomic_t signal_code; +static sig_atomic_t got_signal; + +void handler(int signum, siginfo_t *info, void *ctx_void) +{ + ucontext_t *ctx = (ucontext_t *)ctx_void; + + pr_info("si_signo[%d]\n", info->si_signo); + pr_info("si_errno[%d]\n", info->si_errno); + pr_info("si_code[%d]\n", info->si_code); + pr_info("si_code[0x%p]\n", info->si_addr); + if (signum != SIGSEGV) + errx(1, "ERROR: Received unexpected signal"); + else + got_signal = signum; + if (info->si_code == SEGV_MAPERR) + pr_info("Signal because of unmapped object.\n"); + else if (info->si_code == SI_KERNEL) + pr_info("Signal because of #GP\n"); + else + pr_info("Unknown si_code!\n"); + /* Save the signal code */ + signal_code = info->si_code; + /* + * Move to the next instruction. We have a cushion of + * several NOPs. Thus, we can safely move 8 positions + */ +#ifdef __x86_64__ + ctx->uc_mcontext.gregs[REG_RIP] += 8; +#else + ctx->uc_mcontext.gregs[REG_EIP] += 4; +#endif +} + +struct my_struct { + int a; + int b; + int c; +}; + +void main (void) +{ + struct sigaction action; + unsigned long val; + unsigned long *val_bad = (unsigned long *)0x100000; + + PRINT_BITNESS; + + memset(&action, 0, sizeof(action)); + action.sa_sigaction = &handler; + action.sa_flags = SA_SIGINFO; + sigemptyset(&action.sa_mask); + + if (sigaction(SIGSEGV, &action, NULL) < 0) { + pr_error("Could not set the signal handler!\n"); + exit(1); + } + + asm volatile ("smsw %0\n" + "nop\n" + "nop\n" + "nop\n" + "nop\n" + "nop\n" + "nop\n" + "nop\n" + "nop\n" + "nop\n": "=m"(*val_bad)); + + if (!got_signal) { + pr_fail("Signal not received!\n"); + return; + } +#ifdef __x86_64__ + if (signal_code != SI_KERNEL) + pr_fail("Signal code is not what we expect.\n"); + else + pr_pass("A SEGV_MAPERR page fault was issued.\n"); +#else + if (signal_code != SEGV_MAPERR) + pr_fail("Signal code is not what we expect.\n"); + else + pr_pass("A SEGV_MAPERR page fault was issued.\n"); +#endif + + memset(&action, 0, sizeof(action)); + action.sa_handler = SIG_DFL; + sigemptyset(&action.sa_mask); + + if (sigaction(SIGSEGV, &action, NULL) < 0) { + pr_error("Could not remove signal handler!\n"); + exit(1); + } +} diff --git a/meta-luv/recipes-core/umip/files/umip_ldt_16.c b/meta-luv/recipes-core/umip/files/umip_ldt_16.c new file mode 100644 index 00000000000..0678ed81321 --- /dev/null +++ b/meta-luv/recipes-core/umip/files/umip_ldt_16.c @@ -0,0 +1,364 @@ +/* Test cases for UMIP */ +/* Copyright Intel Corporation 2017 */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include "umip_test_defs.h" +#include "test_umip_ldt_16.h" +#include "test_umip_code_16.h" + +extern unsigned char test_umip[], test_umip_end[]; +extern unsigned char interim[], interim_start[], interim_end[]; +extern unsigned char finish_testing[]; +unsigned short cs_orig; + +#define CODE_DESC_INDEX 1 +#define CODE_16_DESC_INDEX 2 +#define DATA_DESC_INDEX 3 +#define STACK_16_DESC_INDEX 4 +#define STACK_DESC_INDEX 5 +#define DATA_ES_DESC_INDEX 6 +#define DATA_FS_DESC_INDEX 7 +#define DATA_GS_DESC_INDEX 8 + +#define RPL3 3 +#define TI_LDT 1 +#define SEGMENT_SELECTOR(index) (RPL3 | (TI_LDT << 2) | (index << 3)) + +static sig_atomic_t signal_code; +static sig_atomic_t got_signal; + +void handler(int signum, siginfo_t *info, void *ctx_void) +{ + ucontext_t *ctx = (ucontext_t *)ctx_void; + + pr_info("si_signo[%d]\n", info->si_signo); + pr_info("si_errno[%d]\n", info->si_errno); + pr_info("si_code[%d]\n", info->si_code); + pr_info("si_code[0x%p]\n", info->si_addr); + if (signum != SIGSEGV) + pr_error("Received unexpected signal"); + else + got_signal = signum; + if (info->si_code == SEGV_MAPERR) + pr_info("Signal because of unmapped object.\n"); + else if (info->si_code == SI_KERNEL) + pr_info("Signal because of #GP\n"); + else + pr_info("Unknown si_code!\n"); + + pr_fail("Whoa! I got a SIGSEGV! Something went wrong!\n"); + exit(1); +} + +asm(".pushsection .rodata\n\t" + "interim:\n\t" + /* this is the return point */ + "interim_return:\n\t" + /* restore our own stack */ + "mov %ebx, %ss\n\t" + "mov %eax, %esp\n\t" + /* restore ss of caller */ + "pop %ebx\n\t" + /* restore esp of caller*/ + "pop %eax\n\t" + /* prepare to return, set IP, CS is already in stack */ + "push $finish_testing\n\t" + "retf\n\t" + /* this is the interim start */ + "interim_start:\n\t" + /* setup stack */ + "mov $4096, %esp\n\t" + "mov %ecx, %ss\n\t" + /* save old cs */ + "push %edx\n\t" + /* save old esp */ + "push %eax\n\t" + /* save old ss */ + "push %ebx\n\t" + /* prepare to jump */ + /* pass current stack pointer, ss and cs */ + /* this is to know where we need to return to */ + "mov %esp, %eax\n\t" + "mov %ss, %ebx\n\t" + "mov %cs, %edx\n\t" + "push %edi\n\t" + "push $0\n\t" + "retf\n\t" + "interim_end:\n\t" + ".popsection\n\t" + ); + +static int setup_data_segments() +{ + int ret; + struct user_desc desc = { + .entry_number = 0, + .base_addr = 0, + .limit = SEGMENT_SIZE, + .seg_32bit = 0, + .contents = 0, /* data */ + .read_exec_only = 0, + .limit_in_pages = 0, + .seg_not_present = 0, + .useable = 1 + }; + + desc.entry_number = STACK_DESC_INDEX; + desc.base_addr = (unsigned long)&stack_32; + + memset(stack_32, 0x88, SEGMENT_SIZE); + + ret = syscall(SYS_modify_ldt, 1, &desc, sizeof(desc)); + if (ret) { + printf("Failed to install stack semgnet [%d].\n", ret); + return ret; + } + + desc.entry_number = STACK_16_DESC_INDEX; + desc.base_addr = (unsigned long)&stack; + + memset(stack, 0x44, SEGMENT_SIZE); + + ret = syscall(SYS_modify_ldt, 1, &desc, sizeof(desc)); + if (ret) { + pr_error("Failed to install stack semgnet [%d].\n", ret); + return ret; + } + + desc.entry_number = DATA_DESC_INDEX; + desc.base_addr = (unsigned long)&data; + + memset(data, 0x99, SEGMENT_SIZE); + + ret = syscall(SYS_modify_ldt, 1, &desc, sizeof(desc)); + if (ret) { + pr_error("Failed to install data segment [%d].\n", ret); + return ret; + } + + desc.entry_number = DATA_ES_DESC_INDEX; + desc.base_addr = (unsigned long)&data_es; + + memset(data_es, 0x77, SEGMENT_SIZE); + + ret = syscall(SYS_modify_ldt, 1, &desc, sizeof(desc)); + if (ret) { + pr_error("Failed to install data segment [%d].\n", ret); + return ret; + } + + desc.entry_number = DATA_FS_DESC_INDEX; + desc.base_addr = (unsigned long)&data_fs; + + memset(data_es, 0x66, SEGMENT_SIZE); + + ret = syscall(SYS_modify_ldt, 1, &desc, sizeof(desc)); + if (ret) { + pr_error("Failed to install data segment [%d].\n", ret); + return ret; + } + + desc.entry_number = DATA_GS_DESC_INDEX; + desc.base_addr = (unsigned long)&data_gs; + + memset(data_es, 0x55, SEGMENT_SIZE); + + ret = syscall(SYS_modify_ldt, 1, &desc, sizeof(desc)); + if (ret) { + pr_error("Failed to install data segment [%d].\n", ret); + return ret; + } + + return 0; +} + +void main(void) +{ + int ret; + unsigned short interim_cs, interim_ss; + unsigned short test_cs_16, test_ds_16, test_ss_16; + unsigned short test_es_16, test_fs_16, test_gs_16; + unsigned long interim_start_addr; + unsigned char *code_interim, *code_16; + struct sigaction action; + + struct user_desc code_desc = { + .entry_number = CODE_DESC_INDEX, + .seg_32bit = 1, + .contents = 2, /* non-conforming */ + .read_exec_only = 1, + .limit_in_pages = 0, + .seg_not_present = 0, + .useable = 1 + }; + + PRINT_BITNESS; + + memset(&action, 0, sizeof(action)); + action.sa_sigaction = &handler; + action.sa_flags = SA_SIGINFO; + sigemptyset(&action.sa_mask); + + if (sigaction(SIGSEGV, &action, NULL) < 0) { + pr_error("Could not set the signal handler!"); + goto err_out; + } + + code_interim = mmap(NULL, 4096, PROT_WRITE | PROT_READ | PROT_EXEC, + MAP_PRIVATE | MAP_32BIT | MAP_ANONYMOUS, -1, 0); + if (!code_interim) { + pr_error("Failed to allocate memory for interim code segment!\n"); + goto err_out; + } + + memcpy(code_interim, interim, interim_end - interim); + + code_16 = mmap(NULL, CODE_MEM_SIZE, PROT_WRITE | PROT_READ | PROT_EXEC, + MAP_PRIVATE | MAP_32BIT | MAP_ANONYMOUS, -1, 0); + if (!code_16) { + pr_error("Failed to allocate memory for code segment!\n"); + goto err_out; + } + + memcpy(code_16, test_umip, test_umip_end - test_umip); + + /* install our 32-bit intermediate code segment */ + code_desc.base_addr = (unsigned long)code_interim; + code_desc.limit = interim_end - interim + 100; + + ret = syscall(SYS_modify_ldt, 1, &code_desc, sizeof(code_desc)); + if (ret) { + pr_error("Failed to install interim code segment [%d].\n", ret); + goto err_out; + } + + /* install our 16-bit code segment */ + code_desc.entry_number = CODE_16_DESC_INDEX, + code_desc.base_addr = (unsigned long)code_16; + code_desc.seg_32bit = 0, + code_desc.limit = test_umip_end - test_umip + 100; + + ret = syscall(SYS_modify_ldt, 1, &code_desc, sizeof(code_desc)); + if (ret) { + pr_error("Failed to install 16-bit code segment [%d].\n", ret); + goto err_out; + } + + if (setup_data_segments()) { + pr_error("Failed to setup segments [%d].\n", ret); + goto err_out; + } + + interim_cs = SEGMENT_SELECTOR(CODE_DESC_INDEX); + interim_ss = SEGMENT_SELECTOR(STACK_DESC_INDEX); + test_cs_16 = SEGMENT_SELECTOR(CODE_16_DESC_INDEX); + test_ss_16 = SEGMENT_SELECTOR(STACK_16_DESC_INDEX); + test_ds_16 = SEGMENT_SELECTOR(DATA_DESC_INDEX); + test_es_16 = SEGMENT_SELECTOR(DATA_ES_DESC_INDEX); + test_fs_16 = SEGMENT_SELECTOR(DATA_FS_DESC_INDEX); + test_gs_16 = SEGMENT_SELECTOR(DATA_GS_DESC_INDEX); + + /* + * We cannot use the object's interim_start label as it we + * have copied our code to mmap'ed memory. Thus, we need + * calculate it as an offset since the beginning of the + * section + */ + interim_start_addr = interim_start - interim; + + asm(/* make a backup of everything */ + "push %%ds\n\t" + "push %%es\n\t" + "push %%fs\n\t" + "push %%gs\n\t" + "push %%eax\n\t" + "push %%ebx\n\t" + "push %%ecx\n\t" + "push %%edx\n\t" + "push %%edi\n\t" + "push %%esi\n\t" + "push %%ebp\n\t" + /* set new data segment */ + "mov %0, %%ds\n\t" + "mov %1, %%es\n\t" + "mov %2, %%fs\n\t" + "mov %3, %%gs\n\t" + /* + * Save current stack settings and pass them to the new code segment + * via registers. We will save these as soon as we setup a new stack + * segment. + */ + "mov %%esp, %%eax\n\t" + "mov %%ss, %%ebx\n\t" + /* + * Give interim code the new stack segment. We cannot set it here as + * we need it to jump to the test code via retf + */ + "mov %4, %%ecx\n\t" + /* + * Pass the code segment selector to the interim code so that it knows + * where to return + */ + "mov %%cs, %%edx\n\t" + /* + * Pass the code and stacks segment selectors of the 16-bit code + * as we only know them from this context. + */ + "mov %5, %%esi\n\t" + "mov %6, %%edi\n\t" + /* + *ljmp only takes constants. Instead use retf, which takes + * instruction pointer and code segment selector from the stack + */ + "push %7\n\t" + "push %8\n\t" + "retf \n\t" + "finish_testing:\n\t" + /* restore our stack */ + "mov %%ebx, %%ss\n\t" + "mov %%eax, %%esp\n\t" + /* restore everything */ + "pop %%ebp\n\t" + "pop %%esi\n\t" + "pop %%edi\n\t" + "pop %%edx\n\t" + "pop %%ecx\n\t" + "pop %%ebx\n\t" + "pop %%eax\n\t" + "pop %%gs\n\t" + "pop %%fs\n\t" + "pop %%es\n\t" + "pop %%ds\n\t" + : + :"m"(test_ds_16), "m"(test_es_16), "m"(test_fs_16), "m"(test_gs_16), + "m"(interim_ss), "m"(test_ss_16), "m"(test_cs_16), "m"(interim_cs), "m"(interim_start_addr) + ); + + pr_info("===Test results===\n"); + + check_results(); + + memset(&action, 0, sizeof(action)); + action.sa_handler = SIG_DFL; + sigemptyset(&action.sa_mask); + + if (sigaction(SIGSEGV, &action, NULL) < 0) { + pr_error("Could not remove signal handler!"); + exit(1); + } + + printf("Exiting...\n"); + return; +err_out: + pr_error("Could not run tests\n"); + exit(1); + +}; + diff --git a/meta-luv/recipes-core/umip/files/umip_ldt_32.c b/meta-luv/recipes-core/umip/files/umip_ldt_32.c new file mode 100644 index 00000000000..97f7cf6f5d7 --- /dev/null +++ b/meta-luv/recipes-core/umip/files/umip_ldt_32.c @@ -0,0 +1,278 @@ +/* Test cases for UMIP */ +/* Copyright Intel Corporation 2017 */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "umip_test_defs.h" +#include "test_umip_ldt_32.h" +#include "test_umip_code_32.h" + +extern unsigned char test_umip[], test_umip_end[]; +extern unsigned char finish_testing[]; +unsigned short cs_orig; + +#define CODE_DESC_INDEX 1 +#define DATA_DESC_INDEX 2 +#define STACK_DESC_INDEX 3 +#define DATA_ES_DESC_INDEX 4 +#define DATA_FS_DESC_INDEX 5 +#define DATA_GS_DESC_INDEX 6 + +#define RPL3 3 +#define TI_LDT 1 +#define SEGMENT_SELECTOR(index) (RPL3 | (TI_LDT << 2) | (index << 3)) + +static sig_atomic_t signal_code; +static sig_atomic_t got_signal; + +void handler(int signum, siginfo_t *info, void *ctx_void) +{ + ucontext_t *ctx = (ucontext_t *)ctx_void; + + pr_info("si_signo[%d]\n", info->si_signo); + pr_info("si_errno[%d]\n", info->si_errno); + pr_info("si_code[%d]\n", info->si_code); + pr_info("si_code[0x%p]\n", info->si_addr); + if (signum != SIGSEGV) + pr_error("Received unexpected signal"); + else + got_signal = signum; + if (info->si_code == SEGV_MAPERR) + pr_info("Signal because of unmapped object.\n"); + else if (info->si_code == SI_KERNEL) + pr_info("Signal because of #GP\n"); + else + pr_info("Unknown si_code!\n"); + + pr_fail("Whoa! I got a SIGSEGV! Something went wrong!\n"); + exit(1); +} + +static int setup_data_segments() +{ + int ret; + struct user_desc desc = { + .entry_number = 0, + .base_addr = 0, + .limit = SEGMENT_SIZE, + .seg_32bit = 1, + .contents = 0, /* data */ + .read_exec_only = 0, + .limit_in_pages = 0, + .seg_not_present = 0, + .useable = 1 + }; + + desc.entry_number = STACK_DESC_INDEX; + desc.base_addr = (unsigned long)&stack; + + memset(stack, 0x88, SEGMENT_SIZE); + + ret = syscall(SYS_modify_ldt, 1, &desc, sizeof(desc)); + if (ret) { + pr_error("Failed to install stack semgnet [%d].\n", ret); + return ret; + } + + desc.entry_number = DATA_DESC_INDEX; + desc.base_addr = (unsigned long)&data; + + memset(data, 0x99, SEGMENT_SIZE); + + ret = syscall(SYS_modify_ldt, 1, &desc, sizeof(desc)); + if (ret) { + pr_error("Failed to install data segment [%d].\n", ret); + return ret; + } + + desc.entry_number = DATA_ES_DESC_INDEX; + desc.base_addr = (unsigned long)&data_es; + + memset(data_es, 0x77, SEGMENT_SIZE); + + ret = syscall(SYS_modify_ldt, 1, &desc, sizeof(desc)); + if (ret) { + pr_error("Failed to install data segment [%d].\n", ret); + return ret; + } + + desc.entry_number = DATA_FS_DESC_INDEX; + desc.base_addr = (unsigned long)&data_fs; + + memset(data_es, 0x66, SEGMENT_SIZE); + + ret = syscall(SYS_modify_ldt, 1, &desc, sizeof(desc)); + if (ret) { + pr_error("Failed to install data segment [%d].\n", ret); + return ret; + } + + desc.entry_number = DATA_GS_DESC_INDEX; + desc.base_addr = (unsigned long)&data_gs; + + memset(data_es, 0x55, SEGMENT_SIZE); + + ret = syscall(SYS_modify_ldt, 1, &desc, sizeof(desc)); + if (ret) { + pr_error("Failed to install data segment [%d].\n", ret); + return ret; + } + + return 0; +} + +void main(void) +{ + int ret; + unsigned short test_cs, test_ds, test_ss; + unsigned short test_es, test_fs, test_gs; + struct sigaction action; + unsigned char *code; + + struct user_desc code_desc = { + .entry_number = CODE_DESC_INDEX, + .seg_32bit = 1, + .contents = 2, /* non-conforming */ + .read_exec_only = 1, + .limit_in_pages = 0, + .seg_not_present = 0, + .useable = 1 + }; + + PRINT_BITNESS; + + memset(&action, 0, sizeof(action)); + action.sa_sigaction = &handler; + action.sa_flags = SA_SIGINFO; + sigemptyset(&action.sa_mask); + + if (sigaction(SIGSEGV, &action, NULL) < 0) { + pr_error("Could not set the signal handler!"); + goto err_out; + } + + code = mmap(NULL, CODE_MEM_SIZE, PROT_WRITE | PROT_READ | PROT_EXEC, + MAP_PRIVATE | MAP_32BIT | MAP_ANONYMOUS, -1, 0); + if (!code) { + pr_error("Failed to allocate memory for code segment!\n"); + goto err_out; + } + + memcpy(code, test_umip, test_umip_end - test_umip); + + code_desc.base_addr = (unsigned long)code; + code_desc.limit = test_umip_end - test_umip + 100; + + ret = syscall(SYS_modify_ldt, 1, &code_desc, sizeof(code_desc)); + if (ret) { + pr_error("Failed to install code segment [%d].\n", ret); + goto err_out; + } + + if (setup_data_segments()) { + pr_error("Failed to setup segments [%d].\n", ret); + goto err_out; + } + + test_cs = SEGMENT_SELECTOR(CODE_DESC_INDEX); + test_ds = SEGMENT_SELECTOR(DATA_DESC_INDEX); + test_ss = SEGMENT_SELECTOR(STACK_DESC_INDEX); + test_es = SEGMENT_SELECTOR(DATA_ES_DESC_INDEX); + test_fs = SEGMENT_SELECTOR(DATA_FS_DESC_INDEX); + test_gs = SEGMENT_SELECTOR(DATA_GS_DESC_INDEX); + + asm(/* make a backup of everything */ + "push %%ds\n\t" + "push %%es\n\t" + "push %%fs\n\t" + "push %%gs\n\t" + "push %%eax\n\t" + "push %%ebx\n\t" + "push %%ecx\n\t" + "push %%edx\n\t" + "push %%edi\n\t" + "push %%esi\n\t" + "push %%ebp\n\t" + /* set new data segment */ + "mov %0, %%ds\n\t" + "mov %1, %%es\n\t" + "mov %2, %%fs\n\t" + "mov %3, %%gs\n\t" + /* + * Save current stack settings and pass them to the new code segment + * via registers. We will save these as soon as we setup a new stack + * segment. + */ + "mov %%esp, %%eax\n\t" + "mov %%ss, %%ebx\n\t" + /* + * Give test code the new stack segment. We cannot set it here as + * we need it to jump to the test code via retf + */ + "mov %4, %%ecx\n\t" + /* + * Pass the code segment selector to the new code so that it knows + * where to return + */ + "mov %%cs, %%edx\n\t" + /* + * ljmp only takes constants. Instead use retf, which pops + * instruction pointer and code segment selector from the stack + */ + "push %5\n\t" + /* jump to the beginning of the new segment */ + "push $0\n\t" + /* Everything is set. Make the jump */ + "retf \n\t" + /* After running tests, we return here */ + "finish_testing:\n\t" + /* restore our stack */ + "mov %%ebx, %%ss\n\t" + "mov %%eax, %%esp\n\t" + /* restore everything */ + "pop %%ebp\n\t" + "pop %%esi\n\t" + "pop %%edi\n\t" + "pop %%edx\n\t" + "pop %%ecx\n\t" + "pop %%ebx\n\t" + "pop %%eax\n\t" + "pop %%gs\n\t" + "pop %%fs\n\t" + "pop %%es\n\t" + "pop %%ds\n\t" + : + :"m"(test_ds), "m"(test_es), "m"(test_fs), "m"(test_gs), + "m"(test_ss), "m"(test_cs) + ); + + pr_info("===Test results===\n"); + check_results(); + + memset(&action, 0, sizeof(action)); + action.sa_handler = SIG_DFL; + sigemptyset(&action.sa_mask); + + if (sigaction(SIGSEGV, &action, NULL) < 0) { + pr_error("Could not remove signal handler!"); + exit(1); + } + + pr_info("Exiting...\n"); + + return; +err_out: + pr_error("Could not run tests\n"); + exit(1); + +}; + diff --git a/meta-luv/recipes-core/umip/files/umip_ldt_64.c b/meta-luv/recipes-core/umip/files/umip_ldt_64.c new file mode 100644 index 00000000000..9a71b02e066 --- /dev/null +++ b/meta-luv/recipes-core/umip/files/umip_ldt_64.c @@ -0,0 +1,190 @@ +/* Test cases for UMIP */ +/* Copyright Intel Corporation 2017 */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "umip_test_defs.h" +#include "test_umip_ldt_64.h" +#include "test_umip_code_64.h" + +extern unsigned char test_umip[], test_umip_end[]; +extern unsigned char finish_testing[]; +unsigned long old_fsbase, old_gsbase; +unsigned short old_fs, old_gs; + +#define CODE_DESC_INDEX 1 +#define DATA_FS_DESC_INDEX 2 +#define DATA_GS_DESC_INDEX 3 + +#define RPL3 3 +#define TI_LDT 1 +#define SEGMENT_SELECTOR(index) (RPL3 | (TI_LDT << 2) | (index << 3)) + +static sig_atomic_t signal_code; +static sig_atomic_t got_signal; + +void handler(int signum, siginfo_t *info, void *ctx_void) +{ + ucontext_t *ctx = (ucontext_t *)ctx_void; + + pr_info("si_signo[%d]\n", info->si_signo); + pr_info("si_errno[%d]\n", info->si_errno); + pr_info("si_code[%d]\n", info->si_code); + pr_info("si_code[0x%p]\n", info->si_addr); + if (signum != SIGSEGV) + pr_error("Received unexpected signal"); + else + got_signal = signum; + if (info->si_code == SEGV_MAPERR) + pr_info("Signal because of unmapped object.\n"); + else if (info->si_code == SI_KERNEL) + pr_info("Signal because of #GP\n"); + else + pr_info("Unknown si_code!\n"); + + pr_fail("Whoa! I got a SIGSEGV! Something went wrong!\n"); + exit(1); +} + +static int setup_data_segments() +{ + int ret; + struct user_desc desc = { + .entry_number = 0, + .base_addr = 0, + .limit = SEGMENT_SIZE, + .seg_32bit = 1, + .contents = 0, /* data */ + .read_exec_only = 0, + .limit_in_pages = 0, + .seg_not_present = 0, + .useable = 1 + }; + + desc.entry_number = DATA_FS_DESC_INDEX; + desc.base_addr = (unsigned long)&data_fs; + + memset(data_fs, 0x66, SEGMENT_SIZE); + + ret = syscall(SYS_modify_ldt, 1, &desc, sizeof(desc)); + if (ret) { + printf("Failed to install data segment [%d].\n", ret); + return ret; + } + + desc.entry_number = DATA_GS_DESC_INDEX; + desc.base_addr = (unsigned long)&data_gs; + + memset(data_gs, 0x55, SEGMENT_SIZE); + + ret = syscall(SYS_modify_ldt, 1, &desc, sizeof(desc)); + if (ret) { + printf("Failed to install data segment [%d].\n", ret); + return ret; + } + + return 0; +} + +void main(void) +{ + int ret; + unsigned short test_fs, test_gs; + unsigned char *code; + struct sigaction action; + + PRINT_BITNESS; + + memset(&action, 0, sizeof(action)); + action.sa_sigaction = &handler; + action.sa_flags = SA_SIGINFO; + sigemptyset(&action.sa_mask); + + if (sigaction(SIGSEGV, &action, NULL) < 0) { + pr_error("Could not set the signal handler!"); + goto err_out; + } + + code = mmap(NULL, CODE_MEM_SIZE, PROT_WRITE | PROT_READ | PROT_EXEC, + MAP_PRIVATE | MAP_32BIT | MAP_ANONYMOUS, -1, 0); + if (!code) { + printf("Failed to allocate memory for code segment!\n"); + goto err_out; + } + + memcpy(code, test_umip, test_umip_end - test_umip); + + test_fs = SEGMENT_SELECTOR(DATA_FS_DESC_INDEX); + test_gs = SEGMENT_SELECTOR(DATA_GS_DESC_INDEX); + + if (setup_data_segments()) { + pr_error("Failed to setup segments [%d].\n", ret); + goto err_out; + } + + syscall(SYS_arch_prctl, ARCH_GET_FS, &old_fsbase); + syscall(SYS_arch_prctl, ARCH_GET_GS, &old_gsbase); + + asm volatile("movw %%fs, %0" : "=m" (old_fs)); + asm volatile("movw %%gs, %0" : "=m" (old_gs)); + + syscall(SYS_arch_prctl, ARCH_SET_FS, (unsigned long)&data_fs); + syscall(SYS_arch_prctl, ARCH_SET_GS, (unsigned long)&data_gs); + + asm(/* make a backup of everything */ + "push %%rax\n\t" + "push %%rbx\n\t" + "push %%rcx\n\t" + "push %%rdx\n\t" + "push %%rdi\n\t" + "push %%rsi\n\t" + "push %%rbp\n\t" + /* set new data segment */ + /* jump to test code */ + "call *%2\n\t" + /* After running tests, we return here */ + "finish_testing:\n\t" + /* restore everything */ + "pop %%rbp\n\t" + "pop %%rsi\n\t" + "pop %%rdi\n\t" + "pop %%rdx\n\t" + "pop %%rcx\n\t" + "pop %%rbx\n\t" + "pop %%rax\n\t" + : + :"m"(test_fs), "m"(test_gs), "m"(code) + ); + + asm volatile("movw %0,%%fs" : :"m" (old_fs)); + asm volatile("movw %0, %%gs" : : "m" (old_gs)); + syscall(SYS_arch_prctl, ARCH_SET_FS, &old_fsbase); + syscall(SYS_arch_prctl, ARCH_SET_GS, &old_gsbase); + + printf("===Test results===\n"); + check_results(); + + memset(&action, 0, sizeof(action)); + action.sa_handler = SIG_DFL; + sigemptyset(&action.sa_mask); + + if (sigaction(SIGSEGV, &action, NULL) < 0) { + pr_error("Could not remove signal handler!"); + exit(1); + } + printf("Exiting...\n"); + + return; +err_out: + pr_error("Could not run tests\n"); + exit(1); +}; + diff --git a/meta-luv/recipes-core/umip/files/umip_test.c b/meta-luv/recipes-core/umip/files/umip_test.c new file mode 100644 index 00000000000..871467398ad --- /dev/null +++ b/meta-luv/recipes-core/umip/files/umip_test.c @@ -0,0 +1,274 @@ +/* + * tests for Intel User-Mode Execution Prevention + * + * GPLv2 + */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include "umip_test_defs.h" + +#ifdef __x86_64__ +#define GDTR_LEN 10 +#define IDTR_LEN 10 +#else +#define GDTR_LEN 6 +#define IDTR_LEN 6 +#endif + +static sig_atomic_t signal_code; +static sig_atomic_t got_signal; + +static void handler(int signum, siginfo_t *info, void *ctx_void) +{ + ucontext_t *ctx = (ucontext_t *)ctx_void; + + pr_info("si_signo[%d]\n", info->si_signo); + pr_info("si_errno[%d]\n", info->si_errno); + pr_info("si_code[%d]\n", info->si_code); + pr_info("si_code[0x%p]\n", info->si_addr); + if (signum != SIGSEGV) + errx(1, "ERROR: Received unexpected signal"); + else + got_signal = signum; + if (info->si_code == SEGV_MAPERR) + pr_info("Signal because of unmapped object.\n"); + else if (info->si_code == SI_KERNEL) + pr_info("Signal because of #GP\n"); + else + pr_info("Unknown si_code!\n"); + +#ifdef __x86_64__ + pr_pass("I got a SIGSEGV. UMIP not emulated in 64-bit\n"); +#else + pr_fail("FAIL: Whoa! I got a SIGSEGV. This is an error!\n"); +#endif + exit(1); +} + +static unsigned long get_mask(int op_size) { + switch (op_size) { + case 16: + return 0xffff; + case 32: + return 0xffffffff; +#if __x86_64__ + case 64: + return 0xffffffffffffffff; +#endif + default: + pr_error("Invalid operand size!\n"); + /* + * We can't return -1 as it would be equal to the + * 32 or 64-bit mask + */ + return 0; + } +} + +static void call_sgdt() +{ + unsigned char val[GDTR_LEN]; + unsigned long base = INIT_VAL(0x8989898989898989); + unsigned short limit = 0x3d3d; + int i; + + for (i = 0; i < GDTR_LEN; i++) + val[i] = 0; + pr_info("Will issue SGDT and save at [0x%lx]\n", val); + asm volatile("sgdt %0" : "=m" (val)); + + limit = val[1] << 8 | val[0]; + pr_info("GDT Limit [0x%04x]\n", limit); + + if (limit == expected_gdt.limit) + pr_pass("Expected limit value\n"); + else + pr_fail("Unexpected limit value\n"); + +#if 0 + for (i = 0; i < (GDTR_LEN -2); i++) + base |= (unsigned long) val[i+2] << ((i * 8)); +#else + base = *(unsigned long *)(val + 2); +#endif + pr_info("GDT Base [0x%016lx]\n", base); + + if (base == expected_gdt.base) + pr_pass("Expected base value\n"); + else + pr_fail("Unexpected base value\n"); +} + +static void call_sidt() +{ + unsigned char val[IDTR_LEN]; + unsigned long base = INIT_VAL(0x7373737373737373); + unsigned short limit = 0x9696; + int i; + + for (i = 0; i < IDTR_LEN; i++) + val[i] = 0; + pr_info("Will issue SIDT and save at [0x%lx]\n", val); + asm volatile("sidt %0" : "=m" (val)); + + limit = val[1] << 8 | val[0]; + pr_info("IDT Limit [0x%04x]\n", limit); + + if (limit == expected_idt.limit) + pr_pass("Expected limit value\n"); + else + pr_fail("Unexpected limit value\n"); + +#if 0 + for (i = 0; i < (IDTR_LEN -2); i++) + base |= (unsigned long) val[i+2] << ((i * 8)); +#else + base = *(unsigned long *)(val + 2); +#endif + pr_info("IDT Base [0x%016lx]\n", base); + + if (base == expected_idt.base) + pr_pass("Expected base value\n"); + else + pr_fail("Unexpected base value\n"); +} + +static void call_sldt() +{ + unsigned long val = INIT_VAL(0xa1a1a1a1a1a1a1a1); + unsigned long init_val = INIT_VAL(0xa1a1a1a1a1a1a1a1); + /* if operand is memory, result is 16-bit */ + unsigned short mask = 0xffff; + + pr_info("Will issue SLDT and save at [0x%lx]\n", &val); + asm volatile("sldt %0" : "=m" (val)); + + pr_info("SS for LDT[0x%08lx]\n", val); + + /* + * Check that the bits that are supposed to change does so + * as well as that the bits that are not supposed to change + * does not change. + */ \ + if ((val & mask) == expected_ldt && + (val & ~mask) == (init_val & ~mask)) + pr_pass("Obtained expected value\n"); + else + pr_fail("Obtained unexpected value\n"); +} + +static void call_smsw() +{ + unsigned long val = INIT_VAL(0xa2a2a2a2a2a2a2a2); + unsigned long init_val = INIT_VAL(0xa2a2a2a2a2a2a2a2); + unsigned short mask = 0xffff; + + pr_info("Will issue SMSW and save at [0x%lx]\n", &val); + asm volatile("smsw %0" : "=m" (val)); + + pr_info("CR0[0x%08lx]\n", val); + + /* + * Check that the bits that are supposed to change does so + * as well as that the bits that are not supposed to change + * does not change. + */ \ + if ((val & mask) == expected_msw && + (val & ~mask) == (init_val & ~mask)) + pr_pass("Obtained expected value\n"); + else + pr_fail("Obtained unexpected value\n"); +} + +static void call_str() +{ + unsigned int val32 = 0xa4a4a4a4; + unsigned int init_val32 = 0xa4a4a4a4; + unsigned short val16 = 0xa5a5; + unsigned short init_val16 = 0xa5a5; +#if __x86_64__ + unsigned long val64 = 0xa3a3a3a3a3a3a3a3; + + pr_info("Will issue STR and save at m64[0x%p]\n", &val64); + asm volatile("str %0" : "=m" (val64)); + pr_info("SS for TSS[0x%016lx]\n", val64); + + /* All 64 bits are written */ + if (val64 == expected_tr) + pr_pass("Obtained 64-bit expected value\n"); + else + pr_fail("Obtained 64-bit unexpected value\n"); +#endif + + pr_info("Will issue STR and save at m32[0x%p]\n", &val32); + asm volatile("str %0" : "=m" (val32)); + pr_info("SS for TSS[0x%08x]\n", val32); + + /* + * Check that the bits that are supposed to change does so + * as well as that the bits that are not supposed to change + * does not change. Since operand is memory, value will be + * 16-bit. + */ + if ((val32 & 0xffff) == expected_tr && + (val32 & ~0xffff) == (init_val32 & ~0xffff)) + pr_pass("Obtained 32-bit expected value\n"); + else + pr_fail("Obtained 32-bit unexpected value\n"); + + pr_info("Will issue STR and save at m16[0x%p]\n", &val16); + asm volatile("str %0" : "=m" (val16)); + pr_info("SS for TSS[0x%04x]\n", val16); + + /* + * Check that the bits that are supposed to change does so + * as well as that the bits that are not supposed to change + * does not change. Since operand is memory, value will be + * 16-bit. + */ + if ((val16 & 0xffff) == expected_tr && + (val16 & ~0xffff) == (init_val16 & ~0xffff)) + pr_pass("Obtained 16-bit expected value\n"); + else + pr_fail("Obtained 16-bit unexpected value\n"); + +} + +void main(void) +{ + struct sigaction action; + + PRINT_BITNESS; + + memset(&action, 0, sizeof(action)); + action.sa_sigaction = &handler; + action.sa_flags = SA_SIGINFO; + sigemptyset(&action.sa_mask); + + if (sigaction(SIGSEGV, &action, NULL) < 0) { + pr_error("Could not set the signal handler!"); + exit(1); + } + + call_sgdt(); + call_sidt(); + call_sldt(); + call_smsw(); + call_str(); + + memset(&action, 0, sizeof(action)); + action.sa_handler = SIG_DFL; + sigemptyset(&action.sa_mask); + + if (sigaction(SIGSEGV, &action, NULL) < 0) { + pr_error("Could not remove signal handler!"); + exit(1); + } +} diff --git a/meta-luv/recipes-core/umip/files/umip_test2.c b/meta-luv/recipes-core/umip/files/umip_test2.c new file mode 100644 index 00000000000..946a4e4bf7f --- /dev/null +++ b/meta-luv/recipes-core/umip/files/umip_test2.c @@ -0,0 +1,335 @@ +#include +#include +#include +#include +#include +#include +#include "umip_test_defs.h" + +/* Register operands */ + +#if __x86_64__ +#define PUSH(reg) "push %%r"reg"\n" +#define POP(reg) "pop %%r"reg"\n" +#else +#define PUSH(reg) "push %%e"reg"\n" +#define POP(reg) "pop %%e"reg"\n" +#endif + +#define INSNreg16(insn, reg, aux) \ + PUSH(aux) \ + PUSH(reg) \ + insn" %%"reg"\n" \ + "mov %%"reg", %%"aux"\n" \ + POP(reg) \ + "mov %%"aux", %0\n" \ + POP(aux) + +#define INSNreg32(insn, reg, aux) \ + PUSH(aux) \ + PUSH(reg) \ + insn" %%e"reg"\n" \ + "mov %%e"reg", %%e"aux"\n" \ + POP(reg) \ + "movl %%e"aux", %0\n" \ + POP(aux) + +#define INSNreg64(insn, reg, aux) \ + "push %%"aux"\n" \ + "push %%"reg"\n" \ + insn" %%"reg"\n" \ + "mov %%"reg", %%"aux"\n" \ + "pop %%"reg"\n" \ + "mov %%"aux", %0\n" \ + "pop %%"aux"\n" + +#define CHECK_INSN(op_size, insn, reg, val, aux, init, exp) \ + do { \ + val = init; \ + mask = get_mask(op_size); \ + if (!mask) \ + return -1; \ + asm volatile(INSNreg##op_size(insn, reg, aux) : "=m" (val)); \ + /* \ + * Check that the bits that are supposed to change does so \ + * as well as that the bits that are not supposed to change \ + * does not change. \ + */ \ + if (((val & mask) == (exp & mask)) && ((exp & ~mask) == (exp & ~mask))) \ + pr_pass(" On %s-bit '%s %s'! Got [0x%lx] Exp[0x%lx]\n", \ + #op_size, insn, reg, val, exp); \ + else { \ + pr_fail("On %s-bit '%s %s'! Got[0x%lx] Exp[0x%lx]\n", \ + #op_size, insn, reg, val, exp); \ + return -1; \ + } \ + } while(0); + +#define CHECK_ALLreg32(insn, val, init, exp) \ + CHECK_INSN(16, insn, "ax", val, "bx", init, exp); \ + CHECK_INSN(16, insn, "cx", val, "ax", init, exp); \ + CHECK_INSN(16, insn, "dx", val, "ax", init, exp); \ + CHECK_INSN(16, insn, "bx", val, "ax", init, exp); \ + CHECK_INSN(16, insn, "bp", val, "ax", init, exp); \ + CHECK_INSN(16, insn, "si", val, "ax", init, exp); \ + CHECK_INSN(16, insn, "di", val, "ax", init, exp); \ + CHECK_INSN(32, insn, "ax", val, "bx", init, exp); \ + CHECK_INSN(32, insn, "cx", val, "ax", init, exp); \ + CHECK_INSN(32, insn, "dx", val, "ax", init ,exp); \ + CHECK_INSN(32, insn, "bx", val, "ax", init ,exp); \ + CHECK_INSN(32, insn, "bp", val, "ax", init, exp);\ + CHECK_INSN(32, insn, "si", val, "ax", init, exp); \ + CHECK_INSN(32, insn, "di", val, "ax", init, exp); \ + +#define CHECK_ALLreg64(insn, val, init, exp) \ + CHECK_INSN(64, insn, "rax", val, "rbx", init, exp); \ + CHECK_INSN(64, insn, "rcx", val, "rax", init, exp); \ + CHECK_INSN(64, insn, "rdx", val, "rax", init, exp); \ + CHECK_INSN(64, insn, "rbx", val, "rax", init, exp); \ + CHECK_INSN(64, insn, "rbp", val, "rax", init, exp); \ + CHECK_INSN(64, insn, "rsi", val, "rax", init, exp); \ + CHECK_INSN(64, insn, "rdi", val, "rax", init, exp); \ + CHECK_INSN(64, insn, "r8", val, "rax", init, exp); \ + CHECK_INSN(64, insn, "r9", val, "rax", init, exp); \ + CHECK_INSN(64, insn, "r10", val, "rax", init, exp); \ + CHECK_INSN(64, insn, "r11", val, "rax", init, exp); \ + CHECK_INSN(64, insn, "r12", val, "rax", init, exp); \ + CHECK_INSN(64, insn, "r13", val, "rax", init, exp); \ + CHECK_INSN(64, insn, "r14", val, "rax", init, exp); \ + CHECK_INSN(64, insn, "r15", val, "rax", init, exp); + +#if __x86_64__ +#define CHECK_ALLreg(insn, val, init, exp) \ + CHECK_ALLreg32(insn, val, init, exp) \ + CHECK_ALLreg64(insn, val, init, exp) +#else +#define CHECK_ALLreg(insn, val, init, exp) \ + CHECK_ALLreg32(insn, val, init, exp) +#endif + + +/* Memory operands */ +#if __x86_64__ +#define INSNmem(insn, reg, disp) \ + "push %%rax\n" /* rax used to copy values around. Make a copy */ \ + "mov %0, %%rax\n" /* Init our variable. Split into two instructions */ \ + "mov %%rax, "disp"(%%rsp)\n" \ + "mov %%"reg", %%rax\n" /* make a backup of register under test */ \ + "mov %%rsp, %%"reg"\n" /* move rsp to our register under test */ \ + insn" "disp"(%%"reg")\n" /* execute our instruction */ \ + "push "disp"(%%"reg")\n" /* save result to stack */ \ + "mov %%rax, %%"reg"\n" /* restore register under test */ \ + "pop %1\n" /* copy result to our variable */ \ + "pop %%rax\n" /* restore rax */ +#else +#define INSNmem(insn, reg, disp) \ + "push %%eax\n" /* eax used to copy values around. Make a copy */ \ + "mov %0, %%eax\n" /* Init our variable. Split into two instructions */ \ + "mov %%eax, "disp"(%%esp)\n" \ + "mov %%"reg", %%eax\n" /* make a backup of register under test */ \ + "mov %%esp, %%"reg"\n" /* move esp to our register under test */ \ + insn" "disp"(%%"reg")\n" /* execute our instruction */ \ + "push "disp"(%%"reg")\n" /* save result to stack */ \ + "mov %%eax, %%"reg"\n" /* restore register under test */ \ + "pop %1\n" /* copy result to our variable */ \ + "pop %%eax\n" /* restore eax */ +#endif + +/* + * TODO: test no displacement. However, gcc refuses to not use displacement. + * Instead, it uses -0x0(%%reg). No displacement is OK unless the SIB byte + * is used with RBP. + */ +#define INSNmemdisp8(insn, reg) INSNmem(insn, reg, "-0x80") +#define INSNmemdisp32(insn, reg) INSNmem(insn, reg, "-0x1000") + +#define CHECK_INSNmemdisp(INSNmacro, insn, reg, val, init, exp) \ + val = init; \ + /* Memory operands are always treated as 16-bit locations */ \ + mask = get_mask(16); \ + if (!mask) \ + return -1; \ + asm volatile(INSNmacro(insn, reg) : "=m" (val): "m"(val) : "%rax"); \ + /* \ + * Check that the bits that are supposed to change does so \ + * as well as that the bits that are not supposed to change \ + * does not change. \ + */ \ + if (((val & mask) == (exp & mask)) && ((exp & ~mask) == (exp & ~mask))) \ + pr_pass("On '%s %s(%s)'! Got [0x%lx] Exp[0x%lx]\n", \ + insn, #INSNmacro, reg, val, exp); \ + else { \ + pr_fail("On '%s %s(%s)'! Got[0x%lx] Exp[0x%lx]\n", \ + insn, #INSNmacro, reg, val, exp); \ + return -1; \ + } + +#define CHECK_INSNmem(insn, reg, val, init, exp) \ + CHECK_INSNmemdisp(INSNmemdisp8, insn, reg, val, init, exp) \ + CHECK_INSNmemdisp(INSNmemdisp32, insn, reg, val, init, exp) + +#if __x86_64__ +#define CHECK_ALLmem(insn, val, init, exp) \ + CHECK_INSNmem(insn, "rax", val, init, exp) \ + CHECK_INSNmem(insn, "rcx", val, init, exp) \ + CHECK_INSNmem(insn, "rdx", val, init, exp) \ + CHECK_INSNmem(insn, "rbp", val, init, exp) \ + CHECK_INSNmem(insn, "rsi", val, init, exp) \ + CHECK_INSNmem(insn, "rdi", val, init, exp) \ + CHECK_INSNmem(insn, "r8", val, init, exp) \ + CHECK_INSNmem(insn, "r9", val, init, exp) \ + CHECK_INSNmem(insn, "r10", val, init, exp) \ + CHECK_INSNmem(insn, "r11", val, init, exp) \ + CHECK_INSNmem(insn, "r12", val, init, exp) \ + CHECK_INSNmem(insn, "r13", val, init, exp) \ + CHECK_INSNmem(insn, "r14", val, init, exp) \ + CHECK_INSNmem(insn, "r15", val, init, exp) +#else +#define CHECK_ALLmem(insn, val, init, exp) \ + CHECK_INSNmem(insn, "eax", val, init, exp) \ + CHECK_INSNmem(insn, "ecx", val, init, exp) \ + CHECK_INSNmem(insn, "edx", val, init, exp) \ + CHECK_INSNmem(insn, "ebp", val, init, exp) \ + CHECK_INSNmem(insn, "esi", val, init, exp) \ + CHECK_INSNmem(insn, "edi", val, init, exp) +#endif + +#define INIT_SS INIT_VAL(0x1313131313131313) +#define INIT_MSW INIT_VAL(0x1414141414141414) +#define INIT_LDTS INIT_VAL(0x1515151515151515) + +static sig_atomic_t signal_code; +static sig_atomic_t got_signal; + +static unsigned long get_mask(int op_size) { + switch (op_size) { + case 16: + return 0xffff; + case 32: + return 0xffffffff; +#if __x86_64__ + case 64: + return 0xffffffffffffffff; +#endif + default: + pr_error("Invalid operand size!\n"); + /* + * We can't return -1 as it would be equal to the + * 32 or 64-bit mask + */ + return 0; + } +} + +static int test_str(void) +{ + unsigned long val; + unsigned short mask = 0xffff; + + pr_info("====Checking STR. Expected value: [0x%lx]====\n", expected_tr); + pr_info("==Tests for register operands==\n"); + pr_info("Value should be saved at [0x%p]\n", &val); + CHECK_ALLreg("str", val, INIT_SS, expected_tr); + pr_info("==Tests for memory operands==\n"); + pr_info("Value should be saved at [0x%p]\n", &val); + CHECK_ALLmem("str", val, INIT_SS, expected_tr); + /* TODO: check with addressing using SIB byte */ + return 0; + +} + +static int test_smsw(void) +{ + unsigned long val; + unsigned short mask = 0xffff; + + pr_info("====Checking SMSW. Expected value: [0x%lx]====\n", expected_msw); + pr_info("==Tests for register operands==\n"); + pr_info("Value should be saved at [0x%p]\n", &val); + CHECK_ALLreg("smsw", val, INIT_MSW, expected_msw); + pr_info("==Tests for memory operands==\n"); + pr_info("Value should be saved at [0x%p]\n", &val); + CHECK_ALLmem("smsw", val, INIT_MSW, expected_msw); + /* TODO: check with addressing using SIB byte */ + return 0; +} + +static int test_sldt(void) +{ + unsigned long val; + unsigned short mask = 0xffff; + + pr_info("====Checking SLDT. Expected value: [0x%lx]====\n", expected_ldt); + pr_info("==Tests for register operands==\n"); + pr_info("Value should be saved at [0x%p]\n", &val); + CHECK_ALLreg("sldt", val, INIT_LDTS, expected_ldt); + pr_info("==Tests for memory operands==\n"); + pr_info("Value should be saved at [0x%p]\n", &val); + CHECK_ALLmem("sldt", val, INIT_LDTS, expected_ldt); + /* TODO: check with addressing using SIB byte */ + return 0; +} + +static void handler(int signum, siginfo_t *info, void *ctx_void) +{ + ucontext_t *ctx = (ucontext_t *)ctx_void; + + pr_info("si_signo[%d]\n", info->si_signo); + pr_info("si_errno[%d]\n", info->si_errno); + pr_info("si_code[%d]\n", info->si_code); + pr_info("si_code[0x%p]\n", info->si_addr); + if (signum != SIGSEGV) + pr_error("Received unexpected signal"); + else + got_signal = signum; + if (info->si_code == SEGV_MAPERR) + pr_info("Signal because of unmapped object.\n"); + else if (info->si_code == SI_KERNEL) + pr_info("Signal because of #GP\n"); + else + pr_info("Unknown si_code!\n"); + +#ifdef __x86_64__ + pr_pass("I got a SIGSEGV. UMIP not emulated in 64-bit\n"); +#else + pr_fail("FAIL: Whoa! I got a SIGSEGV. This is an error!\n"); +#endif + exit(1); +} + +void main (void) +{ + int ret_str, ret_smsw, ret_sldt; + struct sigaction action; + + PRINT_BITNESS; + + memset(&action, 0, sizeof(action)); + action.sa_sigaction = &handler; + action.sa_flags = SA_SIGINFO; + sigemptyset(&action.sa_mask); + + if (sigaction(SIGSEGV, &action, NULL) < 0) { + pr_error("Could not set the signal handler!"); + exit(1); + } + + pr_info("***Starting tests***\n"); + ret_str = test_str(); + ret_smsw = test_smsw(); + ret_sldt = test_sldt(); + if (ret_str || ret_smsw || ret_sldt) + pr_fail("***Test completed with errors str[%d] smsw[%d] sldt[%d]\n", + ret_str, ret_smsw, ret_sldt); + else + pr_pass("***All tests completed successfully.***\n"); + + memset(&action, 0, sizeof(action)); + action.sa_handler = SIG_DFL; + sigemptyset(&action.sa_mask); + + if (sigaction(SIGSEGV, &action, NULL) < 0) { + pr_error("Could not remove signal handler!"); + exit(1); + } +} diff --git a/meta-luv/recipes-core/umip/files/umip_test_defs.h b/meta-luv/recipes-core/umip/files/umip_test_defs.h new file mode 100644 index 00000000000..d31eb87980f --- /dev/null +++ b/meta-luv/recipes-core/umip/files/umip_test_defs.h @@ -0,0 +1,50 @@ +#ifndef _UMIP_TEST_DEFS_H +#define _UMIP_TEST_DEFS_H +#include + +#define TEST_PASS "\x1b[32m[pass]\x1b[0m " +#define TEST_FAIL "\x1b[31m[FAIL]\x1b[0m " +#define TEST_INFO "\x1b[34m[info]\x1b[0m " +#define TEST_ERROR "\x1b[33m[ERROR]\x1b[0m " + +#define pr_pass(...) printf(TEST_PASS __VA_ARGS__) +#define pr_fail(...) printf(TEST_FAIL __VA_ARGS__) +#define pr_info(...) printf(TEST_INFO __VA_ARGS__) +#define pr_error(...) printf(TEST_ERROR __VA_ARGS__) + +#ifdef __x86_64__ +#define PRINT_BITNESS pr_info("This binary uses 64-bit code\n") +#define INIT_VAL(val) (val) +#else +#define PRINT_BITNESS pr_info("This binary uses 32-bit code\n") +#define INIT_VAL(val) (val & 0xffffffff) +#endif + +#define EXPECTED_SMSW 0x33 +#define EXPECTED_SLDT 0x0 +#define EXPECTED_STR 0x0 +#define EXPECTED_GDT_BASE 0xfffe0000 +#define EXPECTED_GDT_LIMIT 0x0 +#define EXPECTED_IDT_BASE 0xffff0000 +#define EXPECTED_IDT_LIMIT 0x0 + +struct table_desc { + unsigned short limit; + unsigned long base; +} __attribute__((packed)); + +static const unsigned short expected_msw = EXPECTED_SMSW; +static const unsigned short expected_ldt = EXPECTED_SLDT; +static const unsigned short expected_tr = EXPECTED_STR; + +static const struct table_desc expected_gdt = { + .limit = EXPECTED_GDT_LIMIT, + .base = EXPECTED_GDT_BASE +}; + +static const struct table_desc expected_idt = { + .limit = EXPECTED_IDT_LIMIT, + .base = EXPECTED_IDT_BASE +}; + +#endif /* _UMIP_TEST_DEFS_H */ diff --git a/meta-luv/recipes-core/umip/files/umip_test_gen_16.py b/meta-luv/recipes-core/umip/files/umip_test_gen_16.py new file mode 100644 index 00000000000..6533ceac079 --- /dev/null +++ b/meta-luv/recipes-core/umip/files/umip_test_gen_16.py @@ -0,0 +1,408 @@ +""" Test-case generator for UMIP +Copyright Intel Corporation 2017 + +This file generates opcdoes for all (well, most) of the combinations +of memory operands for the UMIP-protected instructions. This includes +all the ModRM values and segments. + +TODO: Add support for 16-bit address encodings +TODO: Add support for SiB address encodings""" + +MODRM_MO0 = 0 +MODRM_MO1 = 1 +MODRM_MO2 = 2 +MODRM_MO3 = 3 + +#TODO: need to be based on the number test cases +SEGMENT_SIZE = 32768 +CODE_MEM_SIZE = 32768 + +class Instruction: + def __init__(self, name, opcode, modrm_reg, result_bytes, expected_val): + self.name = name + self.opcode = opcode + self.modrm_reg = modrm_reg + self.result_bytes = result_bytes + self.expected_val = expected_val + + +class Register: + def __init__(self, mnemonic, modrm_rm, name): + self.mnemonic = mnemonic + self.modrm_rm = modrm_rm + self.name = name + +class Segment: + def __init__(self, name, prefix, array): + self.name = name + self.prefix = prefix + self.array = array + + +BX_SI = Register(["%bx", "%si"], 0, ["bx", "si"]) +BX_DI = Register(["%bx", "%di"], 1, ["bx", "di"]) +BP_SI = Register(["%bp", "%si"], 2, ["bp", "si"]) +BP_DI = Register(["%bp", "%di"], 3, ["bp", "di"]) +SI = Register(["%si"], 4, ["si"]) +DI = Register(["%di"], 5, ["di"]) +BP = Register(["%bp"], 6, ["bp"]) +BX = Register(["%bx"], 7, ["bx"]) + +SMSW = Instruction("smsw", "0xf, 0x1", 4, 2, "expected_msw") +SLDT = Instruction("sldt", "0xf, 0x0", 0, 2, "expected_ldt") +STR = Instruction("str", "0xf, 0x0", 1, 2, "expected_tr") +SIDT = Instruction("sidt", "0xf, 0x1", 1, 6, "&expected_idt") +SGDT = Instruction("sgdt", "0xf, 0x1", 0, 6, "&expected_gdt") + +CS = Segment("cs", "", "code") +DS = Segment("ds", "", "data") +SS = Segment("ss", "", "stack") +ES = Segment("es", "0x26", "data_es") +FS = Segment("fs", "0x64", "data_fs") +GS = Segment("gs", "0x65", "data_gs") + +DATA_SEGS = [ DS, ES, FS, GS ] +INSTS = [SMSW, SLDT, STR, SGDT, SIDT ] + +MO0 = [ BX_SI, BX_DI, BP_SI, BP_DI, SI, DI, BX ] +MO1 = [ BX_SI, BX_DI, BP_SI, BP_DI, SI, DI, BP, BX ] +MO2 = [ BX_SI, BX_DI, BP_SI, BP_DI, SI, DI, BP, BX ] + + +def two_comp_8(val): + if (val > 0): + return val + else: + return ((abs(val) ^ 0xff) + 1) & 0xff + +def two_comp_16(val): + if (val > 0): + return val + else: + return ((abs(val) ^ 0xffff) + 1) & 0xffff + +def get_segment_prefix(segment, register, modrm): + """ default segments """ + if (segment.prefix == ""): + segment_str = "" + if (register == BX_SI): + segment_chk_str = "data" + elif (register == BX_DI): + segment_chk_str = "data" + elif (register == BP_SI): + segment_chk_str = "stack" + elif (register == BP_DI): + segment_chk_str = "stack" + elif (register == SI): + segment_chk_str = "data" + elif (register == DI): + segment_chk_str = "data" + elif (register == BP): + # This is for a pure disp32, no register involved. Thus, default is data + if ((modrm >> 6) == 0): + segment_chk_str = "data" + else: + segment_chk_str = "stack" + elif (register == BX): + segment_chk_str = "data" + else: # we should not fall here + segment_chk_str = "data" + else: + segment_str = segment.prefix + ", " + segment_chk_str = segment.array + + return segment_str, segment_chk_str + +def generate_check_code(comment, segment_chk_str, address, inst): + # TODO: Use an enum here + if (inst.result_bytes == 2): + checkcode = "\tgot = *(unsigned short *)(" + segment_chk_str +" + " + str(hex(address)) + ");\n" + checkcode += "\tprintf(\"%s " + comment + ". Got:[0x%x]Exp[0x%x]\\n\",\n" + checkcode += "\t got==expected ? TEST_PASS : TEST_FAIL, got, expected);\n" + elif (inst.result_bytes == 6): + checkcode = "\tgot = (struct table_desc *)(" + segment_chk_str +" + " + str(hex(address)) + ");\n" + checkcode += "\tprintf(\"%s " + comment + ". Got:Base[0x%lx]Limit[0x%x]ExpBase[0x%lx]Limit[0x%x]\\n\",\n" + checkcode += "\t ((got->base == expected->base) && (got->limit == expected->limit)) ? TEST_PASS : TEST_FAIL, got->base, got->limit, expected->base, expected->limit);\n" + return checkcode + +def generate_disp(modrm, disp): + modrm_mod = modrm >> 6 + modrm_rm = modrm & 0x7 + if (modrm_mod == 0): + # if r/m is 6 (same as BP), this is a disp16 + if (modrm_rm == 6): + disp_2comp = two_comp_16(disp) + disp_str = ", " + str(hex(disp_2comp & 0xff)) + ", " + disp_str += str(hex((disp_2comp >> 8) & 0xff)) + else: + disp_str = "" + elif (modrm_mod == 1): + disp_2comp = two_comp_8(disp) + disp_str = ", " + str(hex(disp)) + elif (modrm_mod == 2): + disp_2comp = two_comp_16(disp) + disp_str = ", " + str(hex(disp_2comp & 0xff)) + ", " + disp_str += str(hex((disp_2comp >> 8) & 0xff)) + + return disp_str + +def generate_code(tc_nr, segment, inst, register, modrm_mod, index, disp): + code_start = "\t\".byte " + code_end = "\\n\\t\"\n" + + modrm = (modrm_mod << 6) | (inst.modrm_reg << 3) | register.modrm_rm + modrm_str = ", " + str(hex(modrm)) + + mov_reg_str = "" + for m in register.mnemonic: + val = two_comp_16(index/len(register.mnemonic)) + # we are just splitting the index between the number of registers indicated in modrm (max is 2) + # this should not be a problem as we always expect the index to be an even number + mov_reg_str += "\t\"mov $" + str(hex(val)) + ", " + m + "\\n\\t\"\n" + + segment_str, segment_chk_str = get_segment_prefix(segment, register, modrm) + + opcode_str = inst.opcode + + disp_str = generate_disp(modrm, disp) + + comment = "Test case " + str(tc_nr) + ": " + comment += "SEG[" + segment_chk_str + "] " + comment += "INSN: " + inst.name + "(" + if (len(register.name) == 2): + comment += register.name[0] + " + " + register.name[1] + else: + comment += register.name[0] + if (modrm_mod == 0): + if ((modrm & 0x7) == 6): + comment += " + disp32). " + else: + comment += "). " + elif (modrm_mod == 1): + comment += " + disp8). " + elif (modrm_mod == 2): + comment += " + disp16). " + comment += "EFF_ADDR[" + str(hex(index + disp)) + "]. " + for n in register.name: + comment += n + "[" + str(hex(index/len(register.name))) + "] " + if (modrm_mod == 0): + comment += "" + elif ((modrm_mod == 0) and ((modrm & 0x7) == 6)): + comment += "disp16[" + str(hex(disp)) + "]" + elif (modrm_mod == 1): + comment += "disp8[" + str(hex(disp)) + "]" + elif (modrm_mod == 2): + comment += "disp16[" + str(hex(disp)) + "]" + + code = "\t/* " + comment + " */\n" + code += mov_reg_str + code += code_start + segment_str + opcode_str + modrm_str + disp_str + code_end + + checkcode = generate_check_code(comment, segment_chk_str, index + disp, inst) + + return code, checkcode, inst.result_bytes + +def generate_special_unit_tests(start_tc_nr, segment, inst, start_idx): + code = "" + checkcode = "" + index = start_idx + tc_nr = start_tc_nr + code_start = "\t\".byte " + code_end = "\\n\\t\"\n" + opcode_str = inst.opcode + + # MOD = 0, r/m = 6 (BP), no index is used, only a disp16 + modrm = (MODRM_MO0 << 6) | (inst.modrm_reg << 3) | BP.modrm_rm + modrm_str = ", " + str(hex(modrm)) + + segment_str, segment_chk_str = get_segment_prefix(segment, BP, modrm) + + disp_str = generate_disp(modrm, index) + + comment = "Special Test case " + str(tc_nr) + ": " + comment += "SEG[" + segment.name + "] " + comment += "INSN: " + inst.name + " (disp32)." + comment += "EFF_ADDR[" + str(hex(index)) + "]." + comment += " disp32[" + str(hex(index)) + "]" + + code = "\t/* " + comment + " */\n" + code += code_start + segment_str + opcode_str + modrm_str + disp_str + code_end + + checkcode += generate_check_code(comment, segment_chk_str, index, inst) + + index += inst.result_bytes + tc_nr += 1 + + return code, checkcode, index, tc_nr + +def generate_unit_tests(segment, inst, start_idx, start_tc_nr): + code = "" + check_code = "" + index = start_idx + testcase_nr = start_tc_nr + + code += "\t /* ==================== Test code for " + inst.name + " ==================== */\n" + code += "\t\"test_umip_" + inst.name + "_" + segment.name + ":\\t\\n\"\n" + + check_code += "\n/* AUTOGENERATED CODE */\n" + if (inst.result_bytes == 2): + check_code += "static void check_tests_" + inst.name + "_" + segment.name + "(const unsigned short expected)\n" + check_code += "{\n" + check_code += "\tunsigned short got;\n\n" + elif (inst.result_bytes == 6): + check_code += "static void check_tests_" + inst.name + "_" + segment.name + "(const struct table_desc *expected)\n" + check_code += "{\n" + check_code += "\tstruct table_desc *got;\n\n" + + run_check_code = "\tcheck_tests_" + inst.name + "_" + segment.name + "(" + inst.expected_val + ");\n" + check_code += "\tprintf(\"=======Results for " + inst.name + " in segment " + segment.name + "=============\\n\");\n" + + for reg in MO0: + c, chk, idx = generate_code(testcase_nr, segment, inst, reg, MODRM_MO0, index, 0) + code += c + check_code += chk + index += idx + testcase_nr += 1 + + # force a negative displacement + start_addr = -100 + index += 100 + + for reg in MO1: + c, chk, idx = generate_code(testcase_nr, segment, inst, reg, MODRM_MO1, index, start_addr) + code += c + check_code += chk + index += idx + testcase_nr += 1 + + # force a negative index + start_addr += index + 100 + index = -100 + + for reg in MO2: + c, chk, idx = generate_code(testcase_nr, segment, inst, reg, MODRM_MO2, index, start_addr) + code += c + check_code += chk + index += idx + testcase_nr += 1 + + start_addr += index + + c, chk, idx, tc_nr = generate_special_unit_tests(testcase_nr, segment, inst, start_addr) + code += c + check_code += chk + testcase_nr = tc_nr + start_addr = idx + + code += "\t\"test_umip_" + inst.name + "_" + segment.name + "_end:\\t\\n\"\n" + check_code += "}\n\n" + + return code, check_code, run_check_code, start_addr, testcase_nr + +def generate_tests_all_insts(seg, start_index, start_test_nr): + run_check_code = "" + test_code = "" + check_code = "" + index = start_index + test_nr = start_test_nr + + for inst in INSTS: + tc, chkc, hdr, index, test_nr = generate_unit_tests(seg, inst, index, test_nr) + run_check_code += hdr + test_code += tc + check_code += chkc + + return test_code, check_code, run_check_code, index, test_nr + +def generate_test_cases(test_code, check_code): + index = 0 + + header_info = "/* ******************** AUTOGENERATED CODE ******************** */\n" + header_info += "#define SEGMENT_SIZE " + str(SEGMENT_SIZE) + "\n" + header_info += "#define CODE_MEM_SIZE " + str(CODE_MEM_SIZE) + "\n" + header_info += "\n" + header_info += "unsigned char data[SEGMENT_SIZE];\n" + header_info += "unsigned char data_es[SEGMENT_SIZE];\n" + header_info += "unsigned char data_fs[SEGMENT_SIZE];\n" + header_info += "unsigned char data_gs[SEGMENT_SIZE];\n" + header_info += "unsigned char stack_32[SEGMENT_SIZE];\n" + header_info += "unsigned char stack[SEGMENT_SIZE];\n" + header_info += "\n" + header_info += "void check_results(void);\n" + header_info += "\n" + + check_code += "/* ******************** AUTOGENERATED CODE ******************** */\n" + check_code += "#include \n" + check_code += "#include \"test_umip_ldt_16.h\"\n\n" + check_code += "#include \"umip_test_defs.h\"\n\n" + check_code += "\n" + + run_check_code = "" + + test_code += "\tasm(\n" + test_code += "\t /* ******************** AUTOGENERATED CODE ******************** */\n" + test_code += "\t\".pushsection .rodata\\n\\t\"\n" + test_code += "\t\"test_umip:\\n\\t\"\n" + test_code += "\t\".code16\\n\\t\"\n" + test_code += "\t/* setup stack */\n" + test_code += "\t\"mov $" + str(SEGMENT_SIZE) + ", %sp\\n\\t\"\n" + test_code += "\t\"mov %si, %ss\\n\\t\"\n" + test_code += "\t/* save caller's cs */\n" + test_code += "\t\"push %dx\\n\\t\"\n" + test_code += "\t/* save caller's sp */\n" + test_code += "\t\"push %ax\\n\\t\"\n" + test_code += "\t/* save caller's ss */\n" + test_code += "\t\"push %bx\\n\\t\"\n" + + test_nr = 0 + + for seg in DATA_SEGS: + index = 0 + tc, chkc, rchk, index, test_nr = generate_tests_all_insts(seg, index, test_nr) + run_check_code += rchk + test_code += tc + check_code += chkc + + test_code += "\t/* preparing to return */\n" + test_code += "\t/* restore caller's ss */\n" + test_code += "\t\"pop %bx\\n\\t\"\n" + test_code += "\t/* restore caller's sp */\n" + test_code += "\t\"pop %ax\\n\\t\"\n" + test_code += "\t/*\n" + test_code += "\t * We only need the return IP, CS is already in stack\n" + test_code += "\t * jump to interim_start, which is at the beginning of\n" + test_code += "\t * this chunk of code\n" + test_code += "\t */\n" + test_code += "\t\"push $0\\n\\t\"\n" + test_code += "\t\"retf\\n\\t\"\n" + test_code += "\t\"test_umip_end:\\t\\n\"\n" + test_code += "\t\".code32\\n\\t\"\n" + test_code += "\t\".popsection\\n\\t\"\n" + test_code += "\t);\n" + + check_code += "\n" + check_code += "void check_results(void)\n" + check_code += "{\n" + check_code += run_check_code + check_code += "}\n" + check_code += "\n" + + return test_code, check_code, header_info, index + +def write_test_files(): + check_code = "" + test_code = "/* This is an autogenerated file. If you intend to debug, better to debug the generating script. */\n\n" + test_code += "\n" + test_code, check_code, check_code_hdr, global_index = generate_test_cases(test_code, check_code) + + fcheck_code = open("test_umip_ldt_16.c", "w") + fheader = open("test_umip_ldt_16.h", "w") + ftest_code = open("test_umip_code_16.h", "w") + ftest_code.writelines(test_code) + fheader.writelines(check_code_hdr) + fcheck_code.writelines(check_code) + ftest_code.close() + fcheck_code.close() + fheader.close() + +write_test_files() diff --git a/meta-luv/recipes-core/umip/files/umip_test_gen_32.py b/meta-luv/recipes-core/umip/files/umip_test_gen_32.py new file mode 100644 index 00000000000..2dce20b8d35 --- /dev/null +++ b/meta-luv/recipes-core/umip/files/umip_test_gen_32.py @@ -0,0 +1,645 @@ +""" Test-case generator for UMIP +Copyright Intel Corporation 2017 + +This file generates opcdoes for all (well, most) of the combinations +of memory operands for the UMIP-protected instructions. This includes +all the ModRM values and segments. + +TODO: Add support for 16-bit address encodings +TODO: Add support for SiB address encodings""" + +MODRM_MO0 = 0 +MODRM_MO1 = 1 +MODRM_MO2 = 2 +MODRM_MO3 = 3 + +#TODO: need to be based on the number test cases +SEGMENT_SIZE = 262144 +CODE_MEM_SIZE = 262144 + +#expected values +EXPECTED_SMSW = 0x33 +EXPECTED_SLDT = 0x0 +EXPECTED_STR = 0x0 +EXPECTED_GDT_BASE = 0xfffe0000 +EXPECTED_GDT_LIMIT = 0x0 +EXPECTED_IDT_BASE = 0xffff0000 +EXPECTED_IDT_LIMIT = 0x0 + +class Instruction: + def __init__(self, name, opcode, modrm_reg, result_bytes, expected_val): + self.name = name + self.opcode = opcode + self.modrm_reg = modrm_reg + self.result_bytes = result_bytes + self.expected_val = expected_val + + +class Register: + def __init__(self, mnemonic, modrm_rm, name): + self.mnemonic = mnemonic + self.modrm_rm = modrm_rm + self.name = name + +class Segment: + def __init__(self, name, prefix, array): + self.name = name + self.prefix = prefix + self.array = array + + +EAX = Register("%eax", 0, "eax") +ECX = Register("%ecx", 1, "ecx") +EDX = Register("%edx", 2, "edx") +EBX = Register("%ebx", 3, "ebx") +ESP = Register("%esp", 4, "esp") +EBP = Register("%ebp", 5, "ebp") +ESI = Register("%esi", 6, "esi") +EDI = Register("%edi", 7, "edi") + +SMSW = Instruction("smsw", "0xf, 0x1", 4, 2, "expected_msw") +SLDT = Instruction("sldt", "0xf, 0x0", 0, 2, "expected_ldt") +STR = Instruction("str", "0xf, 0x0", 1, 2, "expected_tr") +SIDT = Instruction("sidt", "0xf, 0x1", 1, 6, "&expected_idt") +SGDT = Instruction("sgdt", "0xf, 0x1", 0, 6, "&expected_gdt") + +CS = Segment("cs", "", "code") +DS = Segment("ds", "", "data") +SS = Segment("ss", "", "stack") +ES = Segment("es", "0x26", "data_es") +FS = Segment("fs", "0x64", "data_fs") +GS = Segment("gs", "0x65", "data_gs") + +DATA_SEGS = [ DS, ES, FS, GS ] +INSTS = [SMSW, SLDT, STR, SGDT, SIDT ] + +MO0 = [ EAX, ECX, EDX, EBX, ESI, EDI ] +MO1 = [ EAX, ECX, EDX, EBX, EBP, ESI, EDI ] +MO2 = MO1 + +SIB_index = [ EAX, ECX, EDX, EBX, EBP, ESI, EDI ] + +SIB_base_MO0 = [ EAX, ECX, EDX, EBX, ESI, EDI ] +SIB_base_MO1 = [ EAX, ECX, EDX, EBX, EBP, ESI, EDI] +SIB_base_MO2 = SIB_base_MO1 + +def two_comp_32(val): + if (val > 0): + return val + else: + return ((abs(val) ^ 0xffffffff) + 1) & 0xffffffff + +def two_comp_8(val): + if (val > 0): + return hex(val) + else: + return hex(((abs(val) ^ 0xff) + 1) & 0xff) + +def find_backup_reg(do_not_use): + regs = [ EAX, ECX, EDX, EBX, ESP, EBP, ESI, EDI] + for u in do_not_use: + regs.remove(u) + return regs[0] + +def get_segment_prefix(segment, register, modrm, sib=0): + """ default segments """ + if (segment.prefix == ""): + segment_str = "" + if ((register == EBP) or (register == ESP)): + # This is for a pure disp32, no register involved. Thus, default is data + if (((modrm >> 6) == 0) and (modrm & 0x7) == 5): + segment_chk_str = "data" + # If using a sib byte and the base is EBP, use DS as the base is ignored + elif (((modrm >> 6) == 0) and ((modrm & 0x7) == 4) and ((sib & 0x7) == 5)): + segment_chk_str = "data" + else: + segment_chk_str = "stack" + else: + segment_chk_str = "data" + else: + segment_str = segment.prefix + ", " + segment_chk_str = segment.array + + return segment_str, segment_chk_str + +def generate_check_code(comment, segment_chk_str, address, inst): + # TODO: Use an enum here + if (inst.result_bytes == 2): + checkcode = "\tgot = *(unsigned short *)(" + segment_chk_str +" + " + str(hex(address)) + ");\n" + checkcode += "\tprintf(\"%s " + comment + ". Got:[0x%x]Exp[0x%x]\\n\",\n" + checkcode += "\t got==expected ? TEST_PASS : TEST_FAIL, got, expected);\n" + elif (inst.result_bytes == 6): + checkcode = "\tgot = (struct table_desc *)(" + segment_chk_str +" + " + str(hex(address)) + ");\n" + checkcode += "\tprintf(\"%s " + comment + ". Got:Base[0x%lx]Limit[0x%x]ExpBase[0x%lx]Limit[0x%x]\\n\",\n" + checkcode += "\t ((got->base == expected->base) && (got->limit == expected->limit)) ? TEST_PASS : TEST_FAIL, got->base, got->limit, expected->base, expected->limit);\n" + return checkcode + +def generate_disp(modrm, disp, sib=0): + modrm_mod = modrm >> 6 + if (modrm_mod == 0): + + # if a sib byte is used, disp 32 is used + if (((sib &7) == 5) and (modrm & 7) == 4): + disp_2comp = two_comp_32(disp) + disp_str = ", " + str(disp_2comp & 0xff) + ", " + disp_str += str((disp_2comp >> 8) & 0xff) + ", " + disp_str += str((disp_2comp >> 16) & 0xff) + ", " + disp_str += str((disp_2comp >> 24) & 0xff) + else: + disp_str = "" + elif (modrm_mod == 1): + disp_2comp = two_comp_8(disp) + disp_str = ", " + str(disp_2comp) + elif (modrm_mod == 2): + disp_2comp = two_comp_32(disp) + disp_str = ", " + str(disp_2comp & 0xff) + ", " + disp_str += str((disp_2comp >> 8) & 0xff) + ", " + disp_str += str((disp_2comp >> 16) & 0xff) + ", " + disp_str += str((disp_2comp >> 24) & 0xff) + + return disp_str + +def generate_code(tc_nr, segment, inst, register, modrm_mod, index, disp): + code_start = "\t\".byte " + code_end = "\\n\\t\"\n" + + modrm = (modrm_mod << 6) | (inst.modrm_reg << 3) | register.modrm_rm + modrm_str = ", " + str(hex(modrm)) + mov_reg_str = "\t\"mov $" + str(hex(two_comp_32(index))) + ", " + register.mnemonic + "\\n\\t\"\n" + + segment_str, segment_chk_str = get_segment_prefix(segment, register, modrm) + + opcode_str = inst.opcode + + disp_str = generate_disp(modrm, disp) + + comment = "Test case " + str(tc_nr) + ": " + comment += "SEG[" + segment_chk_str + "] " + comment += "INSN: " + inst.name + "(" + register.name + if (modrm_mod == 0): + comment += "). " + elif (modrm_mod == 1): + comment += " + disp8). " + elif (modrm_mod == 2): + comment += " + disp32). " + comment += "EFF_ADDR[" + str(hex(index + disp)) + "]. " + comment += register.name + "[" + str(hex(index)) + "] " + if (modrm_mod == 0): + comment += "" + elif (modrm_mod == 1): + comment += "disp8[" + str(hex(disp)) + "]" + elif (modrm_mod == 2): + comment += "disp32[" + str(hex(disp)) + "]" + + code = "\t/* " + comment + " */\n" + code += mov_reg_str + code += code_start + segment_str + opcode_str + modrm_str + disp_str + code_end + + checkcode = generate_check_code(comment, segment_chk_str, index + disp, inst) + + return code, checkcode, inst.result_bytes + +def generate_code_sib(tc_nr, segment, inst, reg_base, reg_index, modrm_mod, sib_index, sib_base, sib_scale, disp, special=None): + code_start = "\t\".byte " + code_end = "\\n\\t\"\n" + + modrm = (modrm_mod << 6) | (inst.modrm_reg << 3) | ESP.modrm_rm + sib = (sib_scale << 6) | (reg_index.modrm_rm << 3) | reg_base.modrm_rm + modrm_str = ", " + str(hex(modrm)) + sib_str = ", " + str(hex(sib)) + mov_reg_str = "\t\"mov $" + str(hex(two_comp_32(sib_base))) + ", " + reg_base.mnemonic + "\\n\\t\"\n" + mov_reg_str += "\t\"mov $" + str(hex(two_comp_32(sib_index))) + ", " + reg_index.mnemonic + "\\n\\t\"\n" + + if ((reg_index == ESP) or (reg_base == ESP)): + backup_reg = find_backup_reg([reg_base, reg_index]) + backup_str = "\t\"mov " + ESP.mnemonic + ", " + backup_reg.mnemonic + "\\n\\t\"\n" + restore_str = "\t\"mov " + backup_reg.mnemonic + ", " + ESP.mnemonic + "\\n\\t\"\n" + else: + backup_str = "" + restore_str = "" + + segment_str, segment_chk_str = get_segment_prefix(segment, reg_base, modrm, sib) + + opcode_str = inst.opcode + + disp_str = generate_disp(modrm, disp, sib) + + if (special != None): + comment = special + " Test case " + str(tc_nr) + ": " + else: + comment = "Test case " + str(tc_nr) + ": " + comment += "SEG[" + segment_chk_str + "] " + comment += "INSN: " + inst.name + " SIB(b:" + reg_base.name + " i:" + reg_index.name + " s:" + str(sib_scale) + if (modrm_mod == 0): + comment += "). " + elif (modrm_mod == 1): + comment += " + disp8). " + elif (modrm_mod == 2): + comment += " + disp32). " + + # ignore index value when computing effective address + calc_index = sib_index + calc_base = sib_base + if (reg_index == ESP): + calc_index = 0 + # ignore base + if ((reg_base == EBP) and (modrm_mod == 0)): + calc_base = 0 + + eff_addr = calc_base + calc_index*(1< 127): + disp = 0 + else: + disp = index + index = 0 + sib_base = index - 3*(1<> 8) & 0xff)) + ", " + disp_str += str(hex((index >> 16) & 0xff)) + ", " + disp_str += str(hex((index >> 24) & 0xff)) + + comment = "Special Test case " + str(tc_nr) + ": " + comment += "SEG[" + segment.name + "] " + comment += "INSN: " + inst.name + " (disp32)." + comment += "EFF_ADDR[" + str(hex(index)) + "]." + comment += " disp32[" + str(hex(index)) + "]" + + code = "\t/* " + comment + " */\n" + code += code_start + segment_str + opcode_str + modrm_str + disp_str +code_end + + checkcode += generate_check_code(comment, segment_chk_str, index, inst) + + index += inst.result_bytes + tc_nr += 1 + + # With SIB, index is ignored if such index is ESP. This also means + # that the scale is ignored + for reg_base in SIB_base_MO0: + c, chk , i = generate_code_sib(tc_nr, segment, inst, reg_base, ESP, MODRM_MO0, 0xffff, index, 3, 0, "Special") + index += i + code += c + checkcode += chk + tc_nr += 1 + + disp = 0 + for reg_base in SIB_base_MO1: + c, chk , i = generate_code_sib(tc_nr, segment, inst, reg_base, ESP, MODRM_MO1, 0xffff, index, 3, disp, "Special") + disp += i + code += c + checkcode += chk + tc_nr += 1 + + index = index + disp + disp = 0 + + for reg_base in SIB_base_MO2: + c, chk , i = generate_code_sib(tc_nr, segment, inst, reg_base, ESP, MODRM_MO2, 0xfff, index, 3, disp, "Special") + disp += i + code += c + checkcode += chk + tc_nr += 1 + + index += disp + + # with SIB and base register as EBP and mod = 0, the base register is ignored and a disp32 is used. The default register is DS + disp = 0 + for sib_scale in range (0,4): + new_index = (index >> sib_scale) << sib_scale + disp = index - new_index + new_index >>= sib_scale + for sib_index in SIB_base_MO0: + c, chk, i = generate_code_sib(tc_nr, segment, inst, EBP, sib_index, MODRM_MO0, new_index, 0xeeee, sib_scale, disp, "Special") + code += c + disp += i + checkcode += chk + tc_nr += 1 + + index += disp + + #with SIB, base as EBP and base as ESP, only disp32 is considered in the calculation + c, chk, i = generate_code_sib(tc_nr, segment, inst, EBP, ESP, MODRM_MO0, 0xbbbb, 0xcccc, 3, index, "Special") + code += c + disp += i + checkcode += chk + tc_nr += 1 + + index += i + + return code, checkcode, index, tc_nr + +def generate_unit_tests(segment, inst, start_idx, start_tc_nr): + code = "" + check_code = "" + index = start_idx + testcase_nr = start_tc_nr + + code += "\t /* ==================== Test code for " + inst.name + " ==================== */\n" + code += "\t\"test_umip_" + inst.name + "_" + segment.name + ":\\t\\n\"\n" + + check_code += "\n/* AUTOGENERATED CODE */\n" + if (inst.result_bytes == 2): + check_code += "static void check_tests_" + inst.name + "_" + segment.name + "(const unsigned short expected)\n" + check_code += "{\n" + check_code += "\tunsigned short got;\n\n" + elif (inst.result_bytes == 6): + check_code += "static void check_tests_" + inst.name + "_" + segment.name + "(const struct table_desc *expected)\n" + check_code += "{\n" + check_code += "\tstruct table_desc *got;\n\n" + + run_check_code = "\tcheck_tests_" + inst.name + "_" + segment.name + "(" + inst.expected_val + ");\n" + check_code += "\tprintf(\"=======Results for " + inst.name + " in segment " + segment.name + "=============\\n\");\n" + + for reg in MO0: + c, chk, idx = generate_code(testcase_nr, segment, inst, reg, MODRM_MO0, index, 0) + code += c + check_code += chk + index += idx + testcase_nr += 1 + + # in signed 8-bit displacements, the max value is 255. Thus, correct, by adding + # the remainder to index + start_addr = index + if (start_addr > 127): + remainder = start_addr -127 + index = remainder + start_addr = 127 + else: + index = 0 + + for reg in MO1: + c, chk, idx = generate_code(testcase_nr, segment, inst, reg, MODRM_MO1, index, start_addr) + code += c + check_code += chk + index += idx + testcase_nr += 1 + + start_addr += index + index = 0 + + # Force some indexes to be negative + start_addr += 127 + index -=127 + + for reg in MO2: + c, chk, idx = generate_code(testcase_nr, segment, inst, reg, MODRM_MO2, index, start_addr) + code += c + check_code += chk + index += idx + testcase_nr += 1 + + start_addr += index + + c, chk, start_addr, testcase_nr = generate_unit_tests_sib(segment, inst, start_addr, testcase_nr) + code += c + check_code += chk + + c, chk, idx, tc_nr = generate_special_unit_tests(testcase_nr, segment, inst, start_addr) + code += c + check_code += chk + testcase_nr = tc_nr + start_addr = idx + + code += "\t\"test_umip_" + inst.name + "_" + segment.name + "_end:\\t\\n\"\n" + check_code += "}\n\n" + + return code, check_code, run_check_code, start_addr, testcase_nr + +def generate_tests_all_insts(seg, start_index, start_test_nr): + run_check_code = "" + test_code = "" + check_code = "" + index = start_index + test_nr = start_test_nr + + for inst in INSTS: + tc, chkc, hdr, index, test_nr = generate_unit_tests(seg, inst, index, test_nr) + run_check_code += hdr + test_code += tc + check_code += chkc + + return test_code, check_code, run_check_code, index, test_nr + +def generate_test_cases(test_code, check_code): + header_info = "/* ******************** AUTOGENERATED CODE ******************** */\n" + header_info += "#define SEGMENT_SIZE " + str(SEGMENT_SIZE) + "\n" + header_info += "#define CODE_MEM_SIZE " + str(CODE_MEM_SIZE) + "\n" + header_info += "\n" + header_info += "unsigned char data[SEGMENT_SIZE];\n" + header_info += "unsigned char data_es[SEGMENT_SIZE];\n" + header_info += "unsigned char data_fs[SEGMENT_SIZE];\n" + header_info += "unsigned char data_gs[SEGMENT_SIZE];\n" + header_info += "unsigned char stack[SEGMENT_SIZE];\n" + header_info += "\n" + header_info += "void check_results(void);\n" + header_info += "\n" + + check_code += "/* ******************** AUTOGENERATED CODE ******************** */\n" + check_code += "#include \n" + check_code += "#include \"test_umip_ldt_32.h\"\n\n" + check_code += "#include \"umip_test_defs.h\"\n\n" + check_code += "\n" + + run_check_code = "" + + test_code += "\tasm(\n" + test_code += "\t /* ******************** AUTOGENERATED CODE ******************** */\n" + test_code += "\t\".pushsection .rodata\\n\\t\"\n" + test_code += "\t\"test_umip:\\t\\n\"\n" + test_code += "\t/* setup stack */\n" + test_code += "\t\"mov $" + str(SEGMENT_SIZE) + ", %esp\\n\\t\"\n" + test_code += "\t\"mov %ecx, %ss\\n\\t\"\n" + test_code += "\t/* save old cs as passed by us before retf'ing here */\n" + test_code += "\t\"push %edx\\n\\t\"\n" + test_code += "\t/* save old esp as passed by us before retf'ing here */\n" + test_code += "\t\"push %eax\\n\\t\"\n" + test_code += "\t/* save old ss as passed by us before retf'ing here */\n" + test_code += "\t\"push %ebx\\n\\t\"\n" + + + test_nr = 0 + + for seg in DATA_SEGS: + index = 0 + tc, chkc, rchk, index, test_nr = generate_tests_all_insts(seg, index, test_nr) + run_check_code += rchk + test_code += tc + check_code += chkc + + test_code += "\t/* preparing to return */\n" + test_code += "\t/* restore ss */\n" + test_code += "\t\"pop %ebx\\n\\t\"\n" + test_code += "\t/* restore esp */\n" + test_code += "\t\"pop %eax\\n\\t\"\n" + test_code += "\t/* setting return IP, CS is already in stack */\n" + test_code += "\t\"push $finish_testing\\n\\t\"\n" + test_code += "\t\"retf\\n\\t\"\n" + test_code += "\t\"test_umip_end:\\t\\n\"\n" + test_code += "\t\".popsection\\n\\t\"\n" + test_code += "\t);\n" + + check_code += "\n" + check_code += "void check_results(void)\n" + check_code += "{\n" + check_code += run_check_code + check_code += "}\n" + check_code += "\n" + + return test_code, check_code, header_info, index + +def write_test_files(): + check_code = "" + test_code = "/* This is an autogenerated file. If you intend to debug, better to debug the generating script. */\n\n" + test_code += "\n" + test_code, check_code, check_code_hdr, global_index = generate_test_cases(test_code, check_code) + + fcheck_code = open("test_umip_ldt_32.c", "w") + fheader = open("test_umip_ldt_32.h", "w") + ftest_code = open("test_umip_code_32.h", "w") + ftest_code.writelines(test_code) + fheader.writelines(check_code_hdr) + fcheck_code.writelines(check_code) + ftest_code.close() + fcheck_code.close() + fheader.close() + +write_test_files() diff --git a/meta-luv/recipes-core/umip/files/umip_test_gen_64.py b/meta-luv/recipes-core/umip/files/umip_test_gen_64.py new file mode 100644 index 00000000000..f53cc9a543a --- /dev/null +++ b/meta-luv/recipes-core/umip/files/umip_test_gen_64.py @@ -0,0 +1,655 @@ +""" Test-case generator for UMIP +Copyright Intel Corporation 2017 + +This file generates opcdoes for all (well, most) of the combinations +of memory operands for the UMIP-protected instructions. This includes +all the ModRM values and segments. + +TODO: Add support for 16-bit address encodings +TODO: Add support for SiB address encodings""" + +MODRM_MO0 = 0 +MODRM_MO1 = 1 +MODRM_MO2 = 2 +MODRM_MO3 = 3 + +#TODO: need to be based on the number test cases +SEGMENT_SIZE = 1048576 +CODE_MEM_SIZE = 1048576 + +class Instruction: + def __init__(self, name, opcode, modrm_reg, result_bytes, expected_val): + self.name = name + self.opcode = opcode + self.modrm_reg = modrm_reg + self.result_bytes = result_bytes + self.expected_val = expected_val + + +class Register: + def __init__(self, mnemonic, modrm_rm, name, rex_x, rex_b): + self.mnemonic = mnemonic + self.modrm_rm = modrm_rm + self.name = name + self.rex_x = rex_x + self.rex_b = rex_b + +class Segment: + def __init__(self, name, prefix, array): + self.name = name + self.prefix = prefix + self.array = array + + +RAX = Register("%rax", 0, "rax", 0, 0) +RCX = Register("%rcx", 1, "rcx", 0, 0) +RDX = Register("%rdx", 2, "rdx", 0, 0) +RBX = Register("%rbx", 3, "rbx", 0, 0) +RSP = Register("%rsp", 4, "rsp", 0, 0) +RBP = Register("%rbp", 5, "rbp", 0, 0) +RSI = Register("%rsi", 6, "rsi", 0, 0) +RDI = Register("%rdi", 7, "rdi", 0, 0) +R8 = Register("%r8", 0, "r8", 0x42, 0x41) +R9 = Register("%r9", 1, "r9", 0x42, 0x41) +R10 = Register("%r10", 2, "r10", 0x42, 0x41) +R11 = Register("%r11", 3, "r11", 0x42, 0x41) +R12 = Register("%r12", 4, "r12", 0x42, 0x41) +R13 = Register("%r13", 5, "r13", 0x42, 0x41) +R14 = Register("%r14", 6, "r14", 0x42, 0x41) +R15 = Register("%r15", 7, "r15", 0x42, 0x41) + + +SMSW = Instruction("smsw", "0xf, 0x1", 4, 2, "expected_msw") +SLDT = Instruction("sldt", "0xf, 0x0", 0, 2, "expected_ldt") +STR = Instruction("str", "0xf, 0x0", 1, 2, "expected_tr") +SIDT = Instruction("sidt", "0xf, 0x1", 1, 6, "&expected_idt") +SGDT = Instruction("sgdt", "0xf, 0x1", 0, 6, "&expected_gdt") + +CS = Segment("cs", "", "code") +DS = Segment("ds", "", "data") +SS = Segment("ss", "", "stack") +ES = Segment("es", "0x26", "data_es") +FS = Segment("fs", "0x64", "data_fs") +GS = Segment("gs", "0x65", "data_gs") + +DATA_SEGS = [ FS, GS ] +INSTS = [SMSW, SLDT, STR, SGDT, SIDT ] + +MO0 = [ RAX, RCX, RDX, RBX, RSI, RDI, R8, R9, R10, R11, R14, R15 ] +MO1 = [ RAX, RCX, RDX, RBX, RBP, RSI, RDI, R8, R9, R10, R11, R13, R14, R15 ] +MO2 = MO1 + +SIB_index = [ RAX, RCX, RDX, RBX, RBP, RSI, RDI, R8, R9, R10, R11, R12, R13, R14, R15 ] +SIB_base_MO0 = [ RAX, RCX, RDX, RBX, RSI, RDI, R8, R9, R10, R11, R12, R14, R15 ] +SIB_base_MO1 = [ RAX, RCX, RDX, RBX, RBP, RSI, RDI, R8, R9, R10, R11, R12, R13, R14, R15 ] +SIB_base_MO2 = SIB_base_MO1 + +def two_comp_32(val): + if (val >= 0): + return val + else: + return ((abs(val) ^ 0xffffffff) + 1) & 0xffffffff + +def two_comp_64(val): + if (val >= 0): + return val + else: + return ((abs(val) ^ 0xffffffffffffffff) + 1) & 0xffffffffffffffff + +def two_comp_8(val): + if (val >= 0): + return val + else: + return ((abs(val) ^ 0xff) + 1) & 0xff + +def my_hex(val): + hxval = hex(val) + if (hxval[-1] == 'L'): + return hxval[:-1] + else: + return hxval + +def find_backup_reg(do_not_use): + regs = [ RAX, RCX, RDX, RBX, RSP, RBP, RSI, RDI, R8, R9, R10, R11, R12, R13, R14, R15] + for u in do_not_use: + regs.remove(u) + return regs[0] + +def get_segment_prefix(segment, register, modrm, sib=0): + """ default segments """ + if (segment.prefix == ""): + segment_str = "" + if ((register == RBP) or (register == RSP)): + # This is for a pure disp32, no register involved. Thus, default is data + if (((modrm >> 6) == 0) and (modrm & 0x7) == 5): + segment_chk_str = "data" + # If using a sib byte and the base is RBP, use DS as the base is ignored + elif (((modrm >> 6) == 0) and ((modrm & 0x7) == 4) and ((sib & 0x7) == 5)): + segment_chk_str = "data" + else: + segment_chk_str = "stack" + else: + segment_chk_str = "data" + else: + segment_str = segment.prefix + ", " + segment_chk_str = segment.array + + return segment_str, segment_chk_str + +def generate_check_code(comment, segment_chk_str, address, inst): + # TODO: Use an enum here + if (inst.result_bytes == 2): + checkcode = "\tgot = *(unsigned short *)(" + segment_chk_str +" + " + str(hex(address)) + ");\n" + checkcode += "\tprintf(\"%s " + comment + ". Got:[0x%x]Exp[0x%x]\\n\",\n" + checkcode += "\t got==expected ? TEST_PASS : TEST_FAIL, got, expected);\n" + elif (inst.result_bytes == 6): + checkcode = "\tgot = (struct table_desc *)(" + segment_chk_str +" + " + str(hex(address)) + ");\n" + checkcode += "\tprintf(\"%s " + comment + ". Got:Base[0x%lx]Limit[0x%x]ExpBase[0x%lx]Limit[0x%x]\\n\",\n" + checkcode += "\t ((got->base == expected->base) && (got->limit == expected->limit)) ? TEST_PASS : TEST_FAIL, got->base, got->limit, expected->base, expected->limit);\n" + return checkcode + +def generate_disp(modrm, disp, sib=0): + modrm_mod = modrm >> 6 + if (modrm_mod == 0): + + # if a sib byte is used, disp 32 is used + if (((sib &7) == 5) and (modrm & 7) == 4): + disp_2comp = two_comp_32(disp) + disp_str = ", " + str(hex(disp_2comp & 0xff)) + ", " + disp_str += str(hex((disp_2comp >> 8) & 0xff)) + ", " + disp_str += str(hex((disp_2comp >> 16) & 0xff)) + ", " + disp_str += str(hex((disp_2comp >> 24) & 0xff)) + else: + disp_str = "" + elif (modrm_mod == 1): + disp_2comp = two_comp_8(disp) + disp_str = ", " + str(hex(disp_2comp)) + elif (modrm_mod == 2): + disp_2comp = two_comp_32(disp) + disp_str = ", " + str(hex(disp_2comp & 0xff)) + ", " + disp_str += str(hex((disp_2comp >> 8) & 0xff)) + ", " + disp_str += str(hex((disp_2comp >> 16) & 0xff)) + ", " + disp_str += str(hex((disp_2comp >> 24) & 0xff)) + + return disp_str + +def generate_code(tc_nr, segment, inst, register, modrm_mod, index, disp): + code_start = "\t\".byte " + code_end = "\\n\\t\"\n" + + modrm = (modrm_mod << 6) | (inst.modrm_reg << 3) | register.modrm_rm + modrm_str = ", " + str(hex(modrm)) + mov_reg_str = "\t\"mov $" + str(my_hex(two_comp_64(index))) + ", " + register.mnemonic + "\\n\\t\"\n" + + segment_str, segment_chk_str = get_segment_prefix(segment, register, modrm) + + opcode_str = inst.opcode + + if (register.rex_b != 0): + rex_b_str = hex(register.rex_b) + ", " + else: + rex_b_str = "" + + disp_str = generate_disp(modrm, disp) + + comment = "Test case " + str(tc_nr) + ": " + comment += "SEG[" + segment_chk_str + "] " + comment += "INSN: " + inst.name + "(" + register.name + if (modrm_mod == 0): + comment += "). " + elif (modrm_mod == 1): + comment += " + disp8). " + elif (modrm_mod == 2): + comment += " + disp32). " + comment += "EFF_ADDR[" + str(hex(index + disp)) + "]. " + comment += register.name + "[" + str(hex(index)) + "] " + if (modrm_mod == 0): + comment += "" + elif (modrm_mod == 1): + comment += "disp8[" + str(hex(disp)) + "]" + elif (modrm_mod == 2): + comment += "disp32[" + str(hex(disp)) + "]" + + code = "\t/* " + comment + " */\n" + code += mov_reg_str + code += code_start + segment_str + rex_b_str + opcode_str + modrm_str + disp_str + code_end + + checkcode = generate_check_code(comment, segment_chk_str, index + disp, inst) + + return code, checkcode, inst.result_bytes + +def generate_code_sib(tc_nr, segment, inst, reg_base, reg_index, modrm_mod, sib_index, sib_base, sib_scale, disp, special=None): + code_start = "\t\".byte " + code_end = "\\n\\t\"\n" + + modrm = (modrm_mod << 6) | (inst.modrm_reg << 3) | RSP.modrm_rm + sib = (sib_scale << 6) | (reg_index.modrm_rm << 3) | reg_base.modrm_rm + modrm_str = ", " + str(hex(modrm)) + sib_str = ", " + str(hex(sib)) + mov_reg_str = "\t\"mov $" + str(my_hex(two_comp_64(sib_base))) + ", " + reg_base.mnemonic + "\\n\\t\"\n" + mov_reg_str += "\t\"mov $" + str(my_hex(two_comp_64(sib_index))) + ", " + reg_index.mnemonic + "\\n\\t\"\n" + + if ((reg_index == RSP) or (reg_base == RSP)): + backup_reg = find_backup_reg([reg_base, reg_index]) + backup_str = "\t\"mov " + RSP.mnemonic + ", " + backup_reg.mnemonic + "\\n\\t\"\n" + restore_str = "\t\"mov " + backup_reg.mnemonic + ", " + RSP.mnemonic + "\\n\\t\"\n" + else: + backup_str = "" + restore_str = "" + + segment_str, segment_chk_str = get_segment_prefix(segment, reg_base, modrm, sib) + + opcode_str = inst.opcode + + if ((reg_index.rex_x != 0) or (reg_base.rex_b != 0)): + rex_b_str = hex(reg_index.rex_x | reg_base.rex_b) + ", " + else: + rex_b_str = "" + + disp_str = generate_disp(modrm, disp, sib) + + if (special != None): + comment = special + " Test case " + str(tc_nr) + ": " + else: + comment = "Test case " + str(tc_nr) + ": " + comment += "SEG[" + segment_chk_str + "] " + comment += "INSN: " + inst.name + " SIB(b:" + reg_base.name + " i:" + reg_index.name + " s:" + str(sib_scale) + if (modrm_mod == 0): + comment += "). " + elif (modrm_mod == 1): + comment += " + disp8). " + elif (modrm_mod == 2): + comment += " + disp32). " + + # ignore index value when computing effective address + calc_index = sib_index + calc_base = sib_base + if (reg_index == RSP): + calc_index = 0 + # ignore base + if ((reg_base == RBP) and (modrm_mod == 0)): + calc_base = 0 + + eff_addr = calc_base + calc_index*(1< 127): + disp = 0 + else: + disp = index + index = 0 + sib_base = index - 3*(1<> 8) & 0xff)) + ", " + disp_str += str(hex((index >> 16) & 0xff)) + ", " + disp_str += str(hex((index >> 24) & 0xff)) + + comment = "Ricardin Special Test case " + str(tc_nr) + ": " + comment += "SEG[" + segment.name + "] " + comment += "INSN: " + inst.name + " (disp32)." + comment += "EFF_ADDR[" + str(hex(index)) + "]." + comment += " disp32[" + str(hex(index)) + "]" + + code = "\t/* " + comment + " */\n" + code += "\t\"jmp 1f:\\n\\t\"\n" + code += "\t\"1f:\\n\\t\"\n" + code += code_start + segment_str + opcode_str + modrm_str + disp_str +code_end + + checkcode += generate_check_code(comment, segment_chk_str, index, inst) + + index += inst.result_bytes + tc_nr += 1 + + # With SIB, index is ignored if such index is RSP. This also means + # that the scale is ignored + for reg_base in SIB_base_MO0: + c, chk , i = generate_code_sib(tc_nr, segment, inst, reg_base, RSP, MODRM_MO0, 0xffff, index, 3, 0, "Special") + index += i + code += c + checkcode += chk + tc_nr += 1 + + disp = 0 + for reg_base in SIB_base_MO1: + c, chk , i = generate_code_sib(tc_nr, segment, inst, reg_base, RSP, MODRM_MO1, 0xffff, index, 3, disp, "Special") + disp += i + code += c + checkcode += chk + tc_nr += 1 + + index = index + disp + disp = 0 + + for reg_base in SIB_base_MO2: + c, chk , i = generate_code_sib(tc_nr, segment, inst, reg_base, RSP, MODRM_MO2, 0xfff, index, 3, disp, "Special") + disp += i + code += c + checkcode += chk + tc_nr += 1 + + index += disp + + # with SIB and base register as RBP and mod = 0, the base register is ignored and a disp32 is used. The default register is DS + disp = 0 + for sib_scale in range (0,4): + new_index = (index >> sib_scale) << sib_scale + disp = index - new_index + new_index >>= sib_scale + for sib_index in SIB_base_MO0: + c, chk, i = generate_code_sib(tc_nr, segment, inst, RBP, sib_index, MODRM_MO0, new_index, 0xeeee, sib_scale, disp, "Special") + code += c + disp += i + checkcode += chk + tc_nr += 1 + + index += disp + + # with SIB, base as RBP and base as RSP, only disp32 is considered in the calculation + # in 64-bit, the disp is added to RIP + c, chk, i = generate_code_sib(tc_nr, segment, inst, RBP, RSP, MODRM_MO0, 0xbbbb, 0xcccc, 3, index, "Special") + code += c + disp += i + checkcode += chk + tc_nr += 1 + + index += i + + return code, checkcode, index, tc_nr + +def generate_unit_tests(segment, inst, start_idx, start_tc_nr): + code = "" + check_code = "" + index = start_idx + testcase_nr = start_tc_nr + + code += "\t /* ==================== Test code for " + inst.name + " ==================== */\n" + code += "\t\"test_umip_" + inst.name + "_" + segment.name + ":\\t\\n\"\n" + + check_code += "\n/* AUTOGENERATED CODE */\n" + if (inst.result_bytes == 2): + check_code += "static void check_tests_" + inst.name + "_" + segment.name + "(const unsigned short expected)\n" + check_code += "{\n" + check_code += "\tunsigned short got;\n\n" + elif (inst.result_bytes == 6): + check_code += "static void check_tests_" + inst.name + "_" + segment.name + "(const struct table_desc *expected)\n" + check_code += "{\n" + check_code += "\tstruct table_desc *got;\n\n" + + run_check_code = "\tcheck_tests_" + inst.name + "_" + segment.name + "(" + inst.expected_val + ");\n" + check_code += "\tprintf(\"=======Results for " + inst.name + " in segment " + segment.name + "=============\\n\");\n" + + for reg in MO0: + c, chk, idx = generate_code(testcase_nr, segment, inst, reg, MODRM_MO0, index, 0) + code += c + check_code += chk + index += idx + testcase_nr += 1 + + # in signed 8-bit displacements, the max value is 255. Thus, correct, by adding + # the remainder to index + start_addr = index + if (start_addr > 127): + remainder = start_addr -127 + index = remainder + start_addr = 127 + else: + index = 0 + + for reg in MO1: + c, chk, idx = generate_code(testcase_nr, segment, inst, reg, MODRM_MO1, index, start_addr) + code += c + check_code += chk + index += idx + testcase_nr += 1 + + start_addr += index + index = 0 + + # Force some indexes to be negative + start_addr += 127 + index -=127 + + for reg in MO2: + c, chk, idx = generate_code(testcase_nr, segment, inst, reg, MODRM_MO2, index, start_addr) + code += c + check_code += chk + index += idx + testcase_nr += 1 + + start_addr += index + + c, chk, start_addr, testcase_nr = generate_unit_tests_sib(segment, inst, start_addr, testcase_nr) + code += c + check_code += chk + + c, chk, idx, tc_nr = generate_special_unit_tests(testcase_nr, segment, inst, start_addr) + code += c + check_code += chk + testcase_nr = tc_nr + start_addr = idx + + code += "\t\"test_umip_" + inst.name + "_" + segment.name + "_end:\\t\\n\"\n" + check_code += "}\n\n" + + return code, check_code, run_check_code, start_addr, testcase_nr + +def generate_tests_all_insts(seg, start_index, start_test_nr): + run_check_code = "" + test_code = "" + check_code = "" + index = start_index + test_nr = start_test_nr + + for inst in INSTS: + tc, chkc, hdr, index, test_nr = generate_unit_tests(seg, inst, index, test_nr) + run_check_code += hdr + test_code += tc + check_code += chkc + + return test_code, check_code, run_check_code, index, test_nr + +def generate_test_cases(test_code, check_code): + header_info = "/* ******************** AUTOGENERATED CODE ******************** */\n" + header_info += "#define SEGMENT_SIZE " + str(SEGMENT_SIZE) + "\n" + header_info += "#define CODE_MEM_SIZE " + str(CODE_MEM_SIZE) + "\n" + header_info += "\n" + header_info += "unsigned char data_fs[SEGMENT_SIZE];\n" + header_info += "unsigned char data_gs[SEGMENT_SIZE];\n" + header_info += "\n" + header_info += "void check_results(void);\n" + header_info += "\n" + + check_code += "/* ******************** AUTOGENERATED CODE ******************** */\n" + check_code += "#include \n" + check_code += "#include \"test_umip_ldt_64.h\"\n\n" + check_code += "#include \"umip_test_defs.h\"\n\n" + check_code += "\n" + + run_check_code = "" + + test_code += "\tasm(\n" + test_code += "\t /* ******************** AUTOGENERATED CODE ******************** */\n" + test_code += "\t\".pushsection .rodata\\n\\t\"\n" + test_code += "\t\"test_umip:\\t\\n\"\n" + + + test_nr = 0 + + index = 0 + for seg in DATA_SEGS: + index = 0 + tc, chkc, rchk, index, test_nr = generate_tests_all_insts(seg, index, test_nr) + run_check_code += rchk + test_code += tc + check_code += chkc + + #test_code += "\t\"jmp $finish_testing\\n\\t\"\n" + test_code += "\t\"ret\\n\\t\"\n" + test_code += "\t\"test_umip_end:\\t\\n\"\n" + test_code += "\t\".popsection\\n\\t\"\n" + test_code += "\t);\n" + + check_code += "\n" + check_code += "void check_results(void)\n" + check_code += "{\n" + check_code += run_check_code + check_code += "}\n" + check_code += "\n" + + return test_code, check_code, header_info, index + +def write_test_files(): + check_code = "" + test_code = "/* This is an autogenerated file. If you intend to debug, better to debug the generating script. */\n\n" + test_code += "\n" + test_code, check_code, check_code_hdr, global_index = generate_test_cases(test_code, check_code) + + fcheck_code = open("test_umip_ldt_64.c", "w") + fheader = open("test_umip_ldt_64.h", "w") + ftest_code = open("test_umip_code_64.h", "w") + ftest_code.writelines(test_code) + fheader.writelines(check_code_hdr) + fcheck_code.writelines(check_code) + ftest_code.close() + fcheck_code.close() + fheader.close() + +write_test_files() diff --git a/meta-luv/recipes-core/umip/umip-tests_0.1.bb b/meta-luv/recipes-core/umip/umip-tests_0.1.bb new file mode 100644 index 00000000000..0b41db5b23a --- /dev/null +++ b/meta-luv/recipes-core/umip/umip-tests_0.1.bb @@ -0,0 +1,60 @@ +SUMMARY = "Tests for Intel User-Mode Instruction Prevention" +DESCRIPTION = "Use the instructions covered by UMIP and make sure they are handled correctly" +HOMEPAGE = "https://www.github.com/ricardon" +LICENSE = "GPLv2" +LIC_FILES_CHKSUM = "file://COPYING;md5=751419260aa954499f7abaabaa882bbe" + + +SRC_URI="file://umip_test_defs.h \ + file://umip_test.c \ + file://umip_test2.c \ + file://normal_pf.c \ + file://COPYING \ + file://Makefile \ + file://umip_ldt_32.c \ + file://umip_test_gen_32.py \ + file://umip_ldt_16.c \ + file://umip_test_gen_16.py \ + file://umip_ldt_64.c \ + file://umip_test_gen_64.py \ + file://UMIP_README \ + " + + +do_configure[noexec] = "1" + +EXTRA_OEMAKE += " ${TARGET_ARCH}" + +S = "${WORKDIR}" + +do_compile() { + oe_runmake +} + +do_install() { + install -d ${D}${bindir} + install -m 744 ${WORKDIR}/UMIP_README ${D}${bindir} + if [ "${TARGET_ARCH}" = "x86_64" ]; then + install -m 755 ${WORKDIR}/umip_test_64 ${D}${bindir} + install -m 755 ${WORKDIR}/umip_pf_64 ${D}${bindir} + install -m 755 ${WORKDIR}/umip_test2_64 ${D}${bindir} + install -m 755 ${WORKDIR}/umip_ldt_64 ${D}${bindir} + fi + if [ "${TARGET_ARCH}" = "i586" ]; then + install -m 755 ${WORKDIR}/umip_test_32 ${D}${bindir} + install -m 755 ${WORKDIR}/umip_pf_32 ${D}${bindir} + install -m 755 ${WORKDIR}/umip_test2_32 ${D}${bindir} + install -m 755 ${WORKDIR}/umip_ldt_32 ${D}${bindir} + install -m 755 ${WORKDIR}/umip_ldt_16 ${D}${bindir} + fi + if [ "${TARGET_ARCH}" = "i686" ]; then + install -m 755 ${WORKDIR}/umip_test_32 ${D}${bindir} + install -m 755 ${WORKDIR}/umip_pf_32 ${D}${bindir} + install -m 755 ${WORKDIR}/umip_test2_32 ${D}${bindir} + install -m 755 ${WORKDIR}/umip_ldt_32 ${D}${bindir} + install -m 755 ${WORKDIR}/umip_ldt_16 ${D}${bindir} + fi + +} + + From c09d0be80b5f362ab115a3e6a420b843675fd274 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Tue, 24 Jan 2017 15:27:30 -0800 Subject: [PATCH 004/109] luv-yocto-efi-test: add kernel selftests for UMIP Ideally, we might want to add these patches in the vm86-test recipe. Unfortunately, such recipe uses the kernel-source directory to build its artifacts. Thus, we have no option but to apply the patches here. The first patch contains tests that are representative of UMIP use cases. The second patch test UMIP emulation extensively by exercising all the possible combinations of operands and displacement in 16-bit addressing encodings. Signed-off-by: Ricardo Neri --- ...d-tests-for-User-Mode-Instruction-Pr.patch | 116 ++++++ ...d-more-tests-for-User-Mode-Instructi.patch | 394 ++++++++++++++++++ .../linux/linux-yocto-efi-test_4.12.bb | 2 + 3 files changed, 512 insertions(+) create mode 100644 meta-luv/recipes-kernel/linux/linux-yocto-efi-test/0001-selftests-x86-Add-tests-for-User-Mode-Instruction-Pr.patch create mode 100644 meta-luv/recipes-kernel/linux/linux-yocto-efi-test/0002-selftests-x86-Add-more-tests-for-User-Mode-Instructi.patch diff --git a/meta-luv/recipes-kernel/linux/linux-yocto-efi-test/0001-selftests-x86-Add-tests-for-User-Mode-Instruction-Pr.patch b/meta-luv/recipes-kernel/linux/linux-yocto-efi-test/0001-selftests-x86-Add-tests-for-User-Mode-Instruction-Pr.patch new file mode 100644 index 00000000000..7efc6d9f785 --- /dev/null +++ b/meta-luv/recipes-kernel/linux/linux-yocto-efi-test/0001-selftests-x86-Add-tests-for-User-Mode-Instruction-Pr.patch @@ -0,0 +1,116 @@ +From 1a0461d77254759e3990e06171f9fdcfdbad3e38 Mon Sep 17 00:00:00 2001 +From: Ricardo Neri +Date: Tue, 9 Aug 2016 12:48:24 -0700 +Subject: [PATCH 10/11] selftests/x86: Add tests for User-Mode Instruction + Prevention + +Certain user space programs that run on virtual-8086 mode may utilize +instructions protected by the User-Mode Instruction Prevention (UMIP) +security feature present in new Intel processors: SGDT, SIDT and SMSW. In +such a case, a general protection fault is issued if UMIP is enabled. When +such a fault happens, the kernel catches it and emulates the results of +these instructions with dummy values. The purpose of this new +test is to verify whether the impacted instructions can be executed without +causing such #GP. If no #GP exceptions occur, we expect to exit virtual- +8086 mode from INT 0x80. + +The instructions protected by UMIP are executed in representative use +cases: + a) the memory address of the result is given in the form of a displacement + from the base of the data segment + b) the memory address of the result is given in a general purpose register + c) the result is stored directly in a general purpose register. + +Unfortunately, it is not possible to check the results against a set of +expected values because no emulation will occur in systems that do not have +the UMIP feature. Instead, results are printed for verification. + +Cc: Andy Lutomirski +Cc: Andrew Morton +Cc: Borislav Petkov +Cc: Brian Gerst +Cc: Chen Yucong +Cc: Chris Metcalf +Cc: Dave Hansen +Cc: Fenghua Yu +Cc: Huang Rui +Cc: Jiri Slaby +Cc: Jonathan Corbet +Cc: Michael S. Tsirkin +Cc: Paul Gortmaker +Cc: Peter Zijlstra +Cc: Ravi V. Shankar +Cc: Shuah Khan +Cc: Vlastimil Babka +Signed-off-by: Ricardo Neri +--- + tools/testing/selftests/x86/entry_from_vm86.c | 39 ++++++++++++++++++++++++++- + 1 file changed, 38 insertions(+), 1 deletion(-) + +diff --git a/tools/testing/selftests/x86/entry_from_vm86.c b/tools/testing/selftests/x86/entry_from_vm86.c +index d075ea0..377b773 100644 +--- a/tools/testing/selftests/x86/entry_from_vm86.c ++++ b/tools/testing/selftests/x86/entry_from_vm86.c +@@ -95,6 +95,22 @@ asm ( + "int3\n\t" + "vmcode_int80:\n\t" + "int $0x80\n\t" ++ "umip:\n\t" ++ /* addressing via displacements */ ++ "smsw (2052)\n\t" ++ "sidt (2054)\n\t" ++ "sgdt (2060)\n\t" ++ /* addressing via registers */ ++ "mov $2066, %bx\n\t" ++ "smsw (%bx)\n\t" ++ "mov $2068, %bx\n\t" ++ "sidt (%bx)\n\t" ++ "mov $2074, %bx\n\t" ++ "sgdt (%bx)\n\t" ++ /* register operands, only for smsw */ ++ "smsw %ax\n\t" ++ "mov %ax, (2080)\n\t" ++ "int $0x80\n\t" + ".size vmcode, . - vmcode\n\t" + "end_vmcode:\n\t" + ".code32\n\t" +@@ -103,7 +119,7 @@ asm ( + + extern unsigned char vmcode[], end_vmcode[]; + extern unsigned char vmcode_bound[], vmcode_sysenter[], vmcode_syscall[], +- vmcode_sti[], vmcode_int3[], vmcode_int80[]; ++ vmcode_sti[], vmcode_int3[], vmcode_int80[], umip[]; + + /* Returns false if the test was skipped. */ + static bool do_test(struct vm86plus_struct *v86, unsigned long eip, +@@ -218,6 +234,27 @@ int main(void) + v86.regs.eax = (unsigned int)-1; + do_test(&v86, vmcode_int80 - vmcode, VM86_INTx, 0x80, "int80"); + ++ /* UMIP -- should exit with INTx 0x80 unless UMIP was not disabled */ ++ do_test(&v86, umip - vmcode, VM86_INTx, 0x80, "UMIP tests"); ++ printf("[INFO]\tResults of UMIP-protected instructions via displacements:\n"); ++ printf("[INFO]\tSMSW:[0x%04x]\n", *(unsigned short *)(addr + 2052)); ++ printf("[INFO]\tSIDT: limit[0x%04x]base[0x%08lx]\n", ++ *(unsigned short *)(addr + 2054), ++ *(unsigned long *)(addr + 2056)); ++ printf("[INFO]\tSGDT: limit[0x%04x]base[0x%08lx]\n", ++ *(unsigned short *)(addr + 2060), ++ *(unsigned long *)(addr + 2062)); ++ printf("[INFO]\tResults of UMIP-protected instructions via addressing in registers:\n"); ++ printf("[INFO]\tSMSW:[0x%04x]\n", *(unsigned short *)(addr + 2066)); ++ printf("[INFO]\tSIDT: limit[0x%04x]base[0x%08lx]\n", ++ *(unsigned short *)(addr + 2068), ++ *(unsigned long *)(addr + 2070)); ++ printf("[INFO]\tSGDT: limit[0x%04x]base[0x%08lx]\n", ++ *(unsigned short *)(addr + 2074), ++ *(unsigned long *)(addr + 2076)); ++ printf("[INFO]\tResults of SMSW via register operands:\n"); ++ printf("[INFO]\tSMSW:[0x%04x]\n", *(unsigned short *)(addr + 2080)); ++ + /* Execute a null pointer */ + v86.regs.cs = 0; + v86.regs.ss = 0; +-- +2.9.3 + diff --git a/meta-luv/recipes-kernel/linux/linux-yocto-efi-test/0002-selftests-x86-Add-more-tests-for-User-Mode-Instructi.patch b/meta-luv/recipes-kernel/linux/linux-yocto-efi-test/0002-selftests-x86-Add-more-tests-for-User-Mode-Instructi.patch new file mode 100644 index 00000000000..094e96bb777 --- /dev/null +++ b/meta-luv/recipes-kernel/linux/linux-yocto-efi-test/0002-selftests-x86-Add-more-tests-for-User-Mode-Instructi.patch @@ -0,0 +1,394 @@ +From a47399192fed6e0c9cb8ae3a3af1bb158636e0fb Mon Sep 17 00:00:00 2001 +From: Ricardo Neri +Date: Mon, 23 Jan 2017 18:31:35 -0800 +Subject: [PATCH 11/11] selftests/x86: Add more tests for User-Mode Instruction + Prevention + +Add many tests the UMIP emulation done in the kernel. This tests all +the possible combinations in 16-bit addressing mode: address in 1 or 2 +registers, 0, 1, 2-byte displacements as well as register operands. + +Signed-off-by: Ricardo Neri +--- + tools/testing/selftests/x86/entry_from_vm86.c | 342 +++++++++++++++++++++++++- + 1 file changed, 338 insertions(+), 4 deletions(-) + +diff --git a/tools/testing/selftests/x86/entry_from_vm86.c b/tools/testing/selftests/x86/entry_from_vm86.c +index 377b773..c7bf946 100644 +--- a/tools/testing/selftests/x86/entry_from_vm86.c ++++ b/tools/testing/selftests/x86/entry_from_vm86.c +@@ -100,16 +100,162 @@ asm ( + "smsw (2052)\n\t" + "sidt (2054)\n\t" + "sgdt (2060)\n\t" +- /* addressing via registers */ ++ /* addressing via registers, no disp - bx */ + "mov $2066, %bx\n\t" + "smsw (%bx)\n\t" + "mov $2068, %bx\n\t" + "sidt (%bx)\n\t" + "mov $2074, %bx\n\t" + "sgdt (%bx)\n\t" ++ /* addressing via registers, no disp - si */ ++ "mov $2080, %si\n\t" ++ "smsw (%si)\n\t" ++ "mov $2082, %si\n\t" ++ "sidt (%si)\n\t" ++ "mov $2088, %si\n\t" ++ "sgdt (%si)\n\t" ++ /* addressing via registers, no disp - di */ ++ "mov $2094, %di\n\t" ++ "smsw (%di)\n\t" ++ "mov $2096, %di\n\t" ++ "sidt (%di)\n\t" ++ "mov $2102, %di\n\t" ++ "sgdt (%di)\n\t" ++ /* addressing via registers, no disp - bx + si */ ++ "mov $2000, %bx\n\t" ++ "mov $108, %si\n\t" ++ ".byte 0x0f, 0x01, 0x20\n\t" /* smsw (%bx+%si) */ ++ "mov $2000, %bx\n\t" ++ "mov $110, %si\n\t" ++ ".byte 0x0f, 0x01, 0x08\n\t" /* sidt (%bx+%si) */ ++ "mov $2000, %bx\n\t" ++ "mov $116, %si\n\t" ++ ".byte 0x0f, 0x01, 0x00\n\t" /* sgdt (%bx+%si) */ ++ /* addressing via registers, no disp - bx + di */ ++ "mov $2000, %bx\n\t" ++ "mov $122, %di\n\t" ++ ".byte 0x0f, 0x01, 0x21\n\t" /* smsw (%bx+%di) */ ++ "mov $2000, %bx\n\t" ++ "mov $124, %di\n\t" ++ ".byte 0x0f, 0x01, 0x09\n\t" /* sidt (%bx+%di) */ ++ "mov $2000, %bx\n\t" ++ "mov $130, %di\n\t" ++ ".byte 0x0f, 0x01, 0x01\n\t" /* sgdt (%bx+%di) */ ++ /* addressing via registers, no disp - bp + si */ ++ "mov $2000, %bp\n\t" ++ "mov $136, %si\n\t" ++ ".byte 0x0f, 0x01, 0x22\n\t" /* smsw (%bp+%si) */ ++ "mov $2000, %bp\n\t" ++ "mov $138, %si\n\t" ++ ".byte 0x0f, 0x01, 0x0a\n\t" /* sidt (%bp+%si) */ ++ "mov $2000, %bp\n\t" ++ "mov $144, %si\n\t" ++ ".byte 0x0f, 0x01, 0x02\n\t" /* sgdt (%bp+%si) */ ++ /* addressing via registers, no disp - bp + di */ ++ "mov $2000, %bp\n\t" ++ "mov $150, %di\n\t" ++ ".byte 0x0f, 0x01, 0x23\n\t" /* smsw (%bp+%di) */ ++ "mov $2000, %bp\n\t" ++ "mov $152, %di\n\t" ++ ".byte 0x0f, 0x01, 0x0b\n\t" /* sidt (%bp+%di) */ ++ "mov $2000, %bp\n\t" ++ "mov $158, %di\n\t" ++ ".byte 0x0f, 0x01, 0x03\n\t" /* sgdt (%bp+%di) */ ++ /* addressing via registers, 8-bit disp - bx */ ++ "mov $2164, %bx\n\t" ++ ".byte 0x0f, 0x01, 0x67, 0x00\n\t" /* smsw (%bx + 0) */ ++ ".byte 0x0f, 0x01, 0x4f, 0x02\n\t" /* sidt (%bx + 2) */ ++ ".byte 0x0f, 0x01, 0x47, 0x08\n\t" /* sgdt (%bx + 8) */ ++ /* addressing via registers, 8-bit disp - si */ ++ "mov $2178, %si\n\t" ++ ".byte 0x0f, 0x01, 0x64, 0x00\n\t" /* smsw (%si + 0) */ ++ ".byte 0x0f, 0x01, 0x4c, 0x02\n\t" /* sidt (%si + 2) */ ++ ".byte 0x0f, 0x01, 0x44, 0x08\n\t" /* sgdt (%si + 8) */ ++ /* addressing via registers, 8-bit disp - di */ ++ "mov $2192, %di\n\t" ++ ".byte 0x0f, 0x01, 0x65, 0x00\n\t" /* smsw (%si + 0) */ ++ ".byte 0x0f, 0x01, 0x4d, 0x02\n\t" /* sidt (%si + 2) */ ++ ".byte 0x0f, 0x01, 0x45, 0x08\n\t" /* sgdt (%si + 8) */ ++ /* addressing via registers, 8-bit - bx + si */ ++ "mov $2000, %bx\n\t" ++ "mov $206, %si\n\t" ++ ".byte 0x0f, 0x01, 0x60, 0x00\n\t" /* smsw (%bx+%si+0) */ ++ ".byte 0x0f, 0x01, 0x48, 0x02\n\t" /* sidt (%bx+%si+2) */ ++ ".byte 0x0f, 0x01, 0x40, 0x08\n\t" /* sgdt (%bx+%si+8) */ ++ /* addressing via registers, 8-bit - bx + di */ ++ "mov $2000, %bx\n\t" ++ "mov $220, %di\n\t" ++ ".byte 0x0f, 0x01, 0x61, 0x00\n\t" /* smsw (%bx+%di+0) */ ++ ".byte 0x0f, 0x01, 0x49, 0x02\n\t" /* sidt (%bx+%di+2) */ ++ ".byte 0x0f, 0x01, 0x41, 0x08\n\t" /* sgdt (%bx+%di+8) */ ++ /* addressing via registers, 8-bit - bp + si */ ++ "mov $2000, %bp\n\t" ++ "mov $234, %si\n\t" ++ ".byte 0x0f, 0x01, 0x62, 0x00\n\t" /* smsw (%bp+%si+0) */ ++ ".byte 0x0f, 0x01, 0x4a, 0x02\n\t" /* sidt (%bp+%si+2) */ ++ ".byte 0x0f, 0x01, 0x42, 0x08\n\t" /* sgdt (%bp+%si+8) */ ++ /* addressing via registers, 8-bit - bp + di */ ++ "mov $2000, %bp\n\t" ++ "mov $248, %di\n\t" ++ ".byte 0x0f, 0x01, 0x63, 0x00\n\t" /* smsw (%bp+%di+0) */ ++ ".byte 0x0f, 0x01, 0x4b, 0x02\n\t" /* sidt (%bp+%di+2) */ ++ ".byte 0x0f, 0x01, 0x43, 0x08\n\t" /* sgdt (%bp+%di+8) */ ++ /* addressing via registers, 16-bit disp - bx */ ++ "mov $2000, %bx\n\t" ++ ".byte 0x0f, 0x01, 0xa7, 0x06, 0x01\n\t" /* smsw (%bx + 262) */ ++ ".byte 0x0f, 0x01, 0x8f, 0x08, 0x01\n\t" /* sidt (%bx + 264) */ ++ ".byte 0x0f, 0x01, 0x87, 0x0e, 0x01\n\t" /* sgdt (%bx + 270) */ ++ /* addressing via registers, 16-bit disp - si */ ++ "mov $2000, %si\n\t" ++ ".byte 0x0f, 0x01, 0xa4, 0x14, 0x01\n\t" /* smsw (%si + 276) */ ++ ".byte 0x0f, 0x01, 0x8c, 0x16, 0x01\n\t" /* sidt (%si + 278) */ ++ ".byte 0x0f, 0x01, 0x84, 0x1c, 0x01\n\t" /* sgdt (%si + 284) */ ++ /* addressing via registers, 16-bit disp - di */ ++ "mov $2000, %di\n\t" ++ ".byte 0x0f, 0x01, 0xa5, 0x22, 0x01\n\t" /* smsw (%di + 290) */ ++ ".byte 0x0f, 0x01, 0x8d, 0x24, 0x01\n\t" /* sidt (%di + 292) */ ++ ".byte 0x0f, 0x01, 0x85, 0x2a, 0x01\n\t" /* sgdt (%di + 298) */ ++ /* addressing via registers, 16-bit - bx + si */ ++ "mov $1500, %bx\n\t" ++ "mov $500, %si\n\t" ++ ".byte 0x0f, 0x01, 0xa0, 0x30, 0x01\n\t" /* smsw (%bx+%si+304) */ ++ ".byte 0x0f, 0x01, 0x88, 0x32, 0x01\n\t" /* sidt (%bx+%si+306) */ ++ ".byte 0x0f, 0x01, 0x80, 0x38, 0x01\n\t" /* sgdt (%bx+%si+312) */ ++ /* addressing via registers, 16-bit - bx + di */ ++ "mov $1500, %bx\n\t" ++ "mov $500, %di\n\t" ++ ".byte 0x0f, 0x01, 0xa1, 0x3e, 0x01\n\t" /* smsw (%bx+%di+318) */ ++ ".byte 0x0f, 0x01, 0x89, 0x40, 0x01\n\t" /* sidt (%bx+%di+320) */ ++ ".byte 0x0f, 0x01, 0x81, 0x46, 0x01\n\t" /* sgdt (%bx+%di+326) */ ++ /* addressing via registers, 16-bit - bp + si */ ++ "mov $1500, %bp\n\t" ++ "mov $500, %si\n\t" ++ ".byte 0x0f, 0x01, 0xa2, 0x4c, 0x01\n\t" /* smsw (%bp+%si+332) */ ++ ".byte 0x0f, 0x01, 0x8a, 0x4e, 0x01\n\t" /* sidt (%bp+%si+334) */ ++ ".byte 0x0f, 0x01, 0x82, 0x54, 0x01\n\t" /* sgdt (%bp+%si+340) */ ++ /* addressing via registers, 16-bit - bp + di */ ++ "mov $1500, %bp\n\t" ++ "mov $500, %si\n\t" ++ ".byte 0x0f, 0x01, 0xa3, 0x5a, 0x01\n\t" /* smsw (%bp+%di+346) */ ++ ".byte 0x0f, 0x01, 0x8b, 0x5c, 0x01\n\t" /* sidt (%bp+%di+348) */ ++ ".byte 0x0f, 0x01, 0x83, 0x62, 0x01\n\t" /* sgdt (%bp+%di+354) */ + /* register operands, only for smsw */ + "smsw %ax\n\t" +- "mov %ax, (2080)\n\t" ++ "mov %ax, (2360)\n\t" ++ "smsw %cx\n\t" ++ "mov %cx, (2362)\n\t" ++ "smsw %dx\n\t" ++ "mov %dx, (2364)\n\t" ++ "smsw %bx\n\t" ++ "mov %bx, (2366)\n\t" ++ "smsw %sp\n\t" ++ "mov %sp, (2368)\n\t" ++ "smsw %bp\n\t" ++ "mov %bp, (2370)\n\t" ++ "smsw %si\n\t" ++ "mov %si, (2372)\n\t" ++ "smsw %di\n\t" ++ "mov %di, (2374)\n\t" + "int $0x80\n\t" + ".size vmcode, . - vmcode\n\t" + "end_vmcode:\n\t" +@@ -244,7 +390,7 @@ int main(void) + printf("[INFO]\tSGDT: limit[0x%04x]base[0x%08lx]\n", + *(unsigned short *)(addr + 2060), + *(unsigned long *)(addr + 2062)); +- printf("[INFO]\tResults of UMIP-protected instructions via addressing in registers:\n"); ++ printf("[INFO]\tResults of UMIP-protected instructions via addressing in registers: no disp, bx\n"); + printf("[INFO]\tSMSW:[0x%04x]\n", *(unsigned short *)(addr + 2066)); + printf("[INFO]\tSIDT: limit[0x%04x]base[0x%08lx]\n", + *(unsigned short *)(addr + 2068), +@@ -252,8 +398,196 @@ int main(void) + printf("[INFO]\tSGDT: limit[0x%04x]base[0x%08lx]\n", + *(unsigned short *)(addr + 2074), + *(unsigned long *)(addr + 2076)); +- printf("[INFO]\tResults of SMSW via register operands:\n"); ++ ++ printf("[INFO]\tResults of UMIP-protected instructions via addressing in registers: no disp, si\n"); + printf("[INFO]\tSMSW:[0x%04x]\n", *(unsigned short *)(addr + 2080)); ++ printf("[INFO]\tSIDT: limit[0x%04x]base[0x%08lx]\n", ++ *(unsigned short *)(addr + 2082), ++ *(unsigned long *)(addr + 2084)); ++ printf("[INFO]\tSGDT: limit[0x%04x]base[0x%08lx]\n", ++ *(unsigned short *)(addr + 2088), ++ *(unsigned long *)(addr + 2090)); ++ ++ printf("[INFO]\tResults of UMIP-protected instructions via addressing in registers: no disp, di\n"); ++ printf("[INFO]\tSMSW:[0x%04x]\n", *(unsigned short *)(addr + 2094)); ++ printf("[INFO]\tSIDT: limit[0x%04x]base[0x%08lx]\n", ++ *(unsigned short *)(addr + 2096), ++ *(unsigned long *)(addr + 2098)); ++ printf("[INFO]\tSGDT: limit[0x%04x]base[0x%08lx]\n", ++ *(unsigned short *)(addr + 2102), ++ *(unsigned long *)(addr + 2104)); ++ ++ printf("[INFO]\tResults of UMIP-protected instructions via addressing in registers: no disp, bx + si\n"); ++ printf("[INFO]\tSMSW:[0x%04x]\n", *(unsigned short *)(addr + 2108)); ++ printf("[INFO]\tSIDT: limit[0x%04x]base[0x%08lx]\n", ++ *(unsigned short *)(addr + 2110), ++ *(unsigned long *)(addr + 2112)); ++ printf("[INFO]\tSGDT: limit[0x%04x]base[0x%08lx]\n", ++ *(unsigned short *)(addr + 2116), ++ *(unsigned long *)(addr + 2118)); ++ ++ printf("[INFO]\tResults of UMIP-protected instructions via addressing in registers: no disp, bx + di\n"); ++ printf("[INFO]\tSMSW:[0x%04x]\n", *(unsigned short *)(addr + 2122)); ++ printf("[INFO]\tSIDT: limit[0x%04x]base[0x%08lx]\n", ++ *(unsigned short *)(addr + 2124), ++ *(unsigned long *)(addr + 2126)); ++ printf("[INFO]\tSGDT: limit[0x%04x]base[0x%08lx]\n", ++ *(unsigned short *)(addr + 2130), ++ *(unsigned long *)(addr + 2132)); ++ ++ printf("[INFO]\tResults of UMIP-protected instructions via addressing in registers: no disp, bp + si\n"); ++ printf("[INFO]\tSMSW:[0x%04x]\n", *(unsigned short *)(addr + 2136)); ++ printf("[INFO]\tSIDT: limit[0x%04x]base[0x%08lx]\n", ++ *(unsigned short *)(addr + 2138), ++ *(unsigned long *)(addr + 2140)); ++ printf("[INFO]\tSGDT: limit[0x%04x]base[0x%08lx]\n", ++ *(unsigned short *)(addr + 2144), ++ *(unsigned long *)(addr + 2146)); ++ ++ printf("[INFO]\tResults of UMIP-protected instructions via addressing in registers: no disp, bp + di\n"); ++ printf("[INFO]\tSMSW:[0x%04x]\n", *(unsigned short *)(addr + 2150)); ++ printf("[INFO]\tSIDT: limit[0x%04x]base[0x%08lx]\n", ++ *(unsigned short *)(addr + 2152), ++ *(unsigned long *)(addr + 2154)); ++ printf("[INFO]\tSGDT: limit[0x%04x]base[0x%08lx]\n", ++ *(unsigned short *)(addr + 2158), ++ *(unsigned long *)(addr + 2160)); ++ ++ printf("[INFO]\tResults of UMIP-protected instructions via addressing in registers: 8-bit disp, bx\n"); ++ printf("[INFO]\tSMSW:[0x%04x]\n", *(unsigned short *)(addr + 2164)); ++ printf("[INFO]\tSIDT: limit[0x%04x]base[0x%08lx]\n", ++ *(unsigned short *)(addr + 2166), ++ *(unsigned long *)(addr + 2168)); ++ printf("[INFO]\tSGDT: limit[0x%04x]base[0x%08lx]\n", ++ *(unsigned short *)(addr + 2172), ++ *(unsigned long *)(addr + 2174)); ++ ++ printf("[INFO]\tResults of UMIP-protected instructions via addressing in registers: 8-bit disp, si\n"); ++ printf("[INFO]\tSMSW:[0x%04x]\n", *(unsigned short *)(addr + 2178)); ++ printf("[INFO]\tSIDT: limit[0x%04x]base[0x%08lx]\n", ++ *(unsigned short *)(addr + 2180), ++ *(unsigned long *)(addr + 2182)); ++ printf("[INFO]\tSGDT: limit[0x%04x]base[0x%08lx]\n", ++ *(unsigned short *)(addr + 2186), ++ *(unsigned long *)(addr + 2188)); ++ ++ printf("[INFO]\tResults of UMIP-protected instructions via addressing in registers: 8-bit disp, di\n"); ++ printf("[INFO]\tSMSW:[0x%04x]\n", *(unsigned short *)(addr + 2192)); ++ printf("[INFO]\tSIDT: limit[0x%04x]base[0x%08lx]\n", ++ *(unsigned short *)(addr + 2194), ++ *(unsigned long *)(addr + 2196)); ++ printf("[INFO]\tSGDT: limit[0x%04x]base[0x%08lx]\n", ++ *(unsigned short *)(addr + 2200), ++ *(unsigned long *)(addr + 2202)); ++ ++ printf("[INFO]\tResults of UMIP-protected instructions via addressing in registers: 8-bit disp bx +si\n"); ++ printf("[INFO]\tSMSW:[0x%04x]\n", *(unsigned short *)(addr + 2206)); ++ printf("[INFO]\tSIDT: limit[0x%04x]base[0x%08lx]\n", ++ *(unsigned short *)(addr + 2208), ++ *(unsigned long *)(addr + 2210)); ++ printf("[INFO]\tSGDT: limit[0x%04x]base[0x%08lx]\n", ++ *(unsigned short *)(addr + 2214), ++ *(unsigned long *)(addr + 2216)); ++ ++ printf("[INFO]\tResults of UMIP-protected instructions via addressing in registers: 8-bit disp, bx + di\n"); ++ printf("[INFO]\tSMSW:[0x%04x]\n", *(unsigned short *)(addr + 2220)); ++ printf("[INFO]\tSIDT: limit[0x%04x]base[0x%08lx]\n", ++ *(unsigned short *)(addr + 2222), ++ *(unsigned long *)(addr + 2224)); ++ printf("[INFO]\tSGDT: limit[0x%04x]base[0x%08lx]\n", ++ *(unsigned short *)(addr + 2228), ++ *(unsigned long *)(addr + 2230)); ++ ++ printf("[INFO]\tResults of UMIP-protected instructions via addressing in registers: 8-bit disp, bp + si\n"); ++ printf("[INFO]\tSMSW:[0x%04x]\n", *(unsigned short *)(addr + 2234)); ++ printf("[INFO]\tSIDT: limit[0x%04x]base[0x%08lx]\n", ++ *(unsigned short *)(addr + 2236), ++ *(unsigned long *)(addr + 2238)); ++ printf("[INFO]\tSGDT: limit[0x%04x]base[0x%08lx]\n", ++ *(unsigned short *)(addr + 2242), ++ *(unsigned long *)(addr + 2244)); ++ ++ printf("[INFO]\tResults of UMIP-protected instructions via addressing in registers: 8-bit disp, bp + di\n"); ++ printf("[INFO]\tSMSW:[0x%04x]\n", *(unsigned short *)(addr + 2248)); ++ printf("[INFO]\tSIDT: limit[0x%04x]base[0x%08lx]\n", ++ *(unsigned short *)(addr + 2250), ++ *(unsigned long *)(addr + 2252)); ++ printf("[INFO]\tSGDT: limit[0x%04x]base[0x%08lx]\n", ++ *(unsigned short *)(addr + 2256), ++ *(unsigned long *)(addr + 2258)); ++ ++ printf("[INFO]\tResults of UMIP-protected instructions via addressing in registers: 16-bit disp, bx\n"); ++ printf("[INFO]\tSMSW:[0x%04x]\n", *(unsigned short *)(addr + 2262)); ++ printf("[INFO]\tSIDT: limit[0x%04x]base[0x%08lx]\n", ++ *(unsigned short *)(addr + 2264), ++ *(unsigned long *)(addr + 2266)); ++ printf("[INFO]\tSGDT: limit[0x%04x]base[0x%08lx]\n", ++ *(unsigned short *)(addr + 2270), ++ *(unsigned long *)(addr + 2272)); ++ ++ printf("[INFO]\tResults of UMIP-protected instructions via addressing in registers: 16-bit disp, si\n"); ++ printf("[INFO]\tSMSW:[0x%04x]\n", *(unsigned short *)(addr + 2276)); ++ printf("[INFO]\tSIDT: limit[0x%04x]base[0x%08lx]\n", ++ *(unsigned short *)(addr + 2278), ++ *(unsigned long *)(addr + 2280)); ++ printf("[INFO]\tSGDT: limit[0x%04x]base[0x%08lx]\n", ++ *(unsigned short *)(addr + 2284), ++ *(unsigned long *)(addr + 2286)); ++ ++ printf("[INFO]\tResults of UMIP-protected instructions via addressing in registers: 16-bit disp, di\n"); ++ printf("[INFO]\tSMSW:[0x%04x]\n", *(unsigned short *)(addr + 2290)); ++ printf("[INFO]\tSIDT: limit[0x%04x]base[0x%08lx]\n", ++ *(unsigned short *)(addr + 2292), ++ *(unsigned long *)(addr + 2294)); ++ printf("[INFO]\tSGDT: limit[0x%04x]base[0x%08lx]\n", ++ *(unsigned short *)(addr + 2298), ++ *(unsigned long *)(addr + 2230)); ++ ++ printf("[INFO]\tResults of UMIP-protected instructions via addressing in registers: 16-bit disp, bx+si\n"); ++ printf("[INFO]\tSMSW:[0x%04x]\n", *(unsigned short *)(addr + 2304)); ++ printf("[INFO]\tSIDT: limit[0x%04x]base[0x%08lx]\n", ++ *(unsigned short *)(addr + 2306), ++ *(unsigned long *)(addr + 2308)); ++ printf("[INFO]\tSGDT: limit[0x%04x]base[0x%08lx]\n", ++ *(unsigned short *)(addr + 2312), ++ *(unsigned long *)(addr + 2314)); ++ ++ printf("[INFO]\tResults of UMIP-protected instructions via addressing in registers: 16-bit disp, bx+di\n"); ++ printf("[INFO]\tSMSW:[0x%04x]\n", *(unsigned short *)(addr + 2318)); ++ printf("[INFO]\tSIDT: limit[0x%04x]base[0x%08lx]\n", ++ *(unsigned short *)(addr + 2320), ++ *(unsigned long *)(addr + 2322)); ++ printf("[INFO]\tSGDT: limit[0x%04x]base[0x%08lx]\n", ++ *(unsigned short *)(addr + 2326), ++ *(unsigned long *)(addr + 2328)); ++ ++ printf("[INFO]\tResults of UMIP-protected instructions via addressing in registers: 16-bit disp, bp+si\n"); ++ printf("[INFO]\tSMSW:[0x%04x]\n", *(unsigned short *)(addr + 2332)); ++ printf("[INFO]\tSIDT: limit[0x%04x]base[0x%08lx]\n", ++ *(unsigned short *)(addr + 2334), ++ *(unsigned long *)(addr + 2336)); ++ printf("[INFO]\tSGDT: limit[0x%04x]base[0x%08lx]\n", ++ *(unsigned short *)(addr + 2340), ++ *(unsigned long *)(addr + 2342)); ++ ++ printf("[INFO]\tResults of UMIP-protected instructions via addressing in registers: 16-bit disp, bp+di\n"); ++ printf("[INFO]\tSMSW:[0x%04x]\n", *(unsigned short *)(addr + 2346)); ++ printf("[INFO]\tSIDT: limit[0x%04x]base[0x%08lx]\n", ++ *(unsigned short *)(addr + 2348), ++ *(unsigned long *)(addr + 2350)); ++ printf("[INFO]\tSGDT: limit[0x%04x]base[0x%08lx]\n", ++ *(unsigned short *)(addr + 2354), ++ *(unsigned long *)(addr + 2356)); ++ ++ printf("[INFO]\tResults of SMSW via register operands:\n"); ++ printf("[INFO]\tSMSW ax:[0x%04x]\n", *(unsigned short *)(addr + 2360)); ++ printf("[INFO]\tSMSW cx:[0x%04x]\n", *(unsigned short *)(addr + 2362)); ++ printf("[INFO]\tSMSW dx:[0x%04x]\n", *(unsigned short *)(addr + 2364)); ++ printf("[INFO]\tSMSW bx:[0x%04x]\n", *(unsigned short *)(addr + 2366)); ++ printf("[INFO]\tSMSW sp:[0x%04x]\n", *(unsigned short *)(addr + 2368)); ++ printf("[INFO]\tSMSW bp:[0x%04x]\n", *(unsigned short *)(addr + 2370)); ++ printf("[INFO]\tSMSW si:[0x%04x]\n", *(unsigned short *)(addr + 2372)); ++ printf("[INFO]\tSMSW di:[0x%04x]\n", *(unsigned short *)(addr + 2374)); + + /* Execute a null pointer */ + v86.regs.cs = 0; +-- +2.9.3 + diff --git a/meta-luv/recipes-kernel/linux/linux-yocto-efi-test_4.12.bb b/meta-luv/recipes-kernel/linux/linux-yocto-efi-test_4.12.bb index 16d3a2e5244..e0b9b1629cc 100644 --- a/meta-luv/recipes-kernel/linux/linux-yocto-efi-test_4.12.bb +++ b/meta-luv/recipes-kernel/linux/linux-yocto-efi-test_4.12.bb @@ -77,6 +77,8 @@ SRC_URI += "file://0001-x86-mm-Allocate-pages-without-sleeping.patch \ file://0008-x86-efi-Introduce-EFI_WARN_ON_ILLEGAL_ACCESSES.patch \ file://0001-PCI-Vulcan-AHCI-PCI-bar-fix-for-Broadcom-Vulcan-earl.patch \ file://0002-ahci-thunderx2-Fix-for-errata-that-affects-stop-engi.patch \ + file://0001-selftests-x86-Add-tests-for-User-Mode-Instruction-Pr.patch \ + file://0002-selftests-x86-Add-more-tests-for-User-Mode-Instructi.patch \ " COMMON_CFG_x86 = " file://qemux86/modules.cfg \ From 4216c166dc01504f7018fa7677f94f1aeb975cb8 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Wed, 1 Mar 2017 20:40:55 -0800 Subject: [PATCH 005/109] meta-luv: add recipe for virtual-8086 mode kernel selftests Signed-off-by: Ricardo Neri --- .../images/core-image-efi-initramfs.bb | 2 +- meta-luv/recipes-core/vm86/vm86-test.bb | 39 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 meta-luv/recipes-core/vm86/vm86-test.bb diff --git a/meta-luv/recipes-core/images/core-image-efi-initramfs.bb b/meta-luv/recipes-core/images/core-image-efi-initramfs.bb index 8d811bfb9e7..c77c1a213f6 100644 --- a/meta-luv/recipes-core/images/core-image-efi-initramfs.bb +++ b/meta-luv/recipes-core/images/core-image-efi-initramfs.bb @@ -13,7 +13,7 @@ IMAGE_INSTALL = "\ X86_ADDITIONS = "" -IMAGE_INSTALL_append_qemux86 = "${X86_ADDITIONS} umip-tests" +IMAGE_INSTALL_append_qemux86 = "${X86_ADDITIONS} vm86-test umip-tests" IMAGE_INSTALL_append_qemux86-64 = "${X86_ADDITIONS} umip-tests lib32-umip-tests" export IMAGE_BASENAME = "core-image-efi-initramfs" diff --git a/meta-luv/recipes-core/vm86/vm86-test.bb b/meta-luv/recipes-core/vm86/vm86-test.bb new file mode 100644 index 00000000000..5494ed77bed --- /dev/null +++ b/meta-luv/recipes-core/vm86/vm86-test.bb @@ -0,0 +1,39 @@ +DESCRIPTION = "virtual-8086 mode kernel selftests" +HOMEPAGE = "https://www.kernel.org/pub/linux/kernel" +SECTION = "base" +LICENSE = "GPLv2" +LIC_FILES_CHKSUM = "file://COPYING;md5=d7810fab7487fb0aad327b76f1be7cd7" + +S = "${STAGING_KERNEL_DIR}" + +SRCREV="${AUTOREV}" +S = "${STAGING_KERNEL_DIR}" + +do_fetch[noexec] = "1" +do_unpack[depends] += "virtual/kernel:do_unpack" +do_patch[depends] += "virtual/kernel:do_shared_workdir" +do_package[depends] += "virtual/kernel:do_populate_sysroot" + +EXTRA_OEMAKE = " \ + CC='${CC}' \ + -C ${S}/tools/testing/selftests/x86" + +#This is the compilation area +#we need to compile the self tests +do_compile() { + unset CFLAGS + oe_runmake +} + + +#Installing is nothing but putting things in place +do_install() { + # Creating a directory + + install -d ${D}${bindir} + install -m 0755 ${STAGING_KERNEL_DIR}/tools/testing/selftests/x86/entry_from_vm86_32 ${D}${bindir} + install -m 0755 ${STAGING_KERNEL_DIR}/tools/testing/selftests/x86/protection_keys_32 ${D}${bindir} +} + +#LUV_TEST_LOG_PARSER="luv-parser-efivarfs" +#LUV_TEST="efivarfs" From c644594bcbe5d65e899289536f1c4456af7bbcd5 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Fri, 24 Mar 2017 10:16:16 -0700 Subject: [PATCH 006/109] umip: umip_test2: correct print size of expected values MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When checking for the return values of SMSW, SLDT and STR, we use memory operands. Thus, the expected return values are 16-bit. There is no need to use a long lenght field. This helps us to prevent build warnings such as: umip_test2.c:262:2: warning: format ‘%lx’ expects argument of type ‘long unsigned int’, but argument 2 has type ‘int’ [-Wformat=] pr_info("====Checking SLDT. Expected value: [0x%lx]====\n", expected_ldt); ^ Signed-off-by: Ricardo Neri umip_test2.c:262:2: warning: format ‘%lx’ expects argument of type ‘long unsigned int’, but argument 2 has type ‘int’ [-Wformat=] pr_info("====Checking SLDT. Expected value: [0x%lx]====\n", expected_ldt); ^ Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/umip_test2.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/umip_test2.c b/meta-luv/recipes-core/umip/files/umip_test2.c index 946a4e4bf7f..6fa20d63072 100644 --- a/meta-luv/recipes-core/umip/files/umip_test2.c +++ b/meta-luv/recipes-core/umip/files/umip_test2.c @@ -226,7 +226,7 @@ static int test_str(void) unsigned long val; unsigned short mask = 0xffff; - pr_info("====Checking STR. Expected value: [0x%lx]====\n", expected_tr); + pr_info("====Checking STR. Expected value: [0x%x]====\n", expected_tr); pr_info("==Tests for register operands==\n"); pr_info("Value should be saved at [0x%p]\n", &val); CHECK_ALLreg("str", val, INIT_SS, expected_tr); @@ -243,7 +243,7 @@ static int test_smsw(void) unsigned long val; unsigned short mask = 0xffff; - pr_info("====Checking SMSW. Expected value: [0x%lx]====\n", expected_msw); + pr_info("====Checking SMSW. Expected value: [0x%x]====\n", expected_msw); pr_info("==Tests for register operands==\n"); pr_info("Value should be saved at [0x%p]\n", &val); CHECK_ALLreg("smsw", val, INIT_MSW, expected_msw); @@ -259,7 +259,7 @@ static int test_sldt(void) unsigned long val; unsigned short mask = 0xffff; - pr_info("====Checking SLDT. Expected value: [0x%lx]====\n", expected_ldt); + pr_info("====Checking SLDT. Expected value: [0x%x]====\n", expected_ldt); pr_info("==Tests for register operands==\n"); pr_info("Value should be saved at [0x%p]\n", &val); CHECK_ALLreg("sldt", val, INIT_LDTS, expected_ldt); From 8e1cc5def58b317bc0b2eaa7b1c74368b99a346b Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Fri, 24 Mar 2017 10:36:23 -0700 Subject: [PATCH 007/109] umip: umip_test2: cast shorts to unsigned longs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The family of macros CHECK_ALLreg and CHECK_ALLmem can use operands of 16, 32 and 64 bits. Thus, the variables we pass should be the largest of them: longs. Otherwise, we see warnings such as: umip_test2.c:252:2: warning: format ‘%lx’ expects argument of type ‘long unsigned int’, but argument 6 has type ‘int’ [-Wformat=] CHECK_ALLmem("smsw", val, INIT_MSW, expected_msw); ^ Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/umip_test2.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/umip_test2.c b/meta-luv/recipes-core/umip/files/umip_test2.c index 6fa20d63072..3746bb203a9 100644 --- a/meta-luv/recipes-core/umip/files/umip_test2.c +++ b/meta-luv/recipes-core/umip/files/umip_test2.c @@ -229,10 +229,10 @@ static int test_str(void) pr_info("====Checking STR. Expected value: [0x%x]====\n", expected_tr); pr_info("==Tests for register operands==\n"); pr_info("Value should be saved at [0x%p]\n", &val); - CHECK_ALLreg("str", val, INIT_SS, expected_tr); + CHECK_ALLreg("str", val, INIT_SS, (unsigned long)expected_tr); pr_info("==Tests for memory operands==\n"); pr_info("Value should be saved at [0x%p]\n", &val); - CHECK_ALLmem("str", val, INIT_SS, expected_tr); + CHECK_ALLmem("str", val, INIT_SS, (unsigned long)expected_tr); /* TODO: check with addressing using SIB byte */ return 0; @@ -246,10 +246,10 @@ static int test_smsw(void) pr_info("====Checking SMSW. Expected value: [0x%x]====\n", expected_msw); pr_info("==Tests for register operands==\n"); pr_info("Value should be saved at [0x%p]\n", &val); - CHECK_ALLreg("smsw", val, INIT_MSW, expected_msw); + CHECK_ALLreg("smsw", val, INIT_MSW, (unsigned long)expected_msw); pr_info("==Tests for memory operands==\n"); pr_info("Value should be saved at [0x%p]\n", &val); - CHECK_ALLmem("smsw", val, INIT_MSW, expected_msw); + CHECK_ALLmem("smsw", val, INIT_MSW, (unsigned long)expected_msw); /* TODO: check with addressing using SIB byte */ return 0; } @@ -262,10 +262,10 @@ static int test_sldt(void) pr_info("====Checking SLDT. Expected value: [0x%x]====\n", expected_ldt); pr_info("==Tests for register operands==\n"); pr_info("Value should be saved at [0x%p]\n", &val); - CHECK_ALLreg("sldt", val, INIT_LDTS, expected_ldt); + CHECK_ALLreg("sldt", val, INIT_LDTS, (unsigned long)expected_ldt); pr_info("==Tests for memory operands==\n"); pr_info("Value should be saved at [0x%p]\n", &val); - CHECK_ALLmem("sldt", val, INIT_LDTS, expected_ldt); + CHECK_ALLmem("sldt", val, INIT_LDTS, (unsigned long)expected_ldt); /* TODO: check with addressing using SIB byte */ return 0; } From a6a4a31fde1ed13d436555c76b6b3608688e2c1b Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Fri, 24 Mar 2017 10:46:06 -0700 Subject: [PATCH 008/109] umip: remove unused variables There were many variables that we don't use. Remove them. Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/normal_pf.c | 1 - meta-luv/recipes-core/umip/files/umip_ldt_16.c | 3 --- meta-luv/recipes-core/umip/files/umip_ldt_32.c | 3 --- meta-luv/recipes-core/umip/files/umip_test.c | 3 --- meta-luv/recipes-core/umip/files/umip_test2.c | 3 --- 5 files changed, 13 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/normal_pf.c b/meta-luv/recipes-core/umip/files/normal_pf.c index 2678b845f44..79e631e9480 100644 --- a/meta-luv/recipes-core/umip/files/normal_pf.c +++ b/meta-luv/recipes-core/umip/files/normal_pf.c @@ -58,7 +58,6 @@ struct my_struct { void main (void) { struct sigaction action; - unsigned long val; unsigned long *val_bad = (unsigned long *)0x100000; PRINT_BITNESS; diff --git a/meta-luv/recipes-core/umip/files/umip_ldt_16.c b/meta-luv/recipes-core/umip/files/umip_ldt_16.c index 0678ed81321..179c283a69d 100644 --- a/meta-luv/recipes-core/umip/files/umip_ldt_16.c +++ b/meta-luv/recipes-core/umip/files/umip_ldt_16.c @@ -31,13 +31,10 @@ unsigned short cs_orig; #define TI_LDT 1 #define SEGMENT_SELECTOR(index) (RPL3 | (TI_LDT << 2) | (index << 3)) -static sig_atomic_t signal_code; static sig_atomic_t got_signal; void handler(int signum, siginfo_t *info, void *ctx_void) { - ucontext_t *ctx = (ucontext_t *)ctx_void; - pr_info("si_signo[%d]\n", info->si_signo); pr_info("si_errno[%d]\n", info->si_errno); pr_info("si_code[%d]\n", info->si_code); diff --git a/meta-luv/recipes-core/umip/files/umip_ldt_32.c b/meta-luv/recipes-core/umip/files/umip_ldt_32.c index 97f7cf6f5d7..92f10640b9c 100644 --- a/meta-luv/recipes-core/umip/files/umip_ldt_32.c +++ b/meta-luv/recipes-core/umip/files/umip_ldt_32.c @@ -31,13 +31,10 @@ unsigned short cs_orig; #define TI_LDT 1 #define SEGMENT_SELECTOR(index) (RPL3 | (TI_LDT << 2) | (index << 3)) -static sig_atomic_t signal_code; static sig_atomic_t got_signal; void handler(int signum, siginfo_t *info, void *ctx_void) { - ucontext_t *ctx = (ucontext_t *)ctx_void; - pr_info("si_signo[%d]\n", info->si_signo); pr_info("si_errno[%d]\n", info->si_errno); pr_info("si_code[%d]\n", info->si_code); diff --git a/meta-luv/recipes-core/umip/files/umip_test.c b/meta-luv/recipes-core/umip/files/umip_test.c index 871467398ad..1aad9f90fc6 100644 --- a/meta-luv/recipes-core/umip/files/umip_test.c +++ b/meta-luv/recipes-core/umip/files/umip_test.c @@ -22,13 +22,10 @@ #define IDTR_LEN 6 #endif -static sig_atomic_t signal_code; static sig_atomic_t got_signal; static void handler(int signum, siginfo_t *info, void *ctx_void) { - ucontext_t *ctx = (ucontext_t *)ctx_void; - pr_info("si_signo[%d]\n", info->si_signo); pr_info("si_errno[%d]\n", info->si_errno); pr_info("si_code[%d]\n", info->si_code); diff --git a/meta-luv/recipes-core/umip/files/umip_test2.c b/meta-luv/recipes-core/umip/files/umip_test2.c index 3746bb203a9..26b186bf579 100644 --- a/meta-luv/recipes-core/umip/files/umip_test2.c +++ b/meta-luv/recipes-core/umip/files/umip_test2.c @@ -198,7 +198,6 @@ #define INIT_MSW INIT_VAL(0x1414141414141414) #define INIT_LDTS INIT_VAL(0x1515151515151515) -static sig_atomic_t signal_code; static sig_atomic_t got_signal; static unsigned long get_mask(int op_size) { @@ -272,8 +271,6 @@ static int test_sldt(void) static void handler(int signum, siginfo_t *info, void *ctx_void) { - ucontext_t *ctx = (ucontext_t *)ctx_void; - pr_info("si_signo[%d]\n", info->si_signo); pr_info("si_errno[%d]\n", info->si_errno); pr_info("si_code[%d]\n", info->si_code); From 3840b9f0aeedfb6fb32ffa2b8b78f4a88ed21b9d Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Fri, 24 Mar 2017 10:51:04 -0700 Subject: [PATCH 009/109] umip: make main functions return an int MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Instead of returning nothing, have the main functions of test programs to return an integer. This makes warnings like this go away: normal_pf.c:58:6: warning: return type of ‘main’ is not ‘int’ [-Wmain] void main (void) Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/normal_pf.c | 8 +++++--- meta-luv/recipes-core/umip/files/umip_ldt_16.c | 6 +++--- meta-luv/recipes-core/umip/files/umip_ldt_32.c | 6 +++--- meta-luv/recipes-core/umip/files/umip_ldt_64.c | 9 +++------ meta-luv/recipes-core/umip/files/umip_test.c | 6 ++++-- meta-luv/recipes-core/umip/files/umip_test2.c | 6 ++++-- 6 files changed, 22 insertions(+), 19 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/normal_pf.c b/meta-luv/recipes-core/umip/files/normal_pf.c index 79e631e9480..fff7e3b720d 100644 --- a/meta-luv/recipes-core/umip/files/normal_pf.c +++ b/meta-luv/recipes-core/umip/files/normal_pf.c @@ -55,7 +55,7 @@ struct my_struct { int c; }; -void main (void) +int main (void) { struct sigaction action; unsigned long *val_bad = (unsigned long *)0x100000; @@ -85,7 +85,7 @@ void main (void) if (!got_signal) { pr_fail("Signal not received!\n"); - return; + return 1; } #ifdef __x86_64__ if (signal_code != SI_KERNEL) @@ -105,6 +105,8 @@ void main (void) if (sigaction(SIGSEGV, &action, NULL) < 0) { pr_error("Could not remove signal handler!\n"); - exit(1); + return 1; } + + return 0; } diff --git a/meta-luv/recipes-core/umip/files/umip_ldt_16.c b/meta-luv/recipes-core/umip/files/umip_ldt_16.c index 179c283a69d..0adc57bb7cf 100644 --- a/meta-luv/recipes-core/umip/files/umip_ldt_16.c +++ b/meta-luv/recipes-core/umip/files/umip_ldt_16.c @@ -176,7 +176,7 @@ static int setup_data_segments() return 0; } -void main(void) +int main(void) { int ret; unsigned short interim_cs, interim_ss; @@ -352,10 +352,10 @@ void main(void) } printf("Exiting...\n"); - return; + return 0; err_out: pr_error("Could not run tests\n"); - exit(1); + return 1; }; diff --git a/meta-luv/recipes-core/umip/files/umip_ldt_32.c b/meta-luv/recipes-core/umip/files/umip_ldt_32.c index 92f10640b9c..555c008ff91 100644 --- a/meta-luv/recipes-core/umip/files/umip_ldt_32.c +++ b/meta-luv/recipes-core/umip/files/umip_ldt_32.c @@ -127,7 +127,7 @@ static int setup_data_segments() return 0; } -void main(void) +int main(void) { int ret; unsigned short test_cs, test_ds, test_ss; @@ -266,10 +266,10 @@ void main(void) pr_info("Exiting...\n"); - return; + return 0; err_out: pr_error("Could not run tests\n"); - exit(1); + return 1; }; diff --git a/meta-luv/recipes-core/umip/files/umip_ldt_64.c b/meta-luv/recipes-core/umip/files/umip_ldt_64.c index 9a71b02e066..2b6669753e4 100644 --- a/meta-luv/recipes-core/umip/files/umip_ldt_64.c +++ b/meta-luv/recipes-core/umip/files/umip_ldt_64.c @@ -28,13 +28,10 @@ unsigned short old_fs, old_gs; #define TI_LDT 1 #define SEGMENT_SELECTOR(index) (RPL3 | (TI_LDT << 2) | (index << 3)) -static sig_atomic_t signal_code; static sig_atomic_t got_signal; void handler(int signum, siginfo_t *info, void *ctx_void) { - ucontext_t *ctx = (ucontext_t *)ctx_void; - pr_info("si_signo[%d]\n", info->si_signo); pr_info("si_errno[%d]\n", info->si_errno); pr_info("si_code[%d]\n", info->si_code); @@ -94,7 +91,7 @@ static int setup_data_segments() return 0; } -void main(void) +int main(void) { int ret; unsigned short test_fs, test_gs; @@ -182,9 +179,9 @@ void main(void) } printf("Exiting...\n"); - return; + return 0; err_out: pr_error("Could not run tests\n"); - exit(1); + return 1; }; diff --git a/meta-luv/recipes-core/umip/files/umip_test.c b/meta-luv/recipes-core/umip/files/umip_test.c index 1aad9f90fc6..6cf305f7af7 100644 --- a/meta-luv/recipes-core/umip/files/umip_test.c +++ b/meta-luv/recipes-core/umip/files/umip_test.c @@ -238,7 +238,7 @@ static void call_str() } -void main(void) +int main(void) { struct sigaction action; @@ -266,6 +266,8 @@ void main(void) if (sigaction(SIGSEGV, &action, NULL) < 0) { pr_error("Could not remove signal handler!"); - exit(1); + return 1; } + + return 0; } diff --git a/meta-luv/recipes-core/umip/files/umip_test2.c b/meta-luv/recipes-core/umip/files/umip_test2.c index 26b186bf579..a6b9acde953 100644 --- a/meta-luv/recipes-core/umip/files/umip_test2.c +++ b/meta-luv/recipes-core/umip/files/umip_test2.c @@ -294,7 +294,7 @@ static void handler(int signum, siginfo_t *info, void *ctx_void) exit(1); } -void main (void) +int main (void) { int ret_str, ret_smsw, ret_sldt; struct sigaction action; @@ -327,6 +327,8 @@ void main (void) if (sigaction(SIGSEGV, &action, NULL) < 0) { pr_error("Could not remove signal handler!"); - exit(1); + return 1; } + + return 0; } From a0ef85cf5e367ac6a8b491a46efe11021d6ca0f1 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Fri, 24 Mar 2017 10:53:11 -0700 Subject: [PATCH 010/109] umip: umip_test: remove unneeded function get_mask This function is not used anywhere. Get rid of it. Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/umip_test.c | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/umip_test.c b/meta-luv/recipes-core/umip/files/umip_test.c index 6cf305f7af7..bf50eaca7f0 100644 --- a/meta-luv/recipes-core/umip/files/umip_test.c +++ b/meta-luv/recipes-core/umip/files/umip_test.c @@ -49,26 +49,6 @@ static void handler(int signum, siginfo_t *info, void *ctx_void) exit(1); } -static unsigned long get_mask(int op_size) { - switch (op_size) { - case 16: - return 0xffff; - case 32: - return 0xffffffff; -#if __x86_64__ - case 64: - return 0xffffffffffffffff; -#endif - default: - pr_error("Invalid operand size!\n"); - /* - * We can't return -1 as it would be equal to the - * 32 or 64-bit mask - */ - return 0; - } -} - static void call_sgdt() { unsigned char val[GDTR_LEN]; From 1d7a3e12525221279e2bb5073b8ac6986e849c49 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Fri, 24 Mar 2017 10:55:09 -0700 Subject: [PATCH 011/109] umip: umip_test: utilize %p instead of %lx to print pointers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This makes warning like this go away: umip_test.c:61:2: warning: format ‘%lx’ expects argument of type ‘long unsigned int’, but argument 2 has type ‘unsigned char *’ [-Wformat=] pr_info("Will issue SGDT and save at [0x%lx]\n", val); ^ Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/umip_test.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/umip_test.c b/meta-luv/recipes-core/umip/files/umip_test.c index bf50eaca7f0..ef13ca2d019 100644 --- a/meta-luv/recipes-core/umip/files/umip_test.c +++ b/meta-luv/recipes-core/umip/files/umip_test.c @@ -58,7 +58,7 @@ static void call_sgdt() for (i = 0; i < GDTR_LEN; i++) val[i] = 0; - pr_info("Will issue SGDT and save at [0x%lx]\n", val); + pr_info("Will issue SGDT and save at [%p]\n", val); asm volatile("sgdt %0" : "=m" (val)); limit = val[1] << 8 | val[0]; @@ -92,7 +92,7 @@ static void call_sidt() for (i = 0; i < IDTR_LEN; i++) val[i] = 0; - pr_info("Will issue SIDT and save at [0x%lx]\n", val); + pr_info("Will issue SIDT and save at [%p]\n", val); asm volatile("sidt %0" : "=m" (val)); limit = val[1] << 8 | val[0]; @@ -124,7 +124,7 @@ static void call_sldt() /* if operand is memory, result is 16-bit */ unsigned short mask = 0xffff; - pr_info("Will issue SLDT and save at [0x%lx]\n", &val); + pr_info("Will issue SLDT and save at [%p]\n", &val); asm volatile("sldt %0" : "=m" (val)); pr_info("SS for LDT[0x%08lx]\n", val); @@ -147,7 +147,7 @@ static void call_smsw() unsigned long init_val = INIT_VAL(0xa2a2a2a2a2a2a2a2); unsigned short mask = 0xffff; - pr_info("Will issue SMSW and save at [0x%lx]\n", &val); + pr_info("Will issue SMSW and save at [%p]\n", &val); asm volatile("smsw %0" : "=m" (val)); pr_info("CR0[0x%08lx]\n", val); From ab58badd2888c5eb73381df65aabdc06ff105bcc Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Fri, 24 Mar 2017 11:02:45 -0700 Subject: [PATCH 012/109] umip: umip_ldt_16: Fix warning about implicit declaration of syscall MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Apparently, it is not sufficient to include sys/syscall.h as it cannot find the prototype of syscall. Include also unistd.h and make this warning go away: umip_ldt_16.c:117:2: warning: implicit declaration of function ‘syscall’ [-Wimplicit-function-declaration] ret = syscall(SYS_modify_ldt, 1, &desc, sizeof(desc)); ^ Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/umip_ldt_16.c | 1 + meta-luv/recipes-core/umip/files/umip_ldt_64.c | 1 + 2 files changed, 2 insertions(+) diff --git a/meta-luv/recipes-core/umip/files/umip_ldt_16.c b/meta-luv/recipes-core/umip/files/umip_ldt_16.c index 0adc57bb7cf..7b0084a6203 100644 --- a/meta-luv/recipes-core/umip/files/umip_ldt_16.c +++ b/meta-luv/recipes-core/umip/files/umip_ldt_16.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include diff --git a/meta-luv/recipes-core/umip/files/umip_ldt_64.c b/meta-luv/recipes-core/umip/files/umip_ldt_64.c index 2b6669753e4..52aa495d107 100644 --- a/meta-luv/recipes-core/umip/files/umip_ldt_64.c +++ b/meta-luv/recipes-core/umip/files/umip_ldt_64.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include From baae348f7a813e59c03af8ed20d1670554dc68b1 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Fri, 24 Mar 2017 11:19:24 -0700 Subject: [PATCH 013/109] umip: umip_ldt_64: fix printing of uninitialized variable MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We meant to check the return value of setup_data_segments. However, we did not save its return value in the variable we use to print the error. Correct this and avoid seeing this warning: umip_test_defs.h:13:23: warning: ‘ret’ may be used uninitialized in this function [-Wmaybe-uninitialized] #define pr_error(...) printf(TEST_ERROR __VA_ARGS__) ^ umip_ldt_64.c:97:6: note: ‘ret’ was declared here int ret; ^ gcc -Wall -o umip_ldt_64 test_umip_ldt_64.o umip_ldt_64.o Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/umip_ldt_64.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/meta-luv/recipes-core/umip/files/umip_ldt_64.c b/meta-luv/recipes-core/umip/files/umip_ldt_64.c index 52aa495d107..d8d21e02b76 100644 --- a/meta-luv/recipes-core/umip/files/umip_ldt_64.c +++ b/meta-luv/recipes-core/umip/files/umip_ldt_64.c @@ -123,7 +123,8 @@ int main(void) test_fs = SEGMENT_SELECTOR(DATA_FS_DESC_INDEX); test_gs = SEGMENT_SELECTOR(DATA_GS_DESC_INDEX); - if (setup_data_segments()) { + ret = setup_data_segments(); + if (ret) { pr_error("Failed to setup segments [%d].\n", ret); goto err_out; } From 5a9b21827201dafd9b28a0c467d66077d0be4f3c Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Fri, 24 Mar 2017 11:41:22 -0700 Subject: [PATCH 014/109] umip: rework INIT_VAL macro Old compilers will complain that 32-bit longs are too large to contains 64-bit long values. This is true even when they are OR'ed with a 32-bit mask. Thus, instead of OR'ing the value, take the 32-bit value and use it fill the most significant bytes of a 64-bit long. Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/umip_test.c | 12 ++++++------ meta-luv/recipes-core/umip/files/umip_test2.c | 6 +++--- meta-luv/recipes-core/umip/files/umip_test_defs.h | 4 ++-- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/umip_test.c b/meta-luv/recipes-core/umip/files/umip_test.c index ef13ca2d019..cc2015beba1 100644 --- a/meta-luv/recipes-core/umip/files/umip_test.c +++ b/meta-luv/recipes-core/umip/files/umip_test.c @@ -52,7 +52,7 @@ static void handler(int signum, siginfo_t *info, void *ctx_void) static void call_sgdt() { unsigned char val[GDTR_LEN]; - unsigned long base = INIT_VAL(0x8989898989898989); + unsigned long base = INIT_VAL(89898989); unsigned short limit = 0x3d3d; int i; @@ -86,7 +86,7 @@ static void call_sgdt() static void call_sidt() { unsigned char val[IDTR_LEN]; - unsigned long base = INIT_VAL(0x7373737373737373); + unsigned long base = INIT_VAL(73737373); unsigned short limit = 0x9696; int i; @@ -119,8 +119,8 @@ static void call_sidt() static void call_sldt() { - unsigned long val = INIT_VAL(0xa1a1a1a1a1a1a1a1); - unsigned long init_val = INIT_VAL(0xa1a1a1a1a1a1a1a1); + unsigned long val = INIT_VAL(a1a1a1a1); + unsigned long init_val = INIT_VAL(a1a1a1a1); /* if operand is memory, result is 16-bit */ unsigned short mask = 0xffff; @@ -143,8 +143,8 @@ static void call_sldt() static void call_smsw() { - unsigned long val = INIT_VAL(0xa2a2a2a2a2a2a2a2); - unsigned long init_val = INIT_VAL(0xa2a2a2a2a2a2a2a2); + unsigned long val = INIT_VAL(a2a2a2a2); + unsigned long init_val = INIT_VAL(a2a2a2a2); unsigned short mask = 0xffff; pr_info("Will issue SMSW and save at [%p]\n", &val); diff --git a/meta-luv/recipes-core/umip/files/umip_test2.c b/meta-luv/recipes-core/umip/files/umip_test2.c index a6b9acde953..af020b783f5 100644 --- a/meta-luv/recipes-core/umip/files/umip_test2.c +++ b/meta-luv/recipes-core/umip/files/umip_test2.c @@ -194,9 +194,9 @@ CHECK_INSNmem(insn, "edi", val, init, exp) #endif -#define INIT_SS INIT_VAL(0x1313131313131313) -#define INIT_MSW INIT_VAL(0x1414141414141414) -#define INIT_LDTS INIT_VAL(0x1515151515151515) +#define INIT_SS INIT_VAL(13131313) +#define INIT_MSW INIT_VAL(14141414) +#define INIT_LDTS INIT_VAL(15151515) static sig_atomic_t got_signal; diff --git a/meta-luv/recipes-core/umip/files/umip_test_defs.h b/meta-luv/recipes-core/umip/files/umip_test_defs.h index d31eb87980f..adce05b1824 100644 --- a/meta-luv/recipes-core/umip/files/umip_test_defs.h +++ b/meta-luv/recipes-core/umip/files/umip_test_defs.h @@ -14,10 +14,10 @@ #ifdef __x86_64__ #define PRINT_BITNESS pr_info("This binary uses 64-bit code\n") -#define INIT_VAL(val) (val) +#define INIT_VAL(val) (0x##val##val) #else #define PRINT_BITNESS pr_info("This binary uses 32-bit code\n") -#define INIT_VAL(val) (val & 0xffffffff) +#define INIT_VAL(val) (0x##val) #endif #define EXPECTED_SMSW 0x33 From 951513c8f8dfb3b4c57dd3ea957cf7cfcf2fa46c Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Fri, 24 Mar 2017 12:33:21 -0700 Subject: [PATCH 015/109] umip: umip_test_gen_*: utilize a custom to-hex function Python appends an L to any long number and Python decides what type of variable use based on its value. In most of the cases we will not implicitly deal with longs (i.e., most of the time our variables will have small values). However, to represent negative numbers we will rely on 2's complements and thus we will need long variables. To avoid the trailing L, use a custom my_hex function to convert a number to its hex representation. Reported-by: Qian Ouyang Signed-off-by: Ricardo Neri --- .../umip/files/umip_test_gen_16.py | 37 +++++----- .../umip/files/umip_test_gen_32.py | 55 ++++++++------- .../umip/files/umip_test_gen_64.py | 70 +++++++++---------- 3 files changed, 82 insertions(+), 80 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/umip_test_gen_16.py b/meta-luv/recipes-core/umip/files/umip_test_gen_16.py index 6533ceac079..2d60b138850 100644 --- a/meta-luv/recipes-core/umip/files/umip_test_gen_16.py +++ b/meta-luv/recipes-core/umip/files/umip_test_gen_16.py @@ -81,6 +81,9 @@ def two_comp_16(val): else: return ((abs(val) ^ 0xffff) + 1) & 0xffff +def my_hex(val): + return hex(val).rstrip("L") + def get_segment_prefix(segment, register, modrm): """ default segments """ if (segment.prefix == ""): @@ -116,11 +119,11 @@ def get_segment_prefix(segment, register, modrm): def generate_check_code(comment, segment_chk_str, address, inst): # TODO: Use an enum here if (inst.result_bytes == 2): - checkcode = "\tgot = *(unsigned short *)(" + segment_chk_str +" + " + str(hex(address)) + ");\n" + checkcode = "\tgot = *(unsigned short *)(" + segment_chk_str +" + " + str(my_hex(address)) + ");\n" checkcode += "\tprintf(\"%s " + comment + ". Got:[0x%x]Exp[0x%x]\\n\",\n" checkcode += "\t got==expected ? TEST_PASS : TEST_FAIL, got, expected);\n" elif (inst.result_bytes == 6): - checkcode = "\tgot = (struct table_desc *)(" + segment_chk_str +" + " + str(hex(address)) + ");\n" + checkcode = "\tgot = (struct table_desc *)(" + segment_chk_str +" + " + str(my_hex(address)) + ");\n" checkcode += "\tprintf(\"%s " + comment + ". Got:Base[0x%lx]Limit[0x%x]ExpBase[0x%lx]Limit[0x%x]\\n\",\n" checkcode += "\t ((got->base == expected->base) && (got->limit == expected->limit)) ? TEST_PASS : TEST_FAIL, got->base, got->limit, expected->base, expected->limit);\n" return checkcode @@ -132,17 +135,17 @@ def generate_disp(modrm, disp): # if r/m is 6 (same as BP), this is a disp16 if (modrm_rm == 6): disp_2comp = two_comp_16(disp) - disp_str = ", " + str(hex(disp_2comp & 0xff)) + ", " - disp_str += str(hex((disp_2comp >> 8) & 0xff)) + disp_str = ", " + str(my_hex(disp_2comp & 0xff)) + ", " + disp_str += str(my_hex((disp_2comp >> 8) & 0xff)) else: disp_str = "" elif (modrm_mod == 1): disp_2comp = two_comp_8(disp) - disp_str = ", " + str(hex(disp)) + disp_str = ", " + str(my_hex(disp)) elif (modrm_mod == 2): disp_2comp = two_comp_16(disp) - disp_str = ", " + str(hex(disp_2comp & 0xff)) + ", " - disp_str += str(hex((disp_2comp >> 8) & 0xff)) + disp_str = ", " + str(my_hex(disp_2comp & 0xff)) + ", " + disp_str += str(my_hex((disp_2comp >> 8) & 0xff)) return disp_str @@ -151,14 +154,14 @@ def generate_code(tc_nr, segment, inst, register, modrm_mod, index, disp): code_end = "\\n\\t\"\n" modrm = (modrm_mod << 6) | (inst.modrm_reg << 3) | register.modrm_rm - modrm_str = ", " + str(hex(modrm)) + modrm_str = ", " + str(my_hex(modrm)) mov_reg_str = "" for m in register.mnemonic: val = two_comp_16(index/len(register.mnemonic)) # we are just splitting the index between the number of registers indicated in modrm (max is 2) # this should not be a problem as we always expect the index to be an even number - mov_reg_str += "\t\"mov $" + str(hex(val)) + ", " + m + "\\n\\t\"\n" + mov_reg_str += "\t\"mov $" + str(my_hex(val)) + ", " + m + "\\n\\t\"\n" segment_str, segment_chk_str = get_segment_prefix(segment, register, modrm) @@ -182,17 +185,17 @@ def generate_code(tc_nr, segment, inst, register, modrm_mod, index, disp): comment += " + disp8). " elif (modrm_mod == 2): comment += " + disp16). " - comment += "EFF_ADDR[" + str(hex(index + disp)) + "]. " + comment += "EFF_ADDR[" + str(my_hex(index + disp)) + "]. " for n in register.name: - comment += n + "[" + str(hex(index/len(register.name))) + "] " + comment += n + "[" + str(my_hex(index/len(register.name))) + "] " if (modrm_mod == 0): comment += "" elif ((modrm_mod == 0) and ((modrm & 0x7) == 6)): - comment += "disp16[" + str(hex(disp)) + "]" + comment += "disp16[" + str(my_hex(disp)) + "]" elif (modrm_mod == 1): - comment += "disp8[" + str(hex(disp)) + "]" + comment += "disp8[" + str(my_hex(disp)) + "]" elif (modrm_mod == 2): - comment += "disp16[" + str(hex(disp)) + "]" + comment += "disp16[" + str(my_hex(disp)) + "]" code = "\t/* " + comment + " */\n" code += mov_reg_str @@ -213,7 +216,7 @@ def generate_special_unit_tests(start_tc_nr, segment, inst, start_idx): # MOD = 0, r/m = 6 (BP), no index is used, only a disp16 modrm = (MODRM_MO0 << 6) | (inst.modrm_reg << 3) | BP.modrm_rm - modrm_str = ", " + str(hex(modrm)) + modrm_str = ", " + str(my_hex(modrm)) segment_str, segment_chk_str = get_segment_prefix(segment, BP, modrm) @@ -222,8 +225,8 @@ def generate_special_unit_tests(start_tc_nr, segment, inst, start_idx): comment = "Special Test case " + str(tc_nr) + ": " comment += "SEG[" + segment.name + "] " comment += "INSN: " + inst.name + " (disp32)." - comment += "EFF_ADDR[" + str(hex(index)) + "]." - comment += " disp32[" + str(hex(index)) + "]" + comment += "EFF_ADDR[" + str(my_hex(index)) + "]." + comment += " disp32[" + str(my_hex(index)) + "]" code = "\t/* " + comment + " */\n" code += code_start + segment_str + opcode_str + modrm_str + disp_str + code_end diff --git a/meta-luv/recipes-core/umip/files/umip_test_gen_32.py b/meta-luv/recipes-core/umip/files/umip_test_gen_32.py index 2dce20b8d35..181a666ae46 100644 --- a/meta-luv/recipes-core/umip/files/umip_test_gen_32.py +++ b/meta-luv/recipes-core/umip/files/umip_test_gen_32.py @@ -91,9 +91,12 @@ def two_comp_32(val): def two_comp_8(val): if (val > 0): - return hex(val) + return val else: - return hex(((abs(val) ^ 0xff) + 1) & 0xff) + return ((abs(val) ^ 0xff) + 1) & 0xff + +def my_hex(val): + return hex(val).rstrip("L") def find_backup_reg(do_not_use): regs = [ EAX, ECX, EDX, EBX, ESP, EBP, ESI, EDI] @@ -125,11 +128,11 @@ def get_segment_prefix(segment, register, modrm, sib=0): def generate_check_code(comment, segment_chk_str, address, inst): # TODO: Use an enum here if (inst.result_bytes == 2): - checkcode = "\tgot = *(unsigned short *)(" + segment_chk_str +" + " + str(hex(address)) + ");\n" + checkcode = "\tgot = *(unsigned short *)(" + segment_chk_str +" + " + str(my_hex(address)) + ");\n" checkcode += "\tprintf(\"%s " + comment + ". Got:[0x%x]Exp[0x%x]\\n\",\n" checkcode += "\t got==expected ? TEST_PASS : TEST_FAIL, got, expected);\n" elif (inst.result_bytes == 6): - checkcode = "\tgot = (struct table_desc *)(" + segment_chk_str +" + " + str(hex(address)) + ");\n" + checkcode = "\tgot = (struct table_desc *)(" + segment_chk_str +" + " + str(my_hex(address)) + ");\n" checkcode += "\tprintf(\"%s " + comment + ". Got:Base[0x%lx]Limit[0x%x]ExpBase[0x%lx]Limit[0x%x]\\n\",\n" checkcode += "\t ((got->base == expected->base) && (got->limit == expected->limit)) ? TEST_PASS : TEST_FAIL, got->base, got->limit, expected->base, expected->limit);\n" return checkcode @@ -164,8 +167,8 @@ def generate_code(tc_nr, segment, inst, register, modrm_mod, index, disp): code_end = "\\n\\t\"\n" modrm = (modrm_mod << 6) | (inst.modrm_reg << 3) | register.modrm_rm - modrm_str = ", " + str(hex(modrm)) - mov_reg_str = "\t\"mov $" + str(hex(two_comp_32(index))) + ", " + register.mnemonic + "\\n\\t\"\n" + modrm_str = ", " + str(my_hex(modrm)) + mov_reg_str = "\t\"mov $" + str(my_hex(two_comp_32(index))) + ", " + register.mnemonic + "\\n\\t\"\n" segment_str, segment_chk_str = get_segment_prefix(segment, register, modrm) @@ -182,14 +185,14 @@ def generate_code(tc_nr, segment, inst, register, modrm_mod, index, disp): comment += " + disp8). " elif (modrm_mod == 2): comment += " + disp32). " - comment += "EFF_ADDR[" + str(hex(index + disp)) + "]. " - comment += register.name + "[" + str(hex(index)) + "] " + comment += "EFF_ADDR[" + str(my_hex(index + disp)) + "]. " + comment += register.name + "[" + str(my_hex(index)) + "] " if (modrm_mod == 0): comment += "" elif (modrm_mod == 1): - comment += "disp8[" + str(hex(disp)) + "]" + comment += "disp8[" + str(my_hex(disp)) + "]" elif (modrm_mod == 2): - comment += "disp32[" + str(hex(disp)) + "]" + comment += "disp32[" + str(my_hex(disp)) + "]" code = "\t/* " + comment + " */\n" code += mov_reg_str @@ -205,10 +208,10 @@ def generate_code_sib(tc_nr, segment, inst, reg_base, reg_index, modrm_mod, sib_ modrm = (modrm_mod << 6) | (inst.modrm_reg << 3) | ESP.modrm_rm sib = (sib_scale << 6) | (reg_index.modrm_rm << 3) | reg_base.modrm_rm - modrm_str = ", " + str(hex(modrm)) - sib_str = ", " + str(hex(sib)) - mov_reg_str = "\t\"mov $" + str(hex(two_comp_32(sib_base))) + ", " + reg_base.mnemonic + "\\n\\t\"\n" - mov_reg_str += "\t\"mov $" + str(hex(two_comp_32(sib_index))) + ", " + reg_index.mnemonic + "\\n\\t\"\n" + modrm_str = ", " + str(my_hex(modrm)) + sib_str = ", " + str(my_hex(sib)) + mov_reg_str = "\t\"mov $" + str(my_hex(two_comp_32(sib_base))) + ", " + reg_base.mnemonic + "\\n\\t\"\n" + mov_reg_str += "\t\"mov $" + str(my_hex(two_comp_32(sib_index))) + ", " + reg_index.mnemonic + "\\n\\t\"\n" if ((reg_index == ESP) or (reg_base == ESP)): backup_reg = find_backup_reg([reg_base, reg_index]) @@ -248,17 +251,17 @@ def generate_code_sib(tc_nr, segment, inst, reg_base, reg_index, modrm_mod, sib_ eff_addr = calc_base + calc_index*(1<> 8) & 0xff)) + ", " - disp_str += str(hex((index >> 16) & 0xff)) + ", " - disp_str += str(hex((index >> 24) & 0xff)) + disp_str = ", " + str(my_hex(index & 0xff)) + ", " + disp_str += str(my_hex((index >> 8) & 0xff)) + ", " + disp_str += str(my_hex((index >> 16) & 0xff)) + ", " + disp_str += str(my_hex((index >> 24) & 0xff)) comment = "Special Test case " + str(tc_nr) + ": " comment += "SEG[" + segment.name + "] " comment += "INSN: " + inst.name + " (disp32)." - comment += "EFF_ADDR[" + str(hex(index)) + "]." - comment += " disp32[" + str(hex(index)) + "]" + comment += "EFF_ADDR[" + str(my_hex(index)) + "]." + comment += " disp32[" + str(my_hex(index)) + "]" code = "\t/* " + comment + " */\n" code += code_start + segment_str + opcode_str + modrm_str + disp_str +code_end diff --git a/meta-luv/recipes-core/umip/files/umip_test_gen_64.py b/meta-luv/recipes-core/umip/files/umip_test_gen_64.py index f53cc9a543a..795fc12a29e 100644 --- a/meta-luv/recipes-core/umip/files/umip_test_gen_64.py +++ b/meta-luv/recipes-core/umip/files/umip_test_gen_64.py @@ -103,11 +103,7 @@ def two_comp_8(val): return ((abs(val) ^ 0xff) + 1) & 0xff def my_hex(val): - hxval = hex(val) - if (hxval[-1] == 'L'): - return hxval[:-1] - else: - return hxval + return hex(val).rstrip("L") def find_backup_reg(do_not_use): regs = [ RAX, RCX, RDX, RBX, RSP, RBP, RSI, RDI, R8, R9, R10, R11, R12, R13, R14, R15] @@ -139,11 +135,11 @@ def get_segment_prefix(segment, register, modrm, sib=0): def generate_check_code(comment, segment_chk_str, address, inst): # TODO: Use an enum here if (inst.result_bytes == 2): - checkcode = "\tgot = *(unsigned short *)(" + segment_chk_str +" + " + str(hex(address)) + ");\n" + checkcode = "\tgot = *(unsigned short *)(" + segment_chk_str +" + " + str(my_hex(address)) + ");\n" checkcode += "\tprintf(\"%s " + comment + ". Got:[0x%x]Exp[0x%x]\\n\",\n" checkcode += "\t got==expected ? TEST_PASS : TEST_FAIL, got, expected);\n" elif (inst.result_bytes == 6): - checkcode = "\tgot = (struct table_desc *)(" + segment_chk_str +" + " + str(hex(address)) + ");\n" + checkcode = "\tgot = (struct table_desc *)(" + segment_chk_str +" + " + str(my_hex(address)) + ");\n" checkcode += "\tprintf(\"%s " + comment + ". Got:Base[0x%lx]Limit[0x%x]ExpBase[0x%lx]Limit[0x%x]\\n\",\n" checkcode += "\t ((got->base == expected->base) && (got->limit == expected->limit)) ? TEST_PASS : TEST_FAIL, got->base, got->limit, expected->base, expected->limit);\n" return checkcode @@ -155,21 +151,21 @@ def generate_disp(modrm, disp, sib=0): # if a sib byte is used, disp 32 is used if (((sib &7) == 5) and (modrm & 7) == 4): disp_2comp = two_comp_32(disp) - disp_str = ", " + str(hex(disp_2comp & 0xff)) + ", " - disp_str += str(hex((disp_2comp >> 8) & 0xff)) + ", " - disp_str += str(hex((disp_2comp >> 16) & 0xff)) + ", " - disp_str += str(hex((disp_2comp >> 24) & 0xff)) + disp_str = ", " + str(my_hex(disp_2comp & 0xff)) + ", " + disp_str += str(my_hex((disp_2comp >> 8) & 0xff)) + ", " + disp_str += str(my_hex((disp_2comp >> 16) & 0xff)) + ", " + disp_str += str(my_hex((disp_2comp >> 24) & 0xff)) else: disp_str = "" elif (modrm_mod == 1): disp_2comp = two_comp_8(disp) - disp_str = ", " + str(hex(disp_2comp)) + disp_str = ", " + str(my_hex(disp_2comp)) elif (modrm_mod == 2): disp_2comp = two_comp_32(disp) - disp_str = ", " + str(hex(disp_2comp & 0xff)) + ", " - disp_str += str(hex((disp_2comp >> 8) & 0xff)) + ", " - disp_str += str(hex((disp_2comp >> 16) & 0xff)) + ", " - disp_str += str(hex((disp_2comp >> 24) & 0xff)) + disp_str = ", " + str(my_hex(disp_2comp & 0xff)) + ", " + disp_str += str(my_hex((disp_2comp >> 8) & 0xff)) + ", " + disp_str += str(my_hex((disp_2comp >> 16) & 0xff)) + ", " + disp_str += str(my_hex((disp_2comp >> 24) & 0xff)) return disp_str @@ -178,7 +174,7 @@ def generate_code(tc_nr, segment, inst, register, modrm_mod, index, disp): code_end = "\\n\\t\"\n" modrm = (modrm_mod << 6) | (inst.modrm_reg << 3) | register.modrm_rm - modrm_str = ", " + str(hex(modrm)) + modrm_str = ", " + str(my_hex(modrm)) mov_reg_str = "\t\"mov $" + str(my_hex(two_comp_64(index))) + ", " + register.mnemonic + "\\n\\t\"\n" segment_str, segment_chk_str = get_segment_prefix(segment, register, modrm) @@ -186,7 +182,7 @@ def generate_code(tc_nr, segment, inst, register, modrm_mod, index, disp): opcode_str = inst.opcode if (register.rex_b != 0): - rex_b_str = hex(register.rex_b) + ", " + rex_b_str = my_hex(register.rex_b) + ", " else: rex_b_str = "" @@ -201,14 +197,14 @@ def generate_code(tc_nr, segment, inst, register, modrm_mod, index, disp): comment += " + disp8). " elif (modrm_mod == 2): comment += " + disp32). " - comment += "EFF_ADDR[" + str(hex(index + disp)) + "]. " - comment += register.name + "[" + str(hex(index)) + "] " + comment += "EFF_ADDR[" + str(my_hex(index + disp)) + "]. " + comment += register.name + "[" + str(my_hex(index)) + "] " if (modrm_mod == 0): comment += "" elif (modrm_mod == 1): - comment += "disp8[" + str(hex(disp)) + "]" + comment += "disp8[" + str(my_hex(disp)) + "]" elif (modrm_mod == 2): - comment += "disp32[" + str(hex(disp)) + "]" + comment += "disp32[" + str(my_hex(disp)) + "]" code = "\t/* " + comment + " */\n" code += mov_reg_str @@ -224,8 +220,8 @@ def generate_code_sib(tc_nr, segment, inst, reg_base, reg_index, modrm_mod, sib_ modrm = (modrm_mod << 6) | (inst.modrm_reg << 3) | RSP.modrm_rm sib = (sib_scale << 6) | (reg_index.modrm_rm << 3) | reg_base.modrm_rm - modrm_str = ", " + str(hex(modrm)) - sib_str = ", " + str(hex(sib)) + modrm_str = ", " + str(my_hex(modrm)) + sib_str = ", " + str(my_hex(sib)) mov_reg_str = "\t\"mov $" + str(my_hex(two_comp_64(sib_base))) + ", " + reg_base.mnemonic + "\\n\\t\"\n" mov_reg_str += "\t\"mov $" + str(my_hex(two_comp_64(sib_index))) + ", " + reg_index.mnemonic + "\\n\\t\"\n" @@ -242,7 +238,7 @@ def generate_code_sib(tc_nr, segment, inst, reg_base, reg_index, modrm_mod, sib_ opcode_str = inst.opcode if ((reg_index.rex_x != 0) or (reg_base.rex_b != 0)): - rex_b_str = hex(reg_index.rex_x | reg_base.rex_b) + ", " + rex_b_str = my_hex(reg_index.rex_x | reg_base.rex_b) + ", " else: rex_b_str = "" @@ -272,17 +268,17 @@ def generate_code_sib(tc_nr, segment, inst, reg_base, reg_index, modrm_mod, sib_ eff_addr = calc_base + calc_index*(1<> 8) & 0xff)) + ", " - disp_str += str(hex((index >> 16) & 0xff)) + ", " - disp_str += str(hex((index >> 24) & 0xff)) + disp_str = ", " + str(my_hex(index & 0xff)) + ", " + disp_str += str(my_hex((index >> 8) & 0xff)) + ", " + disp_str += str(my_hex((index >> 16) & 0xff)) + ", " + disp_str += str(my_hex((index >> 24) & 0xff)) comment = "Ricardin Special Test case " + str(tc_nr) + ": " comment += "SEG[" + segment.name + "] " comment += "INSN: " + inst.name + " (disp32)." - comment += "EFF_ADDR[" + str(hex(index)) + "]." - comment += " disp32[" + str(hex(index)) + "]" + comment += "EFF_ADDR[" + str(my_hex(index)) + "]." + comment += " disp32[" + str(my_hex(index)) + "]" code = "\t/* " + comment + " */\n" code += "\t\"jmp 1f:\\n\\t\"\n" From f241a7bcba33618b6ee748e8da22a14fd5ef027f Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Fri, 24 Mar 2017 18:05:18 -0700 Subject: [PATCH 016/109] umip: umip_ldt_*: remove mmap MAP_32BIT option This option only makes sense when buiding in x86_64 builds for 64-bit programs. umip_ldt_32 and umip_ldt_16 are 32-bit programs. Reported-by: Qian Ouyang Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/umip_ldt_16.c | 4 ++-- meta-luv/recipes-core/umip/files/umip_ldt_32.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/umip_ldt_16.c b/meta-luv/recipes-core/umip/files/umip_ldt_16.c index 7b0084a6203..a16647642f6 100644 --- a/meta-luv/recipes-core/umip/files/umip_ldt_16.c +++ b/meta-luv/recipes-core/umip/files/umip_ldt_16.c @@ -210,7 +210,7 @@ int main(void) } code_interim = mmap(NULL, 4096, PROT_WRITE | PROT_READ | PROT_EXEC, - MAP_PRIVATE | MAP_32BIT | MAP_ANONYMOUS, -1, 0); + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (!code_interim) { pr_error("Failed to allocate memory for interim code segment!\n"); goto err_out; @@ -219,7 +219,7 @@ int main(void) memcpy(code_interim, interim, interim_end - interim); code_16 = mmap(NULL, CODE_MEM_SIZE, PROT_WRITE | PROT_READ | PROT_EXEC, - MAP_PRIVATE | MAP_32BIT | MAP_ANONYMOUS, -1, 0); + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (!code_16) { pr_error("Failed to allocate memory for code segment!\n"); goto err_out; diff --git a/meta-luv/recipes-core/umip/files/umip_ldt_32.c b/meta-luv/recipes-core/umip/files/umip_ldt_32.c index 555c008ff91..98426f57122 100644 --- a/meta-luv/recipes-core/umip/files/umip_ldt_32.c +++ b/meta-luv/recipes-core/umip/files/umip_ldt_32.c @@ -158,7 +158,7 @@ int main(void) } code = mmap(NULL, CODE_MEM_SIZE, PROT_WRITE | PROT_READ | PROT_EXEC, - MAP_PRIVATE | MAP_32BIT | MAP_ANONYMOUS, -1, 0); + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (!code) { pr_error("Failed to allocate memory for code segment!\n"); goto err_out; From 4d3aae85bdc03e070f6076d15084b949a958afa7 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Fri, 24 Mar 2017 18:10:36 -0700 Subject: [PATCH 017/109] umip: umip_ldt_*: do not run tests from main For reasons beyond my comprehension, certain versions of gcc use esp, not ebp, to refer to automatic variables in main. This is only true for main. This is a problem for these programs as they manipulate the stack. Hence, introduce a new function, called from main, that executes the actual tests. Reported-by: Qian Ouyang Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/umip_ldt_16.c | 6 +++++- meta-luv/recipes-core/umip/files/umip_ldt_32.c | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/umip_ldt_16.c b/meta-luv/recipes-core/umip/files/umip_ldt_16.c index a16647642f6..97df9ca74f7 100644 --- a/meta-luv/recipes-core/umip/files/umip_ldt_16.c +++ b/meta-luv/recipes-core/umip/files/umip_ldt_16.c @@ -177,7 +177,7 @@ static int setup_data_segments() return 0; } -int main(void) +int run_umip_ldt_test(void) { int ret; unsigned short interim_cs, interim_ss; @@ -360,3 +360,7 @@ int main(void) }; +int main(void) +{ + return run_umip_ldt_test(); +} diff --git a/meta-luv/recipes-core/umip/files/umip_ldt_32.c b/meta-luv/recipes-core/umip/files/umip_ldt_32.c index 98426f57122..ac0d731a3bc 100644 --- a/meta-luv/recipes-core/umip/files/umip_ldt_32.c +++ b/meta-luv/recipes-core/umip/files/umip_ldt_32.c @@ -127,7 +127,7 @@ static int setup_data_segments() return 0; } -int main(void) +int run_umip_ldt_test(void) { int ret; unsigned short test_cs, test_ds, test_ss; @@ -273,3 +273,7 @@ int main(void) }; +int main(void) +{ + return run_umip_ldt_test(); +} From 378fadda5b2b3f132bd7f310bb8f60f1193c2d9d Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Tue, 28 Mar 2017 12:34:23 -0700 Subject: [PATCH 018/109] umip: normal_pf: relocate page fault tests to its own function We do this for two reasons: a) some versions of gcc use r/esp, not r/ebp, to reference automatic variables. This has shown to cause problems in the past if we need to update r/esp. b) more exception handling tests will be added to this file. This is preparatory work. Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/normal_pf.c | 53 ++++++++++++-------- 1 file changed, 32 insertions(+), 21 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/normal_pf.c b/meta-luv/recipes-core/umip/files/normal_pf.c index fff7e3b720d..593e06aca8c 100644 --- a/meta-luv/recipes-core/umip/files/normal_pf.c +++ b/meta-luv/recipes-core/umip/files/normal_pf.c @@ -55,23 +55,10 @@ struct my_struct { int c; }; -int main (void) +int test_normal_pf(void) { - struct sigaction action; unsigned long *val_bad = (unsigned long *)0x100000; - PRINT_BITNESS; - - memset(&action, 0, sizeof(action)); - action.sa_sigaction = &handler; - action.sa_flags = SA_SIGINFO; - sigemptyset(&action.sa_mask); - - if (sigaction(SIGSEGV, &action, NULL) < 0) { - pr_error("Could not set the signal handler!\n"); - exit(1); - } - asm volatile ("smsw %0\n" "nop\n" "nop\n" @@ -88,16 +75,40 @@ int main (void) return 1; } #ifdef __x86_64__ - if (signal_code != SI_KERNEL) + if (signal_code != SI_KERNEL) { pr_fail("Signal code is not what we expect.\n"); - else - pr_pass("A SEGV_MAPERR page fault was issued.\n"); + return 1; + } + pr_pass("A SEGV_MAPERR page fault was issued.\n"); + return 0; #else - if (signal_code != SEGV_MAPERR) + if (signal_code != SEGV_MAPERR) { pr_fail("Signal code is not what we expect.\n"); - else - pr_pass("A SEGV_MAPERR page fault was issued.\n"); + return 1; + } + pr_pass("A SEGV_MAPERR page fault was issued.\n"); + return 0; #endif +} + +int main (void) +{ + struct sigaction action; + int ret; + + PRINT_BITNESS; + + memset(&action, 0, sizeof(action)); + action.sa_sigaction = &handler; + action.sa_flags = SA_SIGINFO; + sigemptyset(&action.sa_mask); + + if (sigaction(SIGSEGV, &action, NULL) < 0) { + pr_error("Could not set the signal handler!\n"); + exit(1); + } + + ret = test_normal_pf(); memset(&action, 0, sizeof(action)); action.sa_handler = SIG_DFL; @@ -108,5 +119,5 @@ int main (void) return 1; } - return 0; + return ret; } From db9ff11a6f7ef9eac57b2ea33843c6a7d96a4cc5 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Tue, 28 Mar 2017 15:13:37 -0700 Subject: [PATCH 019/109] umip: normal_pf: add support for SIGILL signal Add support in the signal handler for SIGILL. Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/normal_pf.c | 46 +++++++++++++++----- 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/normal_pf.c b/meta-luv/recipes-core/umip/files/normal_pf.c index 593e06aca8c..20ad94357e8 100644 --- a/meta-luv/recipes-core/umip/files/normal_pf.c +++ b/meta-luv/recipes-core/umip/files/normal_pf.c @@ -26,16 +26,28 @@ void handler(int signum, siginfo_t *info, void *ctx_void) pr_info("si_errno[%d]\n", info->si_errno); pr_info("si_code[%d]\n", info->si_code); pr_info("si_code[0x%p]\n", info->si_addr); - if (signum != SIGSEGV) - errx(1, "ERROR: Received unexpected signal"); - else - got_signal = signum; - if (info->si_code == SEGV_MAPERR) - pr_info("Signal because of unmapped object.\n"); - else if (info->si_code == SI_KERNEL) - pr_info("Signal because of #GP\n"); + + got_signal = signum; + + if (signum == SIGSEGV) { + if (info->si_code == SEGV_MAPERR) + pr_info("Signal because of unmapped object.\n"); + else if (info->si_code == SI_KERNEL) + pr_info("Signal because of #GP\n"); + else + pr_info("Unknown si_code!\n"); + } + else if (signum == SIGILL) { + if (info->si_code == SEGV_MAPERR) + pr_info("Signal because of unmapped object.\n"); + else if (info->si_code == ILL_ILLOPN) + pr_info("Signal because of #UD\n"); + else + pr_info("Unknown si_code!\n"); + } else - pr_info("Unknown si_code!\n"); + errx(1, "ERROR: Received unexpected signal"); + /* Save the signal code */ signal_code = info->si_code; /* @@ -104,9 +116,14 @@ int main (void) sigemptyset(&action.sa_mask); if (sigaction(SIGSEGV, &action, NULL) < 0) { - pr_error("Could not set the signal handler!\n"); + pr_error("Could not set the signal handler for SIGSEGV!\n"); exit(1); - } + } + + if (sigaction(SIGILL, &action, NULL) < 0) { + pr_error("Could not set the signal handler SIGILL!\n"); + exit(1); + } ret = test_normal_pf(); @@ -115,7 +132,12 @@ int main (void) sigemptyset(&action.sa_mask); if (sigaction(SIGSEGV, &action, NULL) < 0) { - pr_error("Could not remove signal handler!\n"); + pr_error("Could not remove signal SIGSEGV handler!\n"); + return 1; + } + + if (sigaction(SIGILL, &action, NULL) < 0) { + pr_error("Could not remove signal SIGILL handler!\n"); return 1; } From 4f2286ea1f1055fdeef5c4001279593d28ba249d Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Tue, 28 Mar 2017 15:17:33 -0700 Subject: [PATCH 020/109] umip: normal_pf: add tests for the LOCK prefix Add tests to verify that the UMIP-protected instructions generate a SIGILL when the LOCK instruction prefix is used. Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/normal_pf.c | 56 ++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/meta-luv/recipes-core/umip/files/normal_pf.c b/meta-luv/recipes-core/umip/files/normal_pf.c index 20ad94357e8..3766a2b2123 100644 --- a/meta-luv/recipes-core/umip/files/normal_pf.c +++ b/meta-luv/recipes-core/umip/files/normal_pf.c @@ -103,6 +103,58 @@ int test_normal_pf(void) #endif } +#define gen_test_lock_prefix_inst(name, inst) \ +static int __test_lock_prefix_##name(void) \ +{ \ + pr_info("Test %s with lock prefix\n", #name); \ + /* name (%eax) with the LOCK prefix */ \ + asm volatile(inst \ + "nop\n" \ + "nop\n" \ + "nop\n" \ + "nop\n"); \ + \ + if (signal_code != ILL_ILLOPN) { \ + pr_fail("Signal code is not what we expect.\n"); \ + signal_code = 0; \ + return 1; \ + } \ + pr_pass("An ILL_ILLOPN exception was issued.\n"); \ + signal_code = 0; \ + \ + return 0; \ +} + +gen_test_lock_prefix_inst(SMSW, ".byte 0xf0, 0xf, 0x1, 0x20\n") +gen_test_lock_prefix_inst(SIDT, ".byte 0xf0, 0xf, 0x1, 0x8\n") +gen_test_lock_prefix_inst(SGDT, ".byte 0xf0, 0xf, 0x1, 0x0\n") +gen_test_lock_prefix_inst(STR, ".byte 0xf0, 0xf, 0x0, 0x8\n") +gen_test_lock_prefix_inst(SLDT, ".byte 0xf0, 0xf, 0x0, 0x0\n") + +static int test_lock_prefix(void) +{ + int ret = 0; + ret = __test_lock_prefix_SMSW(); + if (ret) + return ret; + + ret = __test_lock_prefix_SIDT(); + if (ret) + return ret; + + ret = __test_lock_prefix_SGDT(); + if (ret) + return ret; + + ret = __test_lock_prefix_STR(); + if (ret) + return ret; + + ret = __test_lock_prefix_SLDT(); + + return ret; +} + int main (void) { struct sigaction action; @@ -126,6 +178,10 @@ int main (void) } ret = test_normal_pf(); + if (ret) + return 1; + + ret = test_lock_prefix(); memset(&action, 0, sizeof(action)); action.sa_handler = SIG_DFL; From 064542f883b38c99c1b8a38c53c6077995907d46 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Tue, 28 Mar 2017 16:28:32 -0700 Subject: [PATCH 021/109] umip: normal_pf: add tests for SGDT/SIDT with registers as operands The instructions SGDT and SIDT generate an #UD exception if the operands are registers. Tests that this actually happens. Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/normal_pf.c | 42 ++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/meta-luv/recipes-core/umip/files/normal_pf.c b/meta-luv/recipes-core/umip/files/normal_pf.c index 3766a2b2123..5bdcb3c269b 100644 --- a/meta-luv/recipes-core/umip/files/normal_pf.c +++ b/meta-luv/recipes-core/umip/files/normal_pf.c @@ -155,6 +155,44 @@ static int test_lock_prefix(void) return ret; } +#define gen_test_register_operand_inst(name, inst) \ +static int __test_register_operand_##name(void) \ +{ \ + pr_info("Test %s with register operand\n", #name); \ + /* name (%eax) with the LOCK prefix */ \ + asm volatile(inst \ + "nop\n" \ + "nop\n" \ + "nop\n" \ + "nop\n" \ + "nop\n"); \ + \ + if (signal_code != ILL_ILLOPN) { \ + pr_fail("Signal code is not what we expect.\n"); \ + signal_code = 0; \ + return 1; \ + } \ + pr_pass("An ILL_ILLOPN exception was issued.\n"); \ + signal_code = 0; \ + \ + return 0; \ +} + +gen_test_register_operand_inst(SIDT, ".byte 0xf, 0x1, 0xc8\n") +gen_test_register_operand_inst(SGDT, ".byte 0xf, 0x1, 0xc0\n") + +int test_register_operand(void) +{ + int ret; + + signal_code = 0; + ret = __test_register_operand_SGDT(); + if (ret) + return 1; + + ret = __test_register_operand_SIDT(); +} + int main (void) { struct sigaction action; @@ -182,6 +220,10 @@ int main (void) return 1; ret = test_lock_prefix(); + if (ret) + return 1; + + ret = test_register_operand(); memset(&action, 0, sizeof(action)); action.sa_handler = SIG_DFL; From 2503a7c7f5aad4e26e6cde82998b33cd0a94d747 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Wed, 29 Mar 2017 14:58:57 -0700 Subject: [PATCH 022/109] umip: normal_pf: test the use of null segment selectors In 32-bit programs, the use of null segment selectors should cause a #GP(0) exception. Verify that this indeed happens. Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/normal_pf.c | 142 +++++++++++++++++++ 1 file changed, 142 insertions(+) diff --git a/meta-luv/recipes-core/umip/files/normal_pf.c b/meta-luv/recipes-core/umip/files/normal_pf.c index 5bdcb3c269b..ddc5ac135a5 100644 --- a/meta-luv/recipes-core/umip/files/normal_pf.c +++ b/meta-luv/recipes-core/umip/files/normal_pf.c @@ -193,6 +193,144 @@ int test_register_operand(void) ret = __test_register_operand_SIDT(); } +#ifdef __x86_64__ +int test_null_segment_selectors(void) +{ +} +#else +#define gen_test_null_segment_selector(inst, reg) \ +static int __test_null_segment_selector_##inst##_##reg(void) \ +{ \ + pr_info("Test using null seg sel for " #inst " with " #reg "\n"); \ + asm volatile("push %" #reg "\n" \ + "push %eax\n" \ + "push %ebx\n" \ + "mov $0x1000, %eax\n" \ + "mov $0, %ebx\n" \ + "mov %bx, %" #reg "\n" \ + "smsw %" #reg ":(%eax)\n" \ + "nop\n" \ + "nop\n" \ + "nop\n" \ + "nop\n" \ + "nop\n" \ + "pop %ebx\n" \ + "pop %eax\n" \ + "pop %" #reg "\n"); \ + if (signal_code != SI_KERNEL) { \ + pr_fail("Signal code is not what we expect.\n"); \ + signal_code = 0; \ + return 1; \ + } \ + pr_pass("An ILL_ILLOPN exception was issued.\n"); \ + signal_code = 0; \ + \ + return 0; \ +} + +gen_test_null_segment_selector(smsw, ds) +gen_test_null_segment_selector(smsw, es) +gen_test_null_segment_selector(smsw, fs) +gen_test_null_segment_selector(smsw, gs) +gen_test_null_segment_selector(sidt, ds) +gen_test_null_segment_selector(sidt, es) +gen_test_null_segment_selector(sidt, fs) +gen_test_null_segment_selector(sidt, gs) +gen_test_null_segment_selector(sgdt, ds) +gen_test_null_segment_selector(sgdt, es) +gen_test_null_segment_selector(sgdt, fs) +gen_test_null_segment_selector(sgdt, gs) +gen_test_null_segment_selector(str, ds) +gen_test_null_segment_selector(str, es) +gen_test_null_segment_selector(str, fs) +gen_test_null_segment_selector(str, gs) +gen_test_null_segment_selector(sldt, ds) +gen_test_null_segment_selector(sldt, es) +gen_test_null_segment_selector(sldt, fs) +gen_test_null_segment_selector(sldt, gs) + +int test_null_segment_selectors(void) +{ + int ret; + + ret = __test_null_segment_selector_smsw_ds(); + if (ret) + return 1; + ret = __test_null_segment_selector_smsw_es(); + if (ret) + return 1; + ret = __test_null_segment_selector_smsw_fs(); + if (ret) + return 1; +#ifdef TEST_GS /* TODO: Meddling with gs breaks libc */ + ret = __test_null_segment_selector_smsw_gs(); + if (ret) + return 1; +#endif + + ret = __test_null_segment_selector_sidt_ds(); + if (ret) + return 1; + ret = __test_null_segment_selector_sidt_es(); + if (ret) + return 1; + ret = __test_null_segment_selector_sidt_fs(); + if (ret) + return 1; +#ifdef TEST_GS /* TODO: Meddling with gs breaks libc */ + ret = __test_null_segment_selector_sidt_gs(); + if (ret) + return 1; +#endif + + ret = __test_null_segment_selector_sgdt_ds(); + if (ret) + return 1; + ret = __test_null_segment_selector_sgdt_es(); + if (ret) + return 1; + ret = __test_null_segment_selector_sgdt_fs(); + if (ret) + return 1; +#ifdef TEST_GS /* TODO: Meddling with gs breaks libc */ + ret = __test_null_segment_selector_sgdt_gs(); + if (ret) + return 1; +#endif + + ret = __test_null_segment_selector_sldt_ds(); + if (ret) + return 1; + ret = __test_null_segment_selector_sldt_es(); + if (ret) + return 1; + ret = __test_null_segment_selector_sldt_fs(); + if (ret) + return 1; +#ifdef TEST_GS /* TODO: Meddling with gs breaks libc */ + ret = __test_null_segment_selector_sldt_gs(); + if (ret) + return 1; +#endif + + ret = __test_null_segment_selector_str_ds(); + if (ret) + return 1; + ret = __test_null_segment_selector_str_es(); + if (ret) + return 1; + ret = __test_null_segment_selector_str_fs(); + if (ret) + return 1; +#ifdef TEST_GS /* TODO: Meddling with gs breaks libc */ + ret = __test_null_segment_selector_str_gs(); + if (ret) + return 1; +#endif + return 0; +} +#endif + int main (void) { struct sigaction action; @@ -224,6 +362,10 @@ int main (void) return 1; ret = test_register_operand(); + if (ret) + return 1; + + ret = test_null_segment_selectors(); memset(&action, 0, sizeof(action)); action.sa_handler = SIG_DFL; From 05781d1c69364d652d73fc0aaebe25bac4c9ebaf Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Wed, 29 Mar 2017 21:25:22 -0700 Subject: [PATCH 023/109] umip: normal_pf: add tests for effective address outside of segment limits When the effective address is outside of the segment limits, a general protection fault should be issues. Test that this is the case. Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/normal_pf.c | 200 +++++++++++++++++++ 1 file changed, 200 insertions(+) diff --git a/meta-luv/recipes-core/umip/files/normal_pf.c b/meta-luv/recipes-core/umip/files/normal_pf.c index ddc5ac135a5..015a789620d 100644 --- a/meta-luv/recipes-core/umip/files/normal_pf.c +++ b/meta-luv/recipes-core/umip/files/normal_pf.c @@ -13,6 +13,8 @@ #include #include #include +#include +#include #include "umip_test_defs.h" static sig_atomic_t signal_code; @@ -331,6 +333,200 @@ int test_null_segment_selectors(void) } #endif +#ifdef __x86_64__ +int test_addresses_outside_segment(void) +{ +} +#else + +#define SEGMENT_SIZE 0x1000 +#define CODE_DESC_INDEX 1 +#define DATA_DESC_INDEX 2 + +#define RPL3 3 +#define TI_LDT 1 + +#define SEGMENT_SELECTOR(index) (RPL3 | (TI_LDT << 2) | (index << 3)) + +unsigned char custom_segment[SEGMENT_SIZE]; + +static int setup_data_segments() +{ + int ret; + struct user_desc desc = { + .entry_number = 0, + .base_addr = 0, + .limit = SEGMENT_SIZE, + .seg_32bit = 1, + .contents = 0, /* data */ + .read_exec_only = 0, + .limit_in_pages = 0, + .seg_not_present = 0, + .useable = 1 + }; + + desc.entry_number = DATA_DESC_INDEX; + desc.base_addr = (unsigned long)&custom_segment; + + ret = syscall(SYS_modify_ldt, 1, &desc, sizeof(desc)); + if (ret) { + pr_error("Failed to install stack semgnet [%d].\n", ret); + return ret; + } + + return 0; +} + +#define gen_test_addresses_outside_segment(inst, sel) \ +int __test_addresses_outside_segment_##inst##_##sel(void) \ +{ \ + int ret; \ + unsigned short seg_sel; \ + \ + seg_sel = SEGMENT_SELECTOR(DATA_DESC_INDEX); \ + \ + pr_info("Test address outside of segment limit for " #inst " with " #sel"\n");\ + asm volatile("push %%" #sel"\n" \ + "push %%eax\n" \ + "push %%ebx\n" \ + "mov $0x2000, %%eax\n" \ + "mov %0, %%" #sel "\n" \ + #inst " %%" #sel ":(%%eax)\n" \ + "nop\n" \ + "nop\n" \ + "nop\n" \ + "nop\n" \ + "nop\n" \ + "pop %%ebx\n" \ + "pop %%eax\n" \ + "pop %%" #sel "\n" \ + : \ + :"m" (seg_sel)); \ + \ + if (signal_code != SI_KERNEL) { \ + pr_fail("Signal code is not what we expect.\n"); \ + signal_code = 0; \ + return 1; \ + } \ + pr_pass("An ILL_ILLOPN exception was issued.\n"); \ + signal_code = 0; \ + \ + return 0; \ +} + +gen_test_addresses_outside_segment(smsw, ds) +gen_test_addresses_outside_segment(str, ds) +gen_test_addresses_outside_segment(sldt, ds) +gen_test_addresses_outside_segment(sgdt, ds) +gen_test_addresses_outside_segment(sidt, ds) +gen_test_addresses_outside_segment(smsw, es) +gen_test_addresses_outside_segment(str, es) +gen_test_addresses_outside_segment(sldt, es) +gen_test_addresses_outside_segment(sgdt, es) +gen_test_addresses_outside_segment(sidt, es) +gen_test_addresses_outside_segment(smsw, fs) +gen_test_addresses_outside_segment(str, fs) +gen_test_addresses_outside_segment(sldt, fs) +gen_test_addresses_outside_segment(sgdt, fs) +gen_test_addresses_outside_segment(sidt, fs) +gen_test_addresses_outside_segment(smsw, gs) +gen_test_addresses_outside_segment(str, gs) +gen_test_addresses_outside_segment(sldt, gs) +gen_test_addresses_outside_segment(sgdt, gs) +gen_test_addresses_outside_segment(sidt, gs) + +int test_addresses_outside_segment(void) +{ + int ret; + + ret = setup_data_segments(); + if (ret) + return 1; + + ret = __test_addresses_outside_segment_smsw_ds(); + if (ret) + return 1; + + ret = __test_addresses_outside_segment_str_ds(); + if (ret) + return 1; + + ret = __test_addresses_outside_segment_sgdt_ds(); + if (ret) + return 1; + + ret = __test_addresses_outside_segment_sidt_ds(); + if (ret) + return 1; + + ret = __test_addresses_outside_segment_sldt_ds(); + if (ret) + return 1; + + ret = __test_addresses_outside_segment_smsw_es(); + if (ret) + return 1; + + ret = __test_addresses_outside_segment_str_es(); + if (ret) + return 1; + + ret = __test_addresses_outside_segment_sgdt_es(); + if (ret) + return 1; + + ret = __test_addresses_outside_segment_sidt_es(); + if (ret) + return 1; + + ret = __test_addresses_outside_segment_sldt_es(); + if (ret) + return 1; + + ret = __test_addresses_outside_segment_smsw_fs(); + if (ret) + return 1; + + ret = __test_addresses_outside_segment_str_fs(); + if (ret) + return 1; + + ret = __test_addresses_outside_segment_sgdt_fs(); + if (ret) + return 1; + + ret = __test_addresses_outside_segment_sidt_fs(); + if (ret) + return 1; + + ret = __test_addresses_outside_segment_sldt_fs(); + if (ret) + return 1; + +#ifdef TEST_GS + ret = __test_addresses_outside_segment_smsw_gs(); + if (ret) + return 1; + + ret = __test_addresses_outside_segment_str_gs(); + if (ret) + return 1; + + ret = __test_addresses_outside_segment_sgdt_gs(); + if (ret) + return 1; + + ret = __test_addresses_outside_segment_sidt_gs(); + if (ret) + return 1; + + ret = __test_addresses_outside_segment_sldt_gs(); + if (ret) + return 1; +#endif +} +#endif + int main (void) { struct sigaction action; @@ -366,6 +562,10 @@ int main (void) return 1; ret = test_null_segment_selectors(); + if (ret) + return 1; + + test_addresses_outside_segment(); memset(&action, 0, sizeof(action)); action.sa_handler = SIG_DFL; From 15b7b98ce3734da9df339612a446901e13354732 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Tue, 2 May 2017 14:50:26 -0700 Subject: [PATCH 024/109] linux-yocto-efi-test: Update UMIP tests Test cases were reworked and new test cases were added. Signed-off-by: Ricardo Neri --- ...-tests-for-User-Mode-Instruction-Pr.patch} | 112 ++++++++++++------ ...d-tests-for-instruction-str-and-sldt.patch | 84 +++++++++++++ ...-more-tests-for-User-Mode-Instructi.patch} | 53 +++++---- .../linux/linux-yocto-efi-test_4.12.bb | 6 +- 4 files changed, 191 insertions(+), 64 deletions(-) rename meta-luv/recipes-kernel/linux/linux-yocto-efi-test/{0001-selftests-x86-Add-tests-for-User-Mode-Instruction-Pr.patch => 0023-selftests-x86-Add-tests-for-User-Mode-Instruction-Pr.patch} (50%) create mode 100644 meta-luv/recipes-kernel/linux/linux-yocto-efi-test/0024-selftests-x86-Add-tests-for-instruction-str-and-sldt.patch rename meta-luv/recipes-kernel/linux/linux-yocto-efi-test/{0002-selftests-x86-Add-more-tests-for-User-Mode-Instructi.patch => 0025-selftests-x86-Add-more-tests-for-User-Mode-Instructi.patch} (92%) diff --git a/meta-luv/recipes-kernel/linux/linux-yocto-efi-test/0001-selftests-x86-Add-tests-for-User-Mode-Instruction-Pr.patch b/meta-luv/recipes-kernel/linux/linux-yocto-efi-test/0023-selftests-x86-Add-tests-for-User-Mode-Instruction-Pr.patch similarity index 50% rename from meta-luv/recipes-kernel/linux/linux-yocto-efi-test/0001-selftests-x86-Add-tests-for-User-Mode-Instruction-Pr.patch rename to meta-luv/recipes-kernel/linux/linux-yocto-efi-test/0023-selftests-x86-Add-tests-for-User-Mode-Instruction-Pr.patch index 7efc6d9f785..4a03a30ae3c 100644 --- a/meta-luv/recipes-kernel/linux/linux-yocto-efi-test/0001-selftests-x86-Add-tests-for-User-Mode-Instruction-Pr.patch +++ b/meta-luv/recipes-kernel/linux/linux-yocto-efi-test/0023-selftests-x86-Add-tests-for-User-Mode-Instruction-Pr.patch @@ -1,29 +1,30 @@ -From 1a0461d77254759e3990e06171f9fdcfdbad3e38 Mon Sep 17 00:00:00 2001 +From 50da99041a73ad68cd7e2f00df5dc2e9e3dc8a09 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Tue, 9 Aug 2016 12:48:24 -0700 -Subject: [PATCH 10/11] selftests/x86: Add tests for User-Mode Instruction +Subject: [PATCH 23/26] selftests/x86: Add tests for User-Mode Instruction Prevention Certain user space programs that run on virtual-8086 mode may utilize instructions protected by the User-Mode Instruction Prevention (UMIP) security feature present in new Intel processors: SGDT, SIDT and SMSW. In such a case, a general protection fault is issued if UMIP is enabled. When -such a fault happens, the kernel catches it and emulates the results of +such a fault happens, the kernel traps it and emulates the results of these instructions with dummy values. The purpose of this new -test is to verify whether the impacted instructions can be executed without -causing such #GP. If no #GP exceptions occur, we expect to exit virtual- -8086 mode from INT 0x80. +test is to verify whether the impacted instructions can be executed +without causing such #GP. If no #GP exceptions occur, we expect to exit +virtual-8086 mode from INT3. The instructions protected by UMIP are executed in representative use cases: - a) the memory address of the result is given in the form of a displacement - from the base of the data segment - b) the memory address of the result is given in a general purpose register - c) the result is stored directly in a general purpose register. + a) displacement-only memory addressing + b) register-indirect memory addressing + c) results stored directly in operands Unfortunately, it is not possible to check the results against a set of -expected values because no emulation will occur in systems that do not have -the UMIP feature. Instead, results are printed for verification. +expected values because no emulation will occur in systems that do not +have the UMIP feature. Instead, results are printed for verification. A +simple verification is done to ensure that results of all tests are +identical. Cc: Andy Lutomirski Cc: Andrew Morton @@ -44,18 +45,18 @@ Cc: Shuah Khan Cc: Vlastimil Babka Signed-off-by: Ricardo Neri --- - tools/testing/selftests/x86/entry_from_vm86.c | 39 ++++++++++++++++++++++++++- - 1 file changed, 38 insertions(+), 1 deletion(-) + tools/testing/selftests/x86/entry_from_vm86.c | 67 ++++++++++++++++++++++++++- + 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/x86/entry_from_vm86.c b/tools/testing/selftests/x86/entry_from_vm86.c -index d075ea0..377b773 100644 +index d075ea0..1c39e75 100644 --- a/tools/testing/selftests/x86/entry_from_vm86.c +++ b/tools/testing/selftests/x86/entry_from_vm86.c @@ -95,6 +95,22 @@ asm ( "int3\n\t" "vmcode_int80:\n\t" "int $0x80\n\t" -+ "umip:\n\t" ++ "vmcode_umip:\n\t" + /* addressing via displacements */ + "smsw (2052)\n\t" + "sidt (2054)\n\t" @@ -70,7 +71,7 @@ index d075ea0..377b773 100644 + /* register operands, only for smsw */ + "smsw %ax\n\t" + "mov %ax, (2080)\n\t" -+ "int $0x80\n\t" ++ "int3\n\t" ".size vmcode, . - vmcode\n\t" "end_vmcode:\n\t" ".code32\n\t" @@ -79,34 +80,69 @@ index d075ea0..377b773 100644 extern unsigned char vmcode[], end_vmcode[]; extern unsigned char vmcode_bound[], vmcode_sysenter[], vmcode_syscall[], - vmcode_sti[], vmcode_int3[], vmcode_int80[]; -+ vmcode_sti[], vmcode_int3[], vmcode_int80[], umip[]; ++ vmcode_sti[], vmcode_int3[], vmcode_int80[], vmcode_umip[]; /* Returns false if the test was skipped. */ static bool do_test(struct vm86plus_struct *v86, unsigned long eip, -@@ -218,6 +234,27 @@ int main(void) +@@ -160,6 +176,52 @@ static bool do_test(struct vm86plus_struct *v86, unsigned long eip, + return true; + } + ++void do_umip_tests(struct vm86plus_struct *vm86, unsigned char *test_mem) ++{ ++ struct table_desc { ++ unsigned short limit; ++ unsigned long base; ++ } __attribute__((packed)); ++ ++ struct table_desc *gdt1, *gdt2, *idt1, *idt2; ++ unsigned short msw1, msw2, msw3; ++ ++ /* UMIP -- exit with INT3 unless kernel emulation did not trap #GP */ ++ do_test(vm86, vmcode_umip - vmcode, VM86_TRAP, 3, "UMIP tests"); ++ ++ /* Results from displacement-only addressing */ ++ msw1 = *(unsigned short *)(test_mem + 2052); ++ idt1 = (struct table_desc *)(test_mem + 2054); ++ gdt1 = (struct table_desc *)(test_mem + 2060); ++ ++ /* Results from register-indirect addressing */ ++ msw2 = *(unsigned short *)(test_mem + 2066); ++ idt2 = (struct table_desc *)(test_mem + 2068); ++ gdt2 = (struct table_desc *)(test_mem + 2074); ++ ++ /* Results when using register operands */ ++ msw3 = *(unsigned short *)(test_mem + 2080); ++ ++ printf("[INFO]\tResult from SMSW:[0x%04x]\n", msw1); ++ printf("[INFO]\tResult from SIDT: limit[0x%04x]base[0x%08lx]\n", idt1->limit, idt1->base); ++ printf("[INFO]\tResult from SGDT: limit[0x%04x]base[0x%08lx]\n", gdt1->limit, gdt1->base); ++ ++ if ((msw1 != msw2) || (msw1 != msw3)) ++ printf("[FAIL]\tAll the results of SMSW should be the same.\n"); ++ else ++ printf("[PASS]\tAll the results from SMSW are identical.\n"); ++ ++ if (memcmp(gdt1, gdt2, sizeof(*gdt1))) ++ printf("[FAIL]\tAll the results of SGDT should be the same.\n"); ++ else ++ printf("[PASS]\tAll the results from SGDT are identical.\n"); ++ ++ if (memcmp(idt1, idt2, sizeof(*idt1))) ++ printf("[FAIL]\tAll the results of SIDT should be the same.\n"); ++ else ++ printf("[PASS]\tAll the results from SIDT are identical.\n"); ++} ++ + int main(void) + { + struct vm86plus_struct v86; +@@ -218,6 +280,9 @@ int main(void) v86.regs.eax = (unsigned int)-1; do_test(&v86, vmcode_int80 - vmcode, VM86_INTx, 0x80, "int80"); + /* UMIP -- should exit with INTx 0x80 unless UMIP was not disabled */ -+ do_test(&v86, umip - vmcode, VM86_INTx, 0x80, "UMIP tests"); -+ printf("[INFO]\tResults of UMIP-protected instructions via displacements:\n"); -+ printf("[INFO]\tSMSW:[0x%04x]\n", *(unsigned short *)(addr + 2052)); -+ printf("[INFO]\tSIDT: limit[0x%04x]base[0x%08lx]\n", -+ *(unsigned short *)(addr + 2054), -+ *(unsigned long *)(addr + 2056)); -+ printf("[INFO]\tSGDT: limit[0x%04x]base[0x%08lx]\n", -+ *(unsigned short *)(addr + 2060), -+ *(unsigned long *)(addr + 2062)); -+ printf("[INFO]\tResults of UMIP-protected instructions via addressing in registers:\n"); -+ printf("[INFO]\tSMSW:[0x%04x]\n", *(unsigned short *)(addr + 2066)); -+ printf("[INFO]\tSIDT: limit[0x%04x]base[0x%08lx]\n", -+ *(unsigned short *)(addr + 2068), -+ *(unsigned long *)(addr + 2070)); -+ printf("[INFO]\tSGDT: limit[0x%04x]base[0x%08lx]\n", -+ *(unsigned short *)(addr + 2074), -+ *(unsigned long *)(addr + 2076)); -+ printf("[INFO]\tResults of SMSW via register operands:\n"); -+ printf("[INFO]\tSMSW:[0x%04x]\n", *(unsigned short *)(addr + 2080)); ++ do_umip_tests(&v86, addr); + /* Execute a null pointer */ v86.regs.cs = 0; diff --git a/meta-luv/recipes-kernel/linux/linux-yocto-efi-test/0024-selftests-x86-Add-tests-for-instruction-str-and-sldt.patch b/meta-luv/recipes-kernel/linux/linux-yocto-efi-test/0024-selftests-x86-Add-tests-for-instruction-str-and-sldt.patch new file mode 100644 index 00000000000..ad2bfe64e44 --- /dev/null +++ b/meta-luv/recipes-kernel/linux/linux-yocto-efi-test/0024-selftests-x86-Add-tests-for-instruction-str-and-sldt.patch @@ -0,0 +1,84 @@ +From b4cd52105d035e443ed0469a1f2712e7e8c46fcc Mon Sep 17 00:00:00 2001 +From: Ricardo Neri +Date: Tue, 2 May 2017 14:13:07 -0700 +Subject: [PATCH 24/26] selftests/x86: Add tests for instruction str and sldt + +The instructions str and sldt are not recognized when running on virtual- +8086 mode and generate an invalid operand exception. These two +instructions are protected by the Intel User-Mode Instruction Prevention +(UMIP) security feature. In protected mode, if UMIP is enabled, these +instructions generate a general protection fault if called from CPL > 0. +Linux traps the general protection fault and emulate the results with +dummy values. + +These tests are added to verify that the emulation code does not emulate +these two instructions but issue the expected invalid operand exception. + +Tests fallback to exit with int3 in case emulation does happen. + +Cc: Andy Lutomirski +Cc: Andrew Morton +Cc: Borislav Petkov +Cc: Brian Gerst +Cc: Chen Yucong +Cc: Chris Metcalf +Cc: Dave Hansen +Cc: Fenghua Yu +Cc: Huang Rui +Cc: Jiri Slaby +Cc: Jonathan Corbet +Cc: Michael S. Tsirkin +Cc: Paul Gortmaker +Cc: Peter Zijlstra +Cc: Ravi V. Shankar +Cc: Shuah Khan +Cc: Vlastimil Babka +Signed-off-by: Ricardo Neri +--- + tools/testing/selftests/x86/entry_from_vm86.c | 16 +++++++++++++++- + 1 file changed, 15 insertions(+), 1 deletion(-) + +diff --git a/tools/testing/selftests/x86/entry_from_vm86.c b/tools/testing/selftests/x86/entry_from_vm86.c +index 1c39e75..a335400 100644 +--- a/tools/testing/selftests/x86/entry_from_vm86.c ++++ b/tools/testing/selftests/x86/entry_from_vm86.c +@@ -111,6 +111,11 @@ asm ( + "smsw %ax\n\t" + "mov %ax, (2080)\n\t" + "int3\n\t" ++ "vmcode_umip_str:\n\t" ++ "str %eax\n\t" ++ "vmcode_umip_sldt:\n\t" ++ "sldt %eax\n\t" ++ "int3\n\t" + ".size vmcode, . - vmcode\n\t" + "end_vmcode:\n\t" + ".code32\n\t" +@@ -119,7 +124,8 @@ asm ( + + extern unsigned char vmcode[], end_vmcode[]; + extern unsigned char vmcode_bound[], vmcode_sysenter[], vmcode_syscall[], +- vmcode_sti[], vmcode_int3[], vmcode_int80[], vmcode_umip[]; ++ vmcode_sti[], vmcode_int3[], vmcode_int80[], vmcode_umip[], ++ vmcode_umip_str[], vmcode_umip_sldt[]; + + /* Returns false if the test was skipped. */ + static bool do_test(struct vm86plus_struct *v86, unsigned long eip, +@@ -220,6 +226,14 @@ void do_umip_tests(struct vm86plus_struct *vm86, unsigned char *test_mem) + printf("[FAIL]\tAll the results of SIDT should be the same.\n"); + else + printf("[PASS]\tAll the results from SIDT are identical.\n"); ++ ++ sethandler(SIGILL, sighandler, 0); ++ do_test(vm86, vmcode_umip_str - vmcode, VM86_SIGNAL, 0, "str instruction"); ++ clearhandler(SIGILL); ++ ++ sethandler(SIGILL, sighandler, 0); ++ do_test(vm86, vmcode_umip_sldt - vmcode, VM86_SIGNAL, 0, "sldt instruction"); ++ clearhandler(SIGILL); + } + + int main(void) +-- +2.9.3 + diff --git a/meta-luv/recipes-kernel/linux/linux-yocto-efi-test/0002-selftests-x86-Add-more-tests-for-User-Mode-Instructi.patch b/meta-luv/recipes-kernel/linux/linux-yocto-efi-test/0025-selftests-x86-Add-more-tests-for-User-Mode-Instructi.patch similarity index 92% rename from meta-luv/recipes-kernel/linux/linux-yocto-efi-test/0002-selftests-x86-Add-more-tests-for-User-Mode-Instructi.patch rename to meta-luv/recipes-kernel/linux/linux-yocto-efi-test/0025-selftests-x86-Add-more-tests-for-User-Mode-Instructi.patch index 094e96bb777..6f065536e2e 100644 --- a/meta-luv/recipes-kernel/linux/linux-yocto-efi-test/0002-selftests-x86-Add-more-tests-for-User-Mode-Instructi.patch +++ b/meta-luv/recipes-kernel/linux/linux-yocto-efi-test/0025-selftests-x86-Add-more-tests-for-User-Mode-Instructi.patch @@ -1,7 +1,7 @@ -From a47399192fed6e0c9cb8ae3a3af1bb158636e0fb Mon Sep 17 00:00:00 2001 +From 606f8d1225db3f145217858a4e6c62ae64bcb775 Mon Sep 17 00:00:00 2001 From: Ricardo Neri -Date: Mon, 23 Jan 2017 18:31:35 -0800 -Subject: [PATCH 11/11] selftests/x86: Add more tests for User-Mode Instruction +Date: Tue, 2 May 2017 14:15:47 -0700 +Subject: [PATCH 25/26] selftests/x86: Add more tests for User-Mode Instruction Prevention Add many tests the UMIP emulation done in the kernel. This tests all @@ -10,11 +10,11 @@ registers, 0, 1, 2-byte displacements as well as register operands. Signed-off-by: Ricardo Neri --- - tools/testing/selftests/x86/entry_from_vm86.c | 342 +++++++++++++++++++++++++- - 1 file changed, 338 insertions(+), 4 deletions(-) + tools/testing/selftests/x86/entry_from_vm86.c | 357 +++++++++++++++++++++++++- + 1 file changed, 355 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/x86/entry_from_vm86.c b/tools/testing/selftests/x86/entry_from_vm86.c -index 377b773..c7bf946 100644 +index a335400..2bb666c 100644 --- a/tools/testing/selftests/x86/entry_from_vm86.c +++ b/tools/testing/selftests/x86/entry_from_vm86.c @@ -100,16 +100,162 @@ asm ( @@ -179,26 +179,33 @@ index 377b773..c7bf946 100644 + "mov %si, (2372)\n\t" + "smsw %di\n\t" + "mov %di, (2374)\n\t" - "int $0x80\n\t" - ".size vmcode, . - vmcode\n\t" - "end_vmcode:\n\t" -@@ -244,7 +390,7 @@ int main(void) - printf("[INFO]\tSGDT: limit[0x%04x]base[0x%08lx]\n", - *(unsigned short *)(addr + 2060), - *(unsigned long *)(addr + 2062)); -- printf("[INFO]\tResults of UMIP-protected instructions via addressing in registers:\n"); + "int3\n\t" + "vmcode_umip_str:\n\t" + "str %eax\n\t" +@@ -296,6 +442,213 @@ int main(void) + + /* UMIP -- should exit with INTx 0x80 unless UMIP was not disabled */ + do_umip_tests(&v86, addr); ++ do_test(&v86, vmcode_umip - vmcode, VM86_TRAP, 3, "UMIP tests"); ++ printf("[INFO]\tResults of UMIP-protected instructions via displacements:\n"); ++ printf("[INFO]\tSMSW:[0x%04x]\n", *(unsigned short *)(addr + 2052)); ++ printf("[INFO]\tSIDT: limit[0x%04x]base[0x%08lx]\n", ++ *(unsigned short *)(addr + 2054), ++ *(unsigned long *)(addr + 2056)); ++ printf("[INFO]\tSGDT: limit[0x%04x]base[0x%08lx]\n", ++ *(unsigned short *)(addr + 2060), ++ *(unsigned long *)(addr + 2062)); + printf("[INFO]\tResults of UMIP-protected instructions via addressing in registers: no disp, bx\n"); - printf("[INFO]\tSMSW:[0x%04x]\n", *(unsigned short *)(addr + 2066)); - printf("[INFO]\tSIDT: limit[0x%04x]base[0x%08lx]\n", - *(unsigned short *)(addr + 2068), -@@ -252,8 +398,196 @@ int main(void) - printf("[INFO]\tSGDT: limit[0x%04x]base[0x%08lx]\n", - *(unsigned short *)(addr + 2074), - *(unsigned long *)(addr + 2076)); -- printf("[INFO]\tResults of SMSW via register operands:\n"); ++ printf("[INFO]\tSMSW:[0x%04x]\n", *(unsigned short *)(addr + 2066)); ++ printf("[INFO]\tSIDT: limit[0x%04x]base[0x%08lx]\n", ++ *(unsigned short *)(addr + 2068), ++ *(unsigned long *)(addr + 2070)); ++ printf("[INFO]\tSGDT: limit[0x%04x]base[0x%08lx]\n", ++ *(unsigned short *)(addr + 2074), ++ *(unsigned long *)(addr + 2076)); + + printf("[INFO]\tResults of UMIP-protected instructions via addressing in registers: no disp, si\n"); - printf("[INFO]\tSMSW:[0x%04x]\n", *(unsigned short *)(addr + 2080)); ++ printf("[INFO]\tSMSW:[0x%04x]\n", *(unsigned short *)(addr + 2080)); + printf("[INFO]\tSIDT: limit[0x%04x]base[0x%08lx]\n", + *(unsigned short *)(addr + 2082), + *(unsigned long *)(addr + 2084)); diff --git a/meta-luv/recipes-kernel/linux/linux-yocto-efi-test_4.12.bb b/meta-luv/recipes-kernel/linux/linux-yocto-efi-test_4.12.bb index e0b9b1629cc..7f6f0f21ff3 100644 --- a/meta-luv/recipes-kernel/linux/linux-yocto-efi-test_4.12.bb +++ b/meta-luv/recipes-kernel/linux/linux-yocto-efi-test_4.12.bb @@ -64,7 +64,6 @@ SRC_URI = "git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git;prot # These patches are under discussion on ML SRC_URI += "file://0001-serial-SPCR-check-bit-width-for-the-16550-UART.patch \ " - # Detect illegal accesses to EFI regions (like EFI_CONVENTIONAL_MEMORY, # EFI_LOADER_CODE/DATA, EFI_BOOT_SERVICES_CODE/DATA) by firmware. SRC_URI += "file://0001-x86-mm-Allocate-pages-without-sleeping.patch \ @@ -77,8 +76,9 @@ SRC_URI += "file://0001-x86-mm-Allocate-pages-without-sleeping.patch \ file://0008-x86-efi-Introduce-EFI_WARN_ON_ILLEGAL_ACCESSES.patch \ file://0001-PCI-Vulcan-AHCI-PCI-bar-fix-for-Broadcom-Vulcan-earl.patch \ file://0002-ahci-thunderx2-Fix-for-errata-that-affects-stop-engi.patch \ - file://0001-selftests-x86-Add-tests-for-User-Mode-Instruction-Pr.patch \ - file://0002-selftests-x86-Add-more-tests-for-User-Mode-Instructi.patch \ + file://0023-selftests-x86-Add-tests-for-User-Mode-Instruction-Pr.patch \ + file://0024-selftests-x86-Add-tests-for-instruction-str-and-sldt.patch \ + file://0025-selftests-x86-Add-more-tests-for-User-Mode-Instructi.patch \ " COMMON_CFG_x86 = " file://qemux86/modules.cfg \ From 1bdecbe7250f406c0e8cf7528c767243686c254a Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Fri, 26 May 2017 20:06:43 -0700 Subject: [PATCH 025/109] umip: update README to refer to more exceptions umip_pf has been updated to test for more exceptions in addition to page faults. Reflect the documentation accordingly. Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/UMIP_README | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/UMIP_README b/meta-luv/recipes-core/umip/files/UMIP_README index ac28d240914..3fc82ded37c 100644 --- a/meta-luv/recipes-core/umip/files/UMIP_README +++ b/meta-luv/recipes-core/umip/files/UMIP_README @@ -16,8 +16,8 @@ A quick guide for UMIP testing 16-bit long. Addresses in these tests are computed using only the ModRM byte. Also, these tests use the normal __USER_DS segment with a base address of zero. -* Use umip_pf to verify that a page fault is correctly issued when UMIP- - emulated code tries to write in an invalid memory address of the user +* Use umip_exceptions to verify that a page fault is correctly issued when + UMIP-emulated code tries to write in an invalid memory address of the user space memory. * Use umip_ldt to verify all the possible 32-bit address encodings in the UMIP-emulated instructions. This includes all the ModRM, and SiB displacement @@ -29,4 +29,3 @@ A quick guide for UMIP testing combinations. Furthermore, this test configure a Local Descriptor Table whose segments have base addresses that are different from the __USER_DS segment. Also, the code segment is configured for 16-bit addresses. - From b8e1b85bb64ad29d59730136f20362ecf038d842 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Fri, 26 May 2017 20:09:32 -0700 Subject: [PATCH 026/109] umip: rename norma_pf artifacts to a more generic name Now that normal_pf.c supports testing for other exceptiosn in addition to page faults, rename files accordingly. Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/Makefile | 4 ++-- .../umip/files/{normal_pf.c => umip_exceptions.c} | 0 meta-luv/recipes-core/umip/umip-tests_0.1.bb | 8 ++++---- 3 files changed, 6 insertions(+), 6 deletions(-) rename meta-luv/recipes-core/umip/files/{normal_pf.c => umip_exceptions.c} (100%) diff --git a/meta-luv/recipes-core/umip/files/Makefile b/meta-luv/recipes-core/umip/files/Makefile index daea0702445..fba4b3a4204 100644 --- a/meta-luv/recipes-core/umip/files/Makefile +++ b/meta-luv/recipes-core/umip/files/Makefile @@ -1,7 +1,7 @@ x86_64: $(CC) -o umip_test2_64 umip_test2.c $(CC) -o umip_test_64 umip_test.c - $(CC) -o umip_pf_64 normal_pf.c + $(CC) -o umip_exceptions_64 umip_exceptions.c python umip_test_gen_64.py $(CC) -c test_umip_ldt_64.c @@ -14,7 +14,7 @@ i686: # fo 32-bit builds, use -m32 if building independently $(CC) -o umip_test2_32 umip_test2.c $(CC) -o umip_test_32 umip_test.c - $(CC) -o umip_pf_32 normal_pf.c + $(CC) -o umip_exceptions_32 umip_exceptions.c python umip_test_gen_32.py $(CC) -c test_umip_ldt_32.c diff --git a/meta-luv/recipes-core/umip/files/normal_pf.c b/meta-luv/recipes-core/umip/files/umip_exceptions.c similarity index 100% rename from meta-luv/recipes-core/umip/files/normal_pf.c rename to meta-luv/recipes-core/umip/files/umip_exceptions.c diff --git a/meta-luv/recipes-core/umip/umip-tests_0.1.bb b/meta-luv/recipes-core/umip/umip-tests_0.1.bb index 0b41db5b23a..eaa0fe1d937 100644 --- a/meta-luv/recipes-core/umip/umip-tests_0.1.bb +++ b/meta-luv/recipes-core/umip/umip-tests_0.1.bb @@ -8,7 +8,7 @@ LIC_FILES_CHKSUM = "file://COPYING;md5=751419260aa954499f7abaabaa882bbe" SRC_URI="file://umip_test_defs.h \ file://umip_test.c \ file://umip_test2.c \ - file://normal_pf.c \ + file://umip_exceptions.c \ file://COPYING \ file://Makefile \ file://umip_ldt_32.c \ @@ -36,20 +36,20 @@ do_install() { install -m 744 ${WORKDIR}/UMIP_README ${D}${bindir} if [ "${TARGET_ARCH}" = "x86_64" ]; then install -m 755 ${WORKDIR}/umip_test_64 ${D}${bindir} - install -m 755 ${WORKDIR}/umip_pf_64 ${D}${bindir} + install -m 755 ${WORKDIR}/umip_exceptions_64 ${D}${bindir} install -m 755 ${WORKDIR}/umip_test2_64 ${D}${bindir} install -m 755 ${WORKDIR}/umip_ldt_64 ${D}${bindir} fi if [ "${TARGET_ARCH}" = "i586" ]; then install -m 755 ${WORKDIR}/umip_test_32 ${D}${bindir} - install -m 755 ${WORKDIR}/umip_pf_32 ${D}${bindir} + install -m 755 ${WORKDIR}/umip_exceptions_32 ${D}${bindir} install -m 755 ${WORKDIR}/umip_test2_32 ${D}${bindir} install -m 755 ${WORKDIR}/umip_ldt_32 ${D}${bindir} install -m 755 ${WORKDIR}/umip_ldt_16 ${D}${bindir} fi if [ "${TARGET_ARCH}" = "i686" ]; then install -m 755 ${WORKDIR}/umip_test_32 ${D}${bindir} - install -m 755 ${WORKDIR}/umip_pf_32 ${D}${bindir} + install -m 755 ${WORKDIR}/umip_exceptions_32 ${D}${bindir} install -m 755 ${WORKDIR}/umip_test2_32 ${D}${bindir} install -m 755 ${WORKDIR}/umip_ldt_32 ${D}${bindir} install -m 755 ${WORKDIR}/umip_ldt_16 ${D}${bindir} From 4154ab39ed858bab414c44452bc71510811b6a03 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Tue, 1 Aug 2017 17:26:09 -0700 Subject: [PATCH 027/109] luv-efi: do not build BITS We have removed BITS from the ramdisk. Thus, we don't need to build it. Yet, we get an error when constructing the disk image. TODO: We should use an environment variable to decide whether we want to include (and build BITS). Signed-off-by: Ricardo Neri --- meta-luv/classes/luv-efi.bbclass | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/meta-luv/classes/luv-efi.bbclass b/meta-luv/classes/luv-efi.bbclass index 21d25a92a46..2fa84bc49c2 100644 --- a/meta-luv/classes/luv-efi.bbclass +++ b/meta-luv/classes/luv-efi.bbclass @@ -85,9 +85,10 @@ efi_populate() { install -m 0644 ${DEPLOY_DIR_IMAGE}/${EFI_LOADER_IMAGE} ${DEST}${EFIDIR} fi - if echo "${TARGET_ARCH}" | grep -q "i.86" || [ "${TARGET_ARCH}" = "x86_64" ]; then - efi_populate_bits ${DEST} - fi + # TODO: only populate bits if present in features + #if echo "${TARGET_ARCH}" | grep -q "i.86" || [ "${TARGET_ARCH}" = "x86_64" ]; then + # efi_populate_bits ${DEST} + #fi # Install splash and grub.cfg files into EFI directory. install -m 0644 ${GRUBCFG} ${DEST}${EFIDIR} From edf238a1edb34b8b8fdb8d0905d2b04fc3df3ff6 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Tue, 1 Aug 2017 17:29:54 -0700 Subject: [PATCH 028/109] umip: update definitions of test result to include count It is good to have a count of the test that passed/failed or completed with error. Thus, update our definitions to also increment a counter when the status of a test result is known. Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/umip_test_defs.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/umip_test_defs.h b/meta-luv/recipes-core/umip/files/umip_test_defs.h index adce05b1824..846cc4e0645 100644 --- a/meta-luv/recipes-core/umip/files/umip_test_defs.h +++ b/meta-luv/recipes-core/umip/files/umip_test_defs.h @@ -7,10 +7,10 @@ #define TEST_INFO "\x1b[34m[info]\x1b[0m " #define TEST_ERROR "\x1b[33m[ERROR]\x1b[0m " -#define pr_pass(...) printf(TEST_PASS __VA_ARGS__) -#define pr_fail(...) printf(TEST_FAIL __VA_ARGS__) +#define pr_pass(pass_ctr, ...) do{ printf(TEST_PASS __VA_ARGS__); pass_ctr++; } while(0) +#define pr_fail(fail_ctr, ...) do{ printf(TEST_FAIL __VA_ARGS__); fail_ctr++; } while(0) #define pr_info(...) printf(TEST_INFO __VA_ARGS__) -#define pr_error(...) printf(TEST_ERROR __VA_ARGS__) +#define pr_error(error_ctr, ...) do{ printf(TEST_ERROR __VA_ARGS__); error_ctr++; } while(0) #ifdef __x86_64__ #define PRINT_BITNESS pr_info("This binary uses 64-bit code\n") From a41e3adf821f39c912170788325a3da97e0d6ed2 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Tue, 1 Aug 2017 17:31:42 -0700 Subject: [PATCH 029/109] umip: umip_test: update test program to count test cases Count the number of test cases that failed, passed or had errors. Also, print the test results whenever we exit. Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/umip_test.c | 57 ++++++++++++-------- 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/umip_test.c b/meta-luv/recipes-core/umip/files/umip_test.c index cc2015beba1..da21f29507c 100644 --- a/meta-luv/recipes-core/umip/files/umip_test.c +++ b/meta-luv/recipes-core/umip/files/umip_test.c @@ -22,8 +22,15 @@ #define IDTR_LEN 6 #endif +static int test_passed, test_failed, test_error; static sig_atomic_t got_signal; +static void print_results(void) +{ + printf("RESULTS: passed[%d], failed[%d], error[%d]\n", + test_passed, test_failed, test_errors); +} + static void handler(int signum, siginfo_t *info, void *ctx_void) { pr_info("si_signo[%d]\n", info->si_signo); @@ -42,10 +49,11 @@ static void handler(int signum, siginfo_t *info, void *ctx_void) pr_info("Unknown si_code!\n"); #ifdef __x86_64__ - pr_pass("I got a SIGSEGV. UMIP not emulated in 64-bit\n"); + pr_pass(test_passed, "I got a SIGSEGV. UMIP not emulated in 64-bit\n"); #else - pr_fail("FAIL: Whoa! I got a SIGSEGV. This is an error!\n"); + pr_fail(test_failed, "FAIL: Whoa! I got a SIGSEGV. This is an error!\n"); #endif + print_results(); exit(1); } @@ -65,9 +73,9 @@ static void call_sgdt() pr_info("GDT Limit [0x%04x]\n", limit); if (limit == expected_gdt.limit) - pr_pass("Expected limit value\n"); + pr_pass(test_passed, "Expected limit value\n"); else - pr_fail("Unexpected limit value\n"); + pr_fail(test_failed, "Unexpected limit value\n"); #if 0 for (i = 0; i < (GDTR_LEN -2); i++) @@ -78,9 +86,9 @@ static void call_sgdt() pr_info("GDT Base [0x%016lx]\n", base); if (base == expected_gdt.base) - pr_pass("Expected base value\n"); + pr_pass(test_passed, "Expected base value\n"); else - pr_fail("Unexpected base value\n"); + pr_fail(test_failed, "Unexpected base value\n"); } static void call_sidt() @@ -99,9 +107,9 @@ static void call_sidt() pr_info("IDT Limit [0x%04x]\n", limit); if (limit == expected_idt.limit) - pr_pass("Expected limit value\n"); + pr_pass(test_passed, "Expected limit value\n"); else - pr_fail("Unexpected limit value\n"); + pr_fail(test_failed, "Unexpected limit value\n"); #if 0 for (i = 0; i < (IDTR_LEN -2); i++) @@ -112,9 +120,9 @@ static void call_sidt() pr_info("IDT Base [0x%016lx]\n", base); if (base == expected_idt.base) - pr_pass("Expected base value\n"); + pr_pass(test_passed, "Expected base value\n"); else - pr_fail("Unexpected base value\n"); + pr_fail(test_failed, "Unexpected base value\n"); } static void call_sldt() @@ -136,9 +144,9 @@ static void call_sldt() */ \ if ((val & mask) == expected_ldt && (val & ~mask) == (init_val & ~mask)) - pr_pass("Obtained expected value\n"); + pr_pass(test_passed, "Obtained expected value\n"); else - pr_fail("Obtained unexpected value\n"); + pr_fail(test_failed, "Obtained unexpected value\n"); } static void call_smsw() @@ -159,9 +167,9 @@ static void call_smsw() */ \ if ((val & mask) == expected_msw && (val & ~mask) == (init_val & ~mask)) - pr_pass("Obtained expected value\n"); + pr_pass(test_passed, "Obtained expected value\n"); else - pr_fail("Obtained unexpected value\n"); + pr_fail(test_failed, "Obtained unexpected value\n"); } static void call_str() @@ -179,9 +187,9 @@ static void call_str() /* All 64 bits are written */ if (val64 == expected_tr) - pr_pass("Obtained 64-bit expected value\n"); + pr_pass(test_passed, "Obtained 64-bit expected value\n"); else - pr_fail("Obtained 64-bit unexpected value\n"); + pr_fail(test_failed, "Obtained 64-bit unexpected value\n"); #endif pr_info("Will issue STR and save at m32[0x%p]\n", &val32); @@ -196,9 +204,9 @@ static void call_str() */ if ((val32 & 0xffff) == expected_tr && (val32 & ~0xffff) == (init_val32 & ~0xffff)) - pr_pass("Obtained 32-bit expected value\n"); + pr_pass(test_passed, "Obtained 32-bit expected value\n"); else - pr_fail("Obtained 32-bit unexpected value\n"); + pr_fail(test_failed, "Obtained 32-bit unexpected value\n"); pr_info("Will issue STR and save at m16[0x%p]\n", &val16); asm volatile("str %0" : "=m" (val16)); @@ -212,9 +220,9 @@ static void call_str() */ if ((val16 & 0xffff) == expected_tr && (val16 & ~0xffff) == (init_val16 & ~0xffff)) - pr_pass("Obtained 16-bit expected value\n"); + pr_pass(test_passed, "Obtained 16-bit expected value\n"); else - pr_fail("Obtained 16-bit unexpected value\n"); + pr_fail(test_failed, "Obtained 16-bit unexpected value\n"); } @@ -230,7 +238,8 @@ int main(void) sigemptyset(&action.sa_mask); if (sigaction(SIGSEGV, &action, NULL) < 0) { - pr_error("Could not set the signal handler!"); + pr_error(test_error, "Could not set the signal handler!"); + print_results(); exit(1); } @@ -245,9 +254,11 @@ int main(void) sigemptyset(&action.sa_mask); if (sigaction(SIGSEGV, &action, NULL) < 0) { - pr_error("Could not remove signal handler!"); - return 1; + pr_error(test_error, "Could not remove signal handler!"); + print_results(); + exit(1); } + print_results(); return 0; } From adf5e883ed7889474f2cbf8a7cda2f1803606663 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Tue, 1 Aug 2017 17:35:40 -0700 Subject: [PATCH 030/109] umip: umip_test2: use a long type for test mask with register operands We use a mask to determine what parts of the returned results changed. When operands are registers, instructions can return up to 64-bit values. Thus, it is not sufficient to have a 16-bit mask. Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/umip_test2.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/umip_test2.c b/meta-luv/recipes-core/umip/files/umip_test2.c index af020b783f5..4f6b701761b 100644 --- a/meta-luv/recipes-core/umip/files/umip_test2.c +++ b/meta-luv/recipes-core/umip/files/umip_test2.c @@ -223,7 +223,7 @@ static unsigned long get_mask(int op_size) { static int test_str(void) { unsigned long val; - unsigned short mask = 0xffff; + unsigned long mask = 0xffff; pr_info("====Checking STR. Expected value: [0x%x]====\n", expected_tr); pr_info("==Tests for register operands==\n"); @@ -240,7 +240,7 @@ static int test_str(void) static int test_smsw(void) { unsigned long val; - unsigned short mask = 0xffff; + unsigned long mask = 0xffff; pr_info("====Checking SMSW. Expected value: [0x%x]====\n", expected_msw); pr_info("==Tests for register operands==\n"); @@ -256,7 +256,7 @@ static int test_smsw(void) static int test_sldt(void) { unsigned long val; - unsigned short mask = 0xffff; + unsigned long mask = 0xffff; pr_info("====Checking SLDT. Expected value: [0x%x]====\n", expected_ldt); pr_info("==Tests for register operands==\n"); From 45a11262e6f50aa8a5ea680c815ac0bc5f068aa1 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Tue, 1 Aug 2017 17:37:29 -0700 Subject: [PATCH 031/109] umip: umip_test2: Print the complete expected result of tests We use a 64-bit type to store result of tests. However, depending on the used operands, the result can be 16, 32 or 64-bit. Hence, in the expected value, print both the part of the variable that is expected to change and the part that is expected to remain unchanged. This can help to better understand failures. Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/umip_test2.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/umip_test2.c b/meta-luv/recipes-core/umip/files/umip_test2.c index 4f6b701761b..2391d75af99 100644 --- a/meta-luv/recipes-core/umip/files/umip_test2.c +++ b/meta-luv/recipes-core/umip/files/umip_test2.c @@ -56,11 +56,11 @@ * does not change. \ */ \ if (((val & mask) == (exp & mask)) && ((exp & ~mask) == (exp & ~mask))) \ - pr_pass(" On %s-bit '%s %s'! Got [0x%lx] Exp[0x%lx]\n", \ - #op_size, insn, reg, val, exp); \ + pr_pass(test_passed, " On %s-bit '%s %s'! Got [0x%016lx] Exp[0x%016lx]\n", \ + #op_size, insn, reg, val, (val&mask) | (init&~mask)); \ else { \ - pr_fail("On %s-bit '%s %s'! Got[0x%lx] Exp[0x%lx]\n", \ - #op_size, insn, reg, val, exp); \ + pr_fail(test_failed, "On %s-bit '%s %s'! Got[0x%016lx] Exp[0x%016lx]\n", \ + #op_size, insn, reg, val, (val&mask) | (init&~mask)); \ return -1; \ } \ } while(0); @@ -156,11 +156,11 @@ * does not change. \ */ \ if (((val & mask) == (exp & mask)) && ((exp & ~mask) == (exp & ~mask))) \ - pr_pass("On '%s %s(%s)'! Got [0x%lx] Exp[0x%lx]\n", \ - insn, #INSNmacro, reg, val, exp); \ + pr_pass(test_passed, "On '%s %s(%s)'! Got [0x%016lx] Exp[0x%016lx]\n", \ + insn, #INSNmacro, reg, val, (exp & mask) | (init & ~mask)); \ else { \ - pr_fail("On '%s %s(%s)'! Got[0x%lx] Exp[0x%lx]\n", \ - insn, #INSNmacro, reg, val, exp); \ + pr_fail(test_failed, "On '%s %s(%s)'! Got[0x%016lx] Exp[0x%016lx]\n", \ + insn, #INSNmacro, reg, val, (exp & mask) | (init & ~mask)); \ return -1; \ } From 227581a8bddb0938505842039fb9a16316aed600 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Tue, 1 Aug 2017 17:50:26 -0700 Subject: [PATCH 032/109] umip: umip_test2: do not abort execution on failed test cases In order to have a better idea which test cases pass and which do not, do not abort the test execution on failure. Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/umip_test2.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/umip_test2.c b/meta-luv/recipes-core/umip/files/umip_test2.c index 2391d75af99..6589d2bc31d 100644 --- a/meta-luv/recipes-core/umip/files/umip_test2.c +++ b/meta-luv/recipes-core/umip/files/umip_test2.c @@ -61,7 +61,6 @@ else { \ pr_fail(test_failed, "On %s-bit '%s %s'! Got[0x%016lx] Exp[0x%016lx]\n", \ #op_size, insn, reg, val, (val&mask) | (init&~mask)); \ - return -1; \ } \ } while(0); @@ -161,7 +160,6 @@ else { \ pr_fail(test_failed, "On '%s %s(%s)'! Got[0x%016lx] Exp[0x%016lx]\n", \ insn, #INSNmacro, reg, val, (exp & mask) | (init & ~mask)); \ - return -1; \ } #define CHECK_INSNmem(insn, reg, val, init, exp) \ From da0d119fc89a5019e3ea62845db189bf52536b62 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Tue, 1 Aug 2017 17:59:10 -0700 Subject: [PATCH 033/109] umip: umip_test2: count and print number of test cases Count and print the number of test cases. Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/umip_test2.c | 25 +++++++++++++------ 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/umip_test2.c b/meta-luv/recipes-core/umip/files/umip_test2.c index 6589d2bc31d..073dbee7500 100644 --- a/meta-luv/recipes-core/umip/files/umip_test2.c +++ b/meta-luv/recipes-core/umip/files/umip_test2.c @@ -197,6 +197,13 @@ #define INIT_LDTS INIT_VAL(15151515) static sig_atomic_t got_signal; +static int test_passed, test_failed, test_errors; + +static void print_results(void) +{ + printf("RESULTS: passed[%d], failed[%d], error[%d]\n", + test_passed, test_failed, test_errors); +} static unsigned long get_mask(int op_size) { switch (op_size) { @@ -209,7 +216,7 @@ static unsigned long get_mask(int op_size) { return 0xffffffffffffffff; #endif default: - pr_error("Invalid operand size!\n"); + pr_error(test_errors, "Invalid operand size!\n"); /* * We can't return -1 as it would be equal to the * 32 or 64-bit mask @@ -274,7 +281,7 @@ static void handler(int signum, siginfo_t *info, void *ctx_void) pr_info("si_code[%d]\n", info->si_code); pr_info("si_code[0x%p]\n", info->si_addr); if (signum != SIGSEGV) - pr_error("Received unexpected signal"); + pr_error(test_errors, "Received unexpected signal"); else got_signal = signum; if (info->si_code == SEGV_MAPERR) @@ -285,9 +292,9 @@ static void handler(int signum, siginfo_t *info, void *ctx_void) pr_info("Unknown si_code!\n"); #ifdef __x86_64__ - pr_pass("I got a SIGSEGV. UMIP not emulated in 64-bit\n"); + pr_pass(test_passed, "I got a SIGSEGV. UMIP not emulated in 64-bit\n"); #else - pr_fail("FAIL: Whoa! I got a SIGSEGV. This is an error!\n"); + pr_fail(test_failed, "FAIL: Whoa! I got a SIGSEGV. This is an error!\n"); #endif exit(1); } @@ -305,7 +312,7 @@ int main (void) sigemptyset(&action.sa_mask); if (sigaction(SIGSEGV, &action, NULL) < 0) { - pr_error("Could not set the signal handler!"); + pr_error(test_errors, "Could not set the signal handler!"); exit(1); } @@ -313,20 +320,22 @@ int main (void) ret_str = test_str(); ret_smsw = test_smsw(); ret_sldt = test_sldt(); + if (ret_str || ret_smsw || ret_sldt) - pr_fail("***Test completed with errors str[%d] smsw[%d] sldt[%d]\n", + pr_info("***Test completed with errors str[%d] smsw[%d] sldt[%d]\n", ret_str, ret_smsw, ret_sldt); else - pr_pass("***All tests completed successfully.***\n"); + pr_info("***All tests completed successfully.***\n"); memset(&action, 0, sizeof(action)); action.sa_handler = SIG_DFL; sigemptyset(&action.sa_mask); if (sigaction(SIGSEGV, &action, NULL) < 0) { - pr_error("Could not remove signal handler!"); + pr_error(test_errors, "Could not remove signal handler!"); return 1; } + print_results(); return 0; } From b8507c120fce04b85905158d36d977ed41482518 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Tue, 1 Aug 2017 18:15:17 -0700 Subject: [PATCH 034/109] umip: correc typo when printing siginfo address Programs were incorrectly printing "si_code" while the value printed was siginfo's address. Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/umip_exceptions.c | 2 +- meta-luv/recipes-core/umip/files/umip_ldt_16.c | 2 +- meta-luv/recipes-core/umip/files/umip_ldt_32.c | 2 +- meta-luv/recipes-core/umip/files/umip_ldt_64.c | 2 +- meta-luv/recipes-core/umip/files/umip_test.c | 2 +- meta-luv/recipes-core/umip/files/umip_test2.c | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/umip_exceptions.c b/meta-luv/recipes-core/umip/files/umip_exceptions.c index 015a789620d..6bd83452dac 100644 --- a/meta-luv/recipes-core/umip/files/umip_exceptions.c +++ b/meta-luv/recipes-core/umip/files/umip_exceptions.c @@ -27,7 +27,7 @@ void handler(int signum, siginfo_t *info, void *ctx_void) pr_info("si_signo[%d]\n", info->si_signo); pr_info("si_errno[%d]\n", info->si_errno); pr_info("si_code[%d]\n", info->si_code); - pr_info("si_code[0x%p]\n", info->si_addr); + pr_info("si_addr[0x%p]\n", info->si_addr); got_signal = signum; diff --git a/meta-luv/recipes-core/umip/files/umip_ldt_16.c b/meta-luv/recipes-core/umip/files/umip_ldt_16.c index 97df9ca74f7..e08c83d91e8 100644 --- a/meta-luv/recipes-core/umip/files/umip_ldt_16.c +++ b/meta-luv/recipes-core/umip/files/umip_ldt_16.c @@ -39,7 +39,7 @@ void handler(int signum, siginfo_t *info, void *ctx_void) pr_info("si_signo[%d]\n", info->si_signo); pr_info("si_errno[%d]\n", info->si_errno); pr_info("si_code[%d]\n", info->si_code); - pr_info("si_code[0x%p]\n", info->si_addr); + pr_info("si_addr[0x%p]\n", info->si_addr); if (signum != SIGSEGV) pr_error("Received unexpected signal"); else diff --git a/meta-luv/recipes-core/umip/files/umip_ldt_32.c b/meta-luv/recipes-core/umip/files/umip_ldt_32.c index ac0d731a3bc..bb9adde53c0 100644 --- a/meta-luv/recipes-core/umip/files/umip_ldt_32.c +++ b/meta-luv/recipes-core/umip/files/umip_ldt_32.c @@ -38,7 +38,7 @@ void handler(int signum, siginfo_t *info, void *ctx_void) pr_info("si_signo[%d]\n", info->si_signo); pr_info("si_errno[%d]\n", info->si_errno); pr_info("si_code[%d]\n", info->si_code); - pr_info("si_code[0x%p]\n", info->si_addr); + pr_info("si_addr[0x%p]\n", info->si_addr); if (signum != SIGSEGV) pr_error("Received unexpected signal"); else diff --git a/meta-luv/recipes-core/umip/files/umip_ldt_64.c b/meta-luv/recipes-core/umip/files/umip_ldt_64.c index d8d21e02b76..44bb306b360 100644 --- a/meta-luv/recipes-core/umip/files/umip_ldt_64.c +++ b/meta-luv/recipes-core/umip/files/umip_ldt_64.c @@ -36,7 +36,7 @@ void handler(int signum, siginfo_t *info, void *ctx_void) pr_info("si_signo[%d]\n", info->si_signo); pr_info("si_errno[%d]\n", info->si_errno); pr_info("si_code[%d]\n", info->si_code); - pr_info("si_code[0x%p]\n", info->si_addr); + pr_info("si_addr[0x%p]\n", info->si_addr); if (signum != SIGSEGV) pr_error("Received unexpected signal"); else diff --git a/meta-luv/recipes-core/umip/files/umip_test.c b/meta-luv/recipes-core/umip/files/umip_test.c index da21f29507c..a5103c2d960 100644 --- a/meta-luv/recipes-core/umip/files/umip_test.c +++ b/meta-luv/recipes-core/umip/files/umip_test.c @@ -36,7 +36,7 @@ static void handler(int signum, siginfo_t *info, void *ctx_void) pr_info("si_signo[%d]\n", info->si_signo); pr_info("si_errno[%d]\n", info->si_errno); pr_info("si_code[%d]\n", info->si_code); - pr_info("si_code[0x%p]\n", info->si_addr); + pr_info("si_addr[0x%p]\n", info->si_addr); if (signum != SIGSEGV) errx(1, "ERROR: Received unexpected signal"); else diff --git a/meta-luv/recipes-core/umip/files/umip_test2.c b/meta-luv/recipes-core/umip/files/umip_test2.c index 073dbee7500..a8c3f927511 100644 --- a/meta-luv/recipes-core/umip/files/umip_test2.c +++ b/meta-luv/recipes-core/umip/files/umip_test2.c @@ -279,7 +279,7 @@ static void handler(int signum, siginfo_t *info, void *ctx_void) pr_info("si_signo[%d]\n", info->si_signo); pr_info("si_errno[%d]\n", info->si_errno); pr_info("si_code[%d]\n", info->si_code); - pr_info("si_code[0x%p]\n", info->si_addr); + pr_info("si_addr[0x%p]\n", info->si_addr); if (signum != SIGSEGV) pr_error(test_errors, "Received unexpected signal"); else From 7b34d0421bc2d7a8e061a7f18b53e2de2ae57d18 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Wed, 2 Aug 2017 09:54:16 -0700 Subject: [PATCH 035/109] umip: umip_exceptions: remove unused code Remove rememnants of old prototyping code. Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/umip_exceptions.c | 6 ------ 1 file changed, 6 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/umip_exceptions.c b/meta-luv/recipes-core/umip/files/umip_exceptions.c index 6bd83452dac..aa75b678129 100644 --- a/meta-luv/recipes-core/umip/files/umip_exceptions.c +++ b/meta-luv/recipes-core/umip/files/umip_exceptions.c @@ -63,12 +63,6 @@ void handler(int signum, siginfo_t *info, void *ctx_void) #endif } -struct my_struct { - int a; - int b; - int c; -}; - int test_normal_pf(void) { unsigned long *val_bad = (unsigned long *)0x100000; From b8fc35b3ddbf30960ef358139b7f784404634316 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Wed, 2 Aug 2017 09:56:43 -0700 Subject: [PATCH 036/109] umip: umip_exceptions: count number of failed and passed test cases Add functionality to count the number of test cases that failed or passed as well as the number of test errors. Signed-off-by: Ricardo Neri --- .../recipes-core/umip/files/umip_exceptions.c | 47 ++++++++++++------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/umip_exceptions.c b/meta-luv/recipes-core/umip/files/umip_exceptions.c index aa75b678129..b7857916eb1 100644 --- a/meta-luv/recipes-core/umip/files/umip_exceptions.c +++ b/meta-luv/recipes-core/umip/files/umip_exceptions.c @@ -20,6 +20,14 @@ static sig_atomic_t signal_code; static sig_atomic_t got_signal; +static int test_passed, test_failed, test_errors; + +static void print_results(void) +{ + printf("RESULTS: passed[%d], failed[%d], error[%d]\n", + test_passed, test_failed, test_errors); +} + void handler(int signum, siginfo_t *info, void *ctx_void) { ucontext_t *ctx = (ucontext_t *)ctx_void; @@ -79,22 +87,22 @@ int test_normal_pf(void) "nop\n": "=m"(*val_bad)); if (!got_signal) { - pr_fail("Signal not received!\n"); + pr_fail(test_failed, "Signal not received!\n"); return 1; } #ifdef __x86_64__ if (signal_code != SI_KERNEL) { - pr_fail("Signal code is not what we expect.\n"); + pr_fail(test_failed, "Signal code is not what we expect.\n"); return 1; } - pr_pass("A SEGV_MAPERR page fault was issued.\n"); + pr_pass(test_passed, "A SEGV_MAPERR page fault was issued.\n"); return 0; #else if (signal_code != SEGV_MAPERR) { - pr_fail("Signal code is not what we expect.\n"); + pr_fail(test_failed, "Signal code is not what we expect.\n"); return 1; } - pr_pass("A SEGV_MAPERR page fault was issued.\n"); + pr_pass(test_passed, "A SEGV_MAPERR page fault was issued.\n"); return 0; #endif } @@ -111,11 +119,11 @@ static int __test_lock_prefix_##name(void) \ "nop\n"); \ \ if (signal_code != ILL_ILLOPN) { \ - pr_fail("Signal code is not what we expect.\n"); \ + pr_fail(test_failed, "Signal code is not what we expect.\n");\ signal_code = 0; \ return 1; \ } \ - pr_pass("An ILL_ILLOPN exception was issued.\n"); \ + pr_pass(test_passed, "An ILL_ILLOPN exception was issued.\n"); \ signal_code = 0; \ \ return 0; \ @@ -164,11 +172,11 @@ static int __test_register_operand_##name(void) \ "nop\n"); \ \ if (signal_code != ILL_ILLOPN) { \ - pr_fail("Signal code is not what we expect.\n"); \ + pr_fail(test_failed, "Signal code is not what we expect.\n");\ signal_code = 0; \ return 1; \ } \ - pr_pass("An ILL_ILLOPN exception was issued.\n"); \ + pr_pass(test_passed, "An ILL_ILLOPN exception was issued.\n"); \ signal_code = 0; \ \ return 0; \ @@ -214,11 +222,11 @@ static int __test_null_segment_selector_##inst##_##reg(void) \ "pop %eax\n" \ "pop %" #reg "\n"); \ if (signal_code != SI_KERNEL) { \ - pr_fail("Signal code is not what we expect.\n"); \ + pr_fail(test_failed, "Signal code is not what we expect.\n"); \ signal_code = 0; \ return 1; \ } \ - pr_pass("An ILL_ILLOPN exception was issued.\n"); \ + pr_pass(test_passed, "An ILL_ILLOPN exception was issued.\n"); \ signal_code = 0; \ \ return 0; \ @@ -364,7 +372,7 @@ static int setup_data_segments() ret = syscall(SYS_modify_ldt, 1, &desc, sizeof(desc)); if (ret) { - pr_error("Failed to install stack semgnet [%d].\n", ret); + pr_error(test_errors, "Failed to install stack semgnet [%d].\n", ret); return ret; } @@ -398,11 +406,11 @@ int __test_addresses_outside_segment_##inst##_##sel(void) \ :"m" (seg_sel)); \ \ if (signal_code != SI_KERNEL) { \ - pr_fail("Signal code is not what we expect.\n"); \ + pr_fail(test_failed, "Signal code is not what we expect.\n"); \ signal_code = 0; \ return 1; \ } \ - pr_pass("An ILL_ILLOPN exception was issued.\n"); \ + pr_pass(test_passed, "An ILL_ILLOPN exception was issued.\n"); \ signal_code = 0; \ \ return 0; \ @@ -534,12 +542,12 @@ int main (void) sigemptyset(&action.sa_mask); if (sigaction(SIGSEGV, &action, NULL) < 0) { - pr_error("Could not set the signal handler for SIGSEGV!\n"); + pr_error(test_errors, "Could not set the signal handler for SIGSEGV!\n"); exit(1); } if (sigaction(SIGILL, &action, NULL) < 0) { - pr_error("Could not set the signal handler SIGILL!\n"); + pr_error(test_errors, "Could not set the signal handler SIGILL!\n"); exit(1); } @@ -566,14 +574,17 @@ int main (void) sigemptyset(&action.sa_mask); if (sigaction(SIGSEGV, &action, NULL) < 0) { - pr_error("Could not remove signal SIGSEGV handler!\n"); + pr_error(test_errors, "Could not remove signal SIGSEGV handler!\n"); + print_results(); return 1; } if (sigaction(SIGILL, &action, NULL) < 0) { - pr_error("Could not remove signal SIGILL handler!\n"); + pr_error(test_errors, "Could not remove signal SIGILL handler!\n"); + print_results(); return 1; } + print_results(); return ret; } From acaf4eddec7c78eb4bf4f6d2b6cc5fe58500d32e Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Wed, 2 Aug 2017 10:46:47 -0700 Subject: [PATCH 037/109] umip: umip_exceptions: do not abort tests on failure Allow all tests to run after failed test cases. This is safe as all test cases are independent of each other. Thus, we don't need to worry about subsequent tests running from an initial bad state. Signed-off-by: Ricardo Neri --- .../recipes-core/umip/files/umip_exceptions.c | 237 ++++-------------- 1 file changed, 54 insertions(+), 183 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/umip_exceptions.c b/meta-luv/recipes-core/umip/files/umip_exceptions.c index b7857916eb1..17066728572 100644 --- a/meta-luv/recipes-core/umip/files/umip_exceptions.c +++ b/meta-luv/recipes-core/umip/files/umip_exceptions.c @@ -138,23 +138,12 @@ gen_test_lock_prefix_inst(SLDT, ".byte 0xf0, 0xf, 0x0, 0x0\n") static int test_lock_prefix(void) { int ret = 0; - ret = __test_lock_prefix_SMSW(); - if (ret) - return ret; - - ret = __test_lock_prefix_SIDT(); - if (ret) - return ret; - ret = __test_lock_prefix_SGDT(); - if (ret) - return ret; - - ret = __test_lock_prefix_STR(); - if (ret) - return ret; - - ret = __test_lock_prefix_SLDT(); + __test_lock_prefix_SMSW(); + __test_lock_prefix_SIDT(); + __test_lock_prefix_SGDT(); + __test_lock_prefix_STR(); + __test_lock_prefix_SLDT(); return ret; } @@ -187,14 +176,10 @@ gen_test_register_operand_inst(SGDT, ".byte 0xf, 0x1, 0xc0\n") int test_register_operand(void) { - int ret; - signal_code = 0; - ret = __test_register_operand_SGDT(); - if (ret) - return 1; - ret = __test_register_operand_SIDT(); + __test_register_operand_SGDT(); + __test_register_operand_SIDT(); } #ifdef __x86_64__ @@ -255,81 +240,39 @@ gen_test_null_segment_selector(sldt, gs) int test_null_segment_selectors(void) { - int ret; - - ret = __test_null_segment_selector_smsw_ds(); - if (ret) - return 1; - ret = __test_null_segment_selector_smsw_es(); - if (ret) - return 1; - ret = __test_null_segment_selector_smsw_fs(); - if (ret) - return 1; + __test_null_segment_selector_smsw_ds(); + __test_null_segment_selector_smsw_es(); + __test_null_segment_selector_smsw_fs(); #ifdef TEST_GS /* TODO: Meddling with gs breaks libc */ - ret = __test_null_segment_selector_smsw_gs(); - if (ret) - return 1; + __test_null_segment_selector_smsw_gs(); #endif - - ret = __test_null_segment_selector_sidt_ds(); - if (ret) - return 1; - ret = __test_null_segment_selector_sidt_es(); - if (ret) - return 1; - ret = __test_null_segment_selector_sidt_fs(); - if (ret) - return 1; + __test_null_segment_selector_sidt_ds(); + __test_null_segment_selector_sidt_es(); + __test_null_segment_selector_sidt_fs(); #ifdef TEST_GS /* TODO: Meddling with gs breaks libc */ - ret = __test_null_segment_selector_sidt_gs(); - if (ret) - return 1; + __test_null_segment_selector_sidt_gs(); #endif - ret = __test_null_segment_selector_sgdt_ds(); - if (ret) - return 1; - ret = __test_null_segment_selector_sgdt_es(); - if (ret) - return 1; - ret = __test_null_segment_selector_sgdt_fs(); - if (ret) - return 1; + __test_null_segment_selector_sgdt_ds(); + __test_null_segment_selector_sgdt_es(); + __test_null_segment_selector_sgdt_fs(); #ifdef TEST_GS /* TODO: Meddling with gs breaks libc */ - ret = __test_null_segment_selector_sgdt_gs(); - if (ret) - return 1; + __test_null_segment_selector_sgdt_gs(); #endif - ret = __test_null_segment_selector_sldt_ds(); - if (ret) - return 1; - ret = __test_null_segment_selector_sldt_es(); - if (ret) - return 1; - ret = __test_null_segment_selector_sldt_fs(); - if (ret) - return 1; -#ifdef TEST_GS /* TODO: Meddling with gs breaks libc */ - ret = __test_null_segment_selector_sldt_gs(); - if (ret) - return 1; + __test_null_segment_selector_sldt_ds(); + __test_null_segment_selector_sldt_es(); + __test_null_segment_selector_sldt_fs(); + #ifdef TEST_GS /* TODO: Meddling with gs breaks libc */ + __test_null_segment_selector_sldt_gs(); #endif - ret = __test_null_segment_selector_str_ds(); - if (ret) - return 1; - ret = __test_null_segment_selector_str_es(); - if (ret) - return 1; - ret = __test_null_segment_selector_str_fs(); - if (ret) - return 1; + __test_null_segment_selector_str_ds(); + __test_null_segment_selector_str_es(); + __test_null_segment_selector_str_fs(); + #ifdef TEST_GS /* TODO: Meddling with gs breaks libc */ - ret = __test_null_segment_selector_str_gs(); - if (ret) - return 1; + __test_null_segment_selector_str_gs(); #endif return 0; } @@ -445,86 +388,27 @@ int test_addresses_outside_segment(void) if (ret) return 1; - ret = __test_addresses_outside_segment_smsw_ds(); - if (ret) - return 1; - - ret = __test_addresses_outside_segment_str_ds(); - if (ret) - return 1; - - ret = __test_addresses_outside_segment_sgdt_ds(); - if (ret) - return 1; - - ret = __test_addresses_outside_segment_sidt_ds(); - if (ret) - return 1; - - ret = __test_addresses_outside_segment_sldt_ds(); - if (ret) - return 1; - - ret = __test_addresses_outside_segment_smsw_es(); - if (ret) - return 1; - - ret = __test_addresses_outside_segment_str_es(); - if (ret) - return 1; - - ret = __test_addresses_outside_segment_sgdt_es(); - if (ret) - return 1; - - ret = __test_addresses_outside_segment_sidt_es(); - if (ret) - return 1; - - ret = __test_addresses_outside_segment_sldt_es(); - if (ret) - return 1; - - ret = __test_addresses_outside_segment_smsw_fs(); - if (ret) - return 1; - - ret = __test_addresses_outside_segment_str_fs(); - if (ret) - return 1; - - ret = __test_addresses_outside_segment_sgdt_fs(); - if (ret) - return 1; - - ret = __test_addresses_outside_segment_sidt_fs(); - if (ret) - return 1; - - ret = __test_addresses_outside_segment_sldt_fs(); - if (ret) - return 1; - + __test_addresses_outside_segment_smsw_ds(); + __test_addresses_outside_segment_str_ds(); + __test_addresses_outside_segment_sgdt_ds(); + __test_addresses_outside_segment_sidt_ds(); + __test_addresses_outside_segment_sldt_ds(); + __test_addresses_outside_segment_smsw_es(); + __test_addresses_outside_segment_str_es(); + __test_addresses_outside_segment_sgdt_es(); + __test_addresses_outside_segment_sidt_es(); + __test_addresses_outside_segment_sldt_es(); + __test_addresses_outside_segment_smsw_fs(); + __test_addresses_outside_segment_str_fs(); + __test_addresses_outside_segment_sgdt_fs(); + __test_addresses_outside_segment_sidt_fs(); + __test_addresses_outside_segment_sldt_fs(); #ifdef TEST_GS - ret = __test_addresses_outside_segment_smsw_gs(); - if (ret) - return 1; - - ret = __test_addresses_outside_segment_str_gs(); - if (ret) - return 1; - - ret = __test_addresses_outside_segment_sgdt_gs(); - if (ret) - return 1; - - ret = __test_addresses_outside_segment_sidt_gs(); - if (ret) - return 1; - - ret = __test_addresses_outside_segment_sldt_gs(); - if (ret) - return 1; + __test_addresses_outside_segment_smsw_gs(); + __test_addresses_outside_segment_str_gs(); + __test_addresses_outside_segment_sgdt_gs(); + __test_addresses_outside_segment_sidt_gs(); + __test_addresses_outside_segment_sldt_gs(); #endif } #endif @@ -532,7 +416,6 @@ int test_addresses_outside_segment(void) int main (void) { struct sigaction action; - int ret; PRINT_BITNESS; @@ -551,22 +434,10 @@ int main (void) exit(1); } - ret = test_normal_pf(); - if (ret) - return 1; - - ret = test_lock_prefix(); - if (ret) - return 1; - - ret = test_register_operand(); - if (ret) - return 1; - - ret = test_null_segment_selectors(); - if (ret) - return 1; - + test_normal_pf(); + test_lock_prefix(); + test_register_operand(); + test_null_segment_selectors(); test_addresses_outside_segment(); memset(&action, 0, sizeof(action)); @@ -586,5 +457,5 @@ int main (void) } print_results(); - return ret; + return 0; } From 0776cea79ea938c74508a66c35c58211ba71c5ea Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Wed, 2 Aug 2017 10:51:00 -0700 Subject: [PATCH 038/109] umip: umip_exceptions: add static qualifier where needed All test functions can be static. Add the static qualifier to them. Signed-off-by: Ricardo Neri --- .../recipes-core/umip/files/umip_exceptions.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/umip_exceptions.c b/meta-luv/recipes-core/umip/files/umip_exceptions.c index 17066728572..bc297a1de54 100644 --- a/meta-luv/recipes-core/umip/files/umip_exceptions.c +++ b/meta-luv/recipes-core/umip/files/umip_exceptions.c @@ -28,7 +28,7 @@ static void print_results(void) test_passed, test_failed, test_errors); } -void handler(int signum, siginfo_t *info, void *ctx_void) +static void handler(int signum, siginfo_t *info, void *ctx_void) { ucontext_t *ctx = (ucontext_t *)ctx_void; @@ -71,7 +71,7 @@ void handler(int signum, siginfo_t *info, void *ctx_void) #endif } -int test_normal_pf(void) +static int test_normal_pf(void) { unsigned long *val_bad = (unsigned long *)0x100000; @@ -174,7 +174,7 @@ static int __test_register_operand_##name(void) \ gen_test_register_operand_inst(SIDT, ".byte 0xf, 0x1, 0xc8\n") gen_test_register_operand_inst(SGDT, ".byte 0xf, 0x1, 0xc0\n") -int test_register_operand(void) +static int test_register_operand(void) { signal_code = 0; @@ -183,7 +183,7 @@ int test_register_operand(void) } #ifdef __x86_64__ -int test_null_segment_selectors(void) +static int test_null_segment_selectors(void) { } #else @@ -238,7 +238,7 @@ gen_test_null_segment_selector(sldt, es) gen_test_null_segment_selector(sldt, fs) gen_test_null_segment_selector(sldt, gs) -int test_null_segment_selectors(void) +static int test_null_segment_selectors(void) { __test_null_segment_selector_smsw_ds(); __test_null_segment_selector_smsw_es(); @@ -279,7 +279,7 @@ int test_null_segment_selectors(void) #endif #ifdef __x86_64__ -int test_addresses_outside_segment(void) +static int test_addresses_outside_segment(void) { } #else @@ -323,7 +323,7 @@ static int setup_data_segments() } #define gen_test_addresses_outside_segment(inst, sel) \ -int __test_addresses_outside_segment_##inst##_##sel(void) \ +static int __test_addresses_outside_segment_##inst##_##sel(void) \ { \ int ret; \ unsigned short seg_sel; \ @@ -380,7 +380,7 @@ gen_test_addresses_outside_segment(sldt, gs) gen_test_addresses_outside_segment(sgdt, gs) gen_test_addresses_outside_segment(sidt, gs) -int test_addresses_outside_segment(void) +static int test_addresses_outside_segment(void) { int ret; From 6076f557b627d72c9d9507a1f6a268071f3a9133 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Wed, 2 Aug 2017 10:57:41 -0700 Subject: [PATCH 039/109] umip: umip_exception: remove unused return values from test functions Now that we let tests run after previous tests failed, remove the unused functions' return values. Note that we have variables to register and count the tests' statuses. Signed-off-by: Ricardo Neri --- .../recipes-core/umip/files/umip_exceptions.c | 53 +++++++++---------- 1 file changed, 24 insertions(+), 29 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/umip_exceptions.c b/meta-luv/recipes-core/umip/files/umip_exceptions.c index bc297a1de54..d4ce548a9b9 100644 --- a/meta-luv/recipes-core/umip/files/umip_exceptions.c +++ b/meta-luv/recipes-core/umip/files/umip_exceptions.c @@ -71,7 +71,7 @@ static void handler(int signum, siginfo_t *info, void *ctx_void) #endif } -static int test_normal_pf(void) +static void test_normal_pf(void) { unsigned long *val_bad = (unsigned long *)0x100000; @@ -88,27 +88,27 @@ static int test_normal_pf(void) if (!got_signal) { pr_fail(test_failed, "Signal not received!\n"); - return 1; + return; } #ifdef __x86_64__ if (signal_code != SI_KERNEL) { pr_fail(test_failed, "Signal code is not what we expect.\n"); - return 1; + return; } pr_pass(test_passed, "A SEGV_MAPERR page fault was issued.\n"); - return 0; + return; #else if (signal_code != SEGV_MAPERR) { pr_fail(test_failed, "Signal code is not what we expect.\n"); - return 1; + return; } pr_pass(test_passed, "A SEGV_MAPERR page fault was issued.\n"); - return 0; + return; #endif } #define gen_test_lock_prefix_inst(name, inst) \ -static int __test_lock_prefix_##name(void) \ +static void __test_lock_prefix_##name(void) \ { \ pr_info("Test %s with lock prefix\n", #name); \ /* name (%eax) with the LOCK prefix */ \ @@ -121,12 +121,12 @@ static int __test_lock_prefix_##name(void) \ if (signal_code != ILL_ILLOPN) { \ pr_fail(test_failed, "Signal code is not what we expect.\n");\ signal_code = 0; \ - return 1; \ + return; \ } \ pr_pass(test_passed, "An ILL_ILLOPN exception was issued.\n"); \ signal_code = 0; \ \ - return 0; \ + return; \ } gen_test_lock_prefix_inst(SMSW, ".byte 0xf0, 0xf, 0x1, 0x20\n") @@ -135,21 +135,17 @@ gen_test_lock_prefix_inst(SGDT, ".byte 0xf0, 0xf, 0x1, 0x0\n") gen_test_lock_prefix_inst(STR, ".byte 0xf0, 0xf, 0x0, 0x8\n") gen_test_lock_prefix_inst(SLDT, ".byte 0xf0, 0xf, 0x0, 0x0\n") -static int test_lock_prefix(void) +static void test_lock_prefix(void) { - int ret = 0; - __test_lock_prefix_SMSW(); __test_lock_prefix_SIDT(); __test_lock_prefix_SGDT(); __test_lock_prefix_STR(); __test_lock_prefix_SLDT(); - - return ret; } #define gen_test_register_operand_inst(name, inst) \ -static int __test_register_operand_##name(void) \ +static void __test_register_operand_##name(void) \ { \ pr_info("Test %s with register operand\n", #name); \ /* name (%eax) with the LOCK prefix */ \ @@ -163,18 +159,18 @@ static int __test_register_operand_##name(void) \ if (signal_code != ILL_ILLOPN) { \ pr_fail(test_failed, "Signal code is not what we expect.\n");\ signal_code = 0; \ - return 1; \ + return; \ } \ pr_pass(test_passed, "An ILL_ILLOPN exception was issued.\n"); \ signal_code = 0; \ \ - return 0; \ + return; \ } gen_test_register_operand_inst(SIDT, ".byte 0xf, 0x1, 0xc8\n") gen_test_register_operand_inst(SGDT, ".byte 0xf, 0x1, 0xc0\n") -static int test_register_operand(void) +static void test_register_operand(void) { signal_code = 0; @@ -183,12 +179,12 @@ static int test_register_operand(void) } #ifdef __x86_64__ -static int test_null_segment_selectors(void) +static void test_null_segment_selectors(void) { } #else #define gen_test_null_segment_selector(inst, reg) \ -static int __test_null_segment_selector_##inst##_##reg(void) \ +static void __test_null_segment_selector_##inst##_##reg(void) \ { \ pr_info("Test using null seg sel for " #inst " with " #reg "\n"); \ asm volatile("push %" #reg "\n" \ @@ -209,12 +205,12 @@ static int __test_null_segment_selector_##inst##_##reg(void) \ if (signal_code != SI_KERNEL) { \ pr_fail(test_failed, "Signal code is not what we expect.\n"); \ signal_code = 0; \ - return 1; \ + return; \ } \ pr_pass(test_passed, "An ILL_ILLOPN exception was issued.\n"); \ signal_code = 0; \ \ - return 0; \ + return; \ } gen_test_null_segment_selector(smsw, ds) @@ -238,7 +234,7 @@ gen_test_null_segment_selector(sldt, es) gen_test_null_segment_selector(sldt, fs) gen_test_null_segment_selector(sldt, gs) -static int test_null_segment_selectors(void) +static void test_null_segment_selectors(void) { __test_null_segment_selector_smsw_ds(); __test_null_segment_selector_smsw_es(); @@ -274,12 +270,11 @@ static int test_null_segment_selectors(void) #ifdef TEST_GS /* TODO: Meddling with gs breaks libc */ __test_null_segment_selector_str_gs(); #endif - return 0; } #endif #ifdef __x86_64__ -static int test_addresses_outside_segment(void) +static void test_addresses_outside_segment(void) { } #else @@ -323,7 +318,7 @@ static int setup_data_segments() } #define gen_test_addresses_outside_segment(inst, sel) \ -static int __test_addresses_outside_segment_##inst##_##sel(void) \ +static void __test_addresses_outside_segment_##inst##_##sel(void) \ { \ int ret; \ unsigned short seg_sel; \ @@ -351,12 +346,12 @@ static int __test_addresses_outside_segment_##inst##_##sel(void) \ if (signal_code != SI_KERNEL) { \ pr_fail(test_failed, "Signal code is not what we expect.\n"); \ signal_code = 0; \ - return 1; \ + return; \ } \ pr_pass(test_passed, "An ILL_ILLOPN exception was issued.\n"); \ signal_code = 0; \ \ - return 0; \ + return; \ } gen_test_addresses_outside_segment(smsw, ds) @@ -386,7 +381,7 @@ static int test_addresses_outside_segment(void) ret = setup_data_segments(); if (ret) - return 1; + return; __test_addresses_outside_segment_smsw_ds(); __test_addresses_outside_segment_str_ds(); From 40c44d7fdbd7e2f75df7e01bbeef4b65331714b1 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Wed, 2 Aug 2017 11:02:32 -0700 Subject: [PATCH 040/109] umip: umip_exceptions: save a couple of lines By putting the definitions of emtpy functions in a single line, save a couple of lines in the file. Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/umip_exceptions.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/umip_exceptions.c b/meta-luv/recipes-core/umip/files/umip_exceptions.c index d4ce548a9b9..7d454969c76 100644 --- a/meta-luv/recipes-core/umip/files/umip_exceptions.c +++ b/meta-luv/recipes-core/umip/files/umip_exceptions.c @@ -179,9 +179,7 @@ static void test_register_operand(void) } #ifdef __x86_64__ -static void test_null_segment_selectors(void) -{ -} +static void test_null_segment_selectors(void) {} #else #define gen_test_null_segment_selector(inst, reg) \ static void __test_null_segment_selector_##inst##_##reg(void) \ @@ -274,9 +272,7 @@ static void test_null_segment_selectors(void) #endif #ifdef __x86_64__ -static void test_addresses_outside_segment(void) -{ -} +static void test_addresses_outside_segment(void) {} #else #define SEGMENT_SIZE 0x1000 From 4ccbd2b1fd036c6b203f2d67be33ff20c2837a85 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Wed, 2 Aug 2017 11:03:41 -0700 Subject: [PATCH 041/109] umip: umip_exception: fix a typo Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/umip_exceptions.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/meta-luv/recipes-core/umip/files/umip_exceptions.c b/meta-luv/recipes-core/umip/files/umip_exceptions.c index 7d454969c76..f387e1f1edc 100644 --- a/meta-luv/recipes-core/umip/files/umip_exceptions.c +++ b/meta-luv/recipes-core/umip/files/umip_exceptions.c @@ -306,7 +306,7 @@ static int setup_data_segments() ret = syscall(SYS_modify_ldt, 1, &desc, sizeof(desc)); if (ret) { - pr_error(test_errors, "Failed to install stack semgnet [%d].\n", ret); + pr_error(test_errors, "Failed to install stack segment [%d].\n", ret); return ret; } From cde04de8dd7551ddf0afd044972c16dbfa4e88f5 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Wed, 2 Aug 2017 11:13:18 -0700 Subject: [PATCH 042/109] umip: umip_exceptions: rename test_normal_pf as test_pf Rename function test_maperr_pf to be more descriptive of what the test does. Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/umip_exceptions.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/umip_exceptions.c b/meta-luv/recipes-core/umip/files/umip_exceptions.c index f387e1f1edc..363285840a2 100644 --- a/meta-luv/recipes-core/umip/files/umip_exceptions.c +++ b/meta-luv/recipes-core/umip/files/umip_exceptions.c @@ -71,7 +71,7 @@ static void handler(int signum, siginfo_t *info, void *ctx_void) #endif } -static void test_normal_pf(void) +static void test_maperr_pf(void) { unsigned long *val_bad = (unsigned long *)0x100000; @@ -425,7 +425,7 @@ int main (void) exit(1); } - test_normal_pf(); + test_maperr_pf(); test_lock_prefix(); test_register_operand(); test_null_segment_selectors(); From b95032209258b90ca8f721ba7a98f8b844ca12b3 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Wed, 2 Aug 2017 11:15:12 -0700 Subject: [PATCH 043/109] umip: umip_exceptions: add message to describe the purpose of the tests This makes this test more uniform to the manner in which other tests are written. Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/umip_exceptions.c | 1 + 1 file changed, 1 insertion(+) diff --git a/meta-luv/recipes-core/umip/files/umip_exceptions.c b/meta-luv/recipes-core/umip/files/umip_exceptions.c index 363285840a2..db421269e7c 100644 --- a/meta-luv/recipes-core/umip/files/umip_exceptions.c +++ b/meta-luv/recipes-core/umip/files/umip_exceptions.c @@ -75,6 +75,7 @@ static void test_maperr_pf(void) { unsigned long *val_bad = (unsigned long *)0x100000; + pr_info("Test page fault because unmapped memory for smsw with addr %p\n", 0x100000); asm volatile ("smsw %0\n" "nop\n" "nop\n" From 715ebd07f14ec0b461962f46800609d27754f3a0 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Wed, 2 Aug 2017 13:12:36 -0700 Subject: [PATCH 044/109] umip: umip_exceptions: add more tests for unmapped address page faults Add test for all instrutions trying to access an unmapped memory location with the expectation of causing a page fault. Signed-off-by: Ricardo Neri --- .../recipes-core/umip/files/umip_exceptions.c | 90 ++++++++++++------- 1 file changed, 58 insertions(+), 32 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/umip_exceptions.c b/meta-luv/recipes-core/umip/files/umip_exceptions.c index db421269e7c..5e07389be0e 100644 --- a/meta-luv/recipes-core/umip/files/umip_exceptions.c +++ b/meta-luv/recipes-core/umip/files/umip_exceptions.c @@ -71,41 +71,67 @@ static void handler(int signum, siginfo_t *info, void *ctx_void) #endif } -static void test_maperr_pf(void) -{ - unsigned long *val_bad = (unsigned long *)0x100000; - - pr_info("Test page fault because unmapped memory for smsw with addr %p\n", 0x100000); - asm volatile ("smsw %0\n" - "nop\n" - "nop\n" - "nop\n" - "nop\n" - "nop\n" - "nop\n" - "nop\n" - "nop\n" - "nop\n": "=m"(*val_bad)); - - if (!got_signal) { - pr_fail(test_failed, "Signal not received!\n"); - return; - } #ifdef __x86_64__ - if (signal_code != SI_KERNEL) { - pr_fail(test_failed, "Signal code is not what we expect.\n"); - return; - } - pr_pass(test_passed, "A SEGV_MAPERR page fault was issued.\n"); - return; +#define inspect_signal(sigcode) \ + do { \ + if (sigcode != SI_KERNEL) { \ + pr_fail(test_failed, "Signal code is not what we expect.\n");\ + return; \ + } \ + pr_pass(test_passed, "A SEGV_MAPERR page fault was issued.\n"); \ + return; \ + } while (0) #else - if (signal_code != SEGV_MAPERR) { - pr_fail(test_failed, "Signal code is not what we expect.\n"); - return; - } - pr_pass(test_passed, "A SEGV_MAPERR page fault was issued.\n"); - return; +#define inspect_signal(sigcode) \ + do { \ + if (sigcode != SEGV_MAPERR) { \ + pr_fail(test_failed, "Signal code is not what we expect.\n");\ + return; \ + } \ + pr_pass(test_passed, "A SEGV_MAPERR page fault was issued.\n"); \ + return; \ + } while (0) #endif + +#define gen_test_maperr_pf_inst(inst, bad_addr) \ +static void __test_maperr_pf_##inst(void) \ +{ \ + unsigned long *val_bad = (unsigned long *)bad_addr; \ + \ + signal_code = 0; \ + \ + pr_info("Test page fault because unmapped memory for %s with addr %p\n", #inst, val_bad);\ + asm volatile (#inst" %0\n" \ + "nop\n" \ + "nop\n" \ + "nop\n" \ + "nop\n" \ + "nop\n" \ + "nop\n" \ + "nop\n" \ + "nop\n" \ + "nop\n": "=m"(*val_bad)); \ + \ + if (!got_signal) { \ + pr_fail(test_failed, "Signal not received!\n"); \ + return; \ + } \ + inspect_signal(signal_code); \ +} + +gen_test_maperr_pf_inst(smsw, 0x100000) +gen_test_maperr_pf_inst(sidt, 0x100000) +gen_test_maperr_pf_inst(sgdt, 0x100000) +gen_test_maperr_pf_inst(str, 0x100000) +gen_test_maperr_pf_inst(sldt, 0x100000) + +static void test_maperr_pf(void) +{ + __test_maperr_pf_smsw(); + __test_maperr_pf_sidt(); + __test_maperr_pf_sgdt(); + __test_maperr_pf_str(); + __test_maperr_pf_sldt(); } #define gen_test_lock_prefix_inst(name, inst) \ From 832014b245c7895f19c48ed22a360ed9c6866dcc Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Wed, 2 Aug 2017 14:54:29 -0700 Subject: [PATCH 045/109] umip: definitions: add macros to print test results These macros are useful when we know the test result and its expected value. Rather than writing this pattern multiple times, simply wrap it in a macro. We need two kinds of macros as sometimes the test results fits in a single variable while y some other cases is of type table_desc. Signed-off-by: Ricardo Neri --- .../recipes-core/umip/files/umip_test_defs.h | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/meta-luv/recipes-core/umip/files/umip_test_defs.h b/meta-luv/recipes-core/umip/files/umip_test_defs.h index 846cc4e0645..294819e210b 100644 --- a/meta-luv/recipes-core/umip/files/umip_test_defs.h +++ b/meta-luv/recipes-core/umip/files/umip_test_defs.h @@ -12,6 +12,35 @@ #define pr_info(...) printf(TEST_INFO __VA_ARGS__) #define pr_error(error_ctr, ...) do{ printf(TEST_ERROR __VA_ARGS__); error_ctr++; } while(0) +/* + * Use this definiton to check for results that fit in a single variable + * (e.g., char, short, int, long, double) + */ +#define pr_result(result, expected, text, pass_ctr, fail_ctr) \ + do{ \ + if (result == expected) \ + pr_pass(pass_ctr, text); \ + else \ + pr_fail(fail_ctr, text); \ + printf("Got:[0x%x]Exp[0x%x]\n", result, expected); \ + } while(0) + +/* + * Use this definiton to check for results that in struct table_desc + * (e.g., char, short, int, long, double) + */ +#define pr_result_table(result, expected, text, pass_ctr, fail_ctr) \ + do{ \ + if ((result->base == expected->base) && \ + (result->limit == expected->limit)) \ + pr_pass(pass_ctr, text); \ + else \ + pr_fail(fail_ctr, text); \ + printf("Got:Base[0x%lx]Limit[0x%x]ExpBase[0x%lx]Limit[0x%x]\n", \ + got->base, got->limit, \ + expected->base, expected->limit); \ + } while(0) + #ifdef __x86_64__ #define PRINT_BITNESS pr_info("This binary uses 64-bit code\n") #define INIT_VAL(val) (0x##val##val) From 503bdee61225d02bf3b775b31f7496fb7ee0c5ee Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Wed, 2 Aug 2017 14:56:39 -0700 Subject: [PATCH 046/109] umip: 16-bit LDT tests: update code generator to count test cases Update the code generator to define and increment variables that count the number of passed and failed test cases. Also, define a variable to count the number of errors. Signed-off-by: Ricardo Neri --- .../umip/files/umip_test_gen_16.py | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/umip_test_gen_16.py b/meta-luv/recipes-core/umip/files/umip_test_gen_16.py index 2d60b138850..6c788e57de5 100644 --- a/meta-luv/recipes-core/umip/files/umip_test_gen_16.py +++ b/meta-luv/recipes-core/umip/files/umip_test_gen_16.py @@ -17,6 +17,10 @@ SEGMENT_SIZE = 32768 CODE_MEM_SIZE = 32768 +TEST_PASS_CTR_VAR = "test_passed" +TEST_FAIL_CTR_VAR = "test_failed" +TEST_ERROR_CTR_VAR = "test_errors" + class Instruction: def __init__(self, name, opcode, modrm_reg, result_bytes, expected_val): self.name = name @@ -116,16 +120,15 @@ def get_segment_prefix(segment, register, modrm): return segment_str, segment_chk_str -def generate_check_code(comment, segment_chk_str, address, inst): +def generate_check_code(comment, segment_chk_str, address, inst, pass_ctr, fail_ctr): # TODO: Use an enum here + comment += ". " if (inst.result_bytes == 2): checkcode = "\tgot = *(unsigned short *)(" + segment_chk_str +" + " + str(my_hex(address)) + ");\n" - checkcode += "\tprintf(\"%s " + comment + ". Got:[0x%x]Exp[0x%x]\\n\",\n" - checkcode += "\t got==expected ? TEST_PASS : TEST_FAIL, got, expected);\n" + checkcode += "\tpr_result(got, expected, \"" + comment + "\", " + pass_ctr + ", " + fail_ctr + ");\n" elif (inst.result_bytes == 6): checkcode = "\tgot = (struct table_desc *)(" + segment_chk_str +" + " + str(my_hex(address)) + ");\n" - checkcode += "\tprintf(\"%s " + comment + ". Got:Base[0x%lx]Limit[0x%x]ExpBase[0x%lx]Limit[0x%x]\\n\",\n" - checkcode += "\t ((got->base == expected->base) && (got->limit == expected->limit)) ? TEST_PASS : TEST_FAIL, got->base, got->limit, expected->base, expected->limit);\n" + checkcode += "\tpr_result_table(got, expected, \"" + comment + "\", " + pass_ctr + ", " + fail_ctr + ");\n" return checkcode def generate_disp(modrm, disp): @@ -201,7 +204,7 @@ def generate_code(tc_nr, segment, inst, register, modrm_mod, index, disp): code += mov_reg_str code += code_start + segment_str + opcode_str + modrm_str + disp_str + code_end - checkcode = generate_check_code(comment, segment_chk_str, index + disp, inst) + checkcode = generate_check_code(comment, segment_chk_str, index + disp, inst, TEST_PASS_CTR_VAR, TEST_FAIL_CTR_VAR) return code, checkcode, inst.result_bytes @@ -231,7 +234,7 @@ def generate_special_unit_tests(start_tc_nr, segment, inst, start_idx): code = "\t/* " + comment + " */\n" code += code_start + segment_str + opcode_str + modrm_str + disp_str + code_end - checkcode += generate_check_code(comment, segment_chk_str, index, inst) + checkcode += generate_check_code(comment, segment_chk_str, index, inst, TEST_PASS_CTR_VAR, TEST_FAIL_CTR_VAR) index += inst.result_bytes tc_nr += 1 @@ -339,6 +342,10 @@ def generate_test_cases(test_code, check_code): check_code += "#include \"test_umip_ldt_16.h\"\n\n" check_code += "#include \"umip_test_defs.h\"\n\n" check_code += "\n" + check_code +="int " + TEST_PASS_CTR_VAR + ";\n" + check_code +="int " + TEST_FAIL_CTR_VAR + ";\n" + check_code +="int " + TEST_ERROR_CTR_VAR + ";\n" + check_code += "\n" run_check_code = "" From 58841946c92ef5a17c32b45166bbccd93f1bb98d Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Wed, 2 Aug 2017 14:57:54 -0700 Subject: [PATCH 047/109] umip: 16-bit LDT tests: count the number of tests Update test result macros to also count the number of tests cases that pass, fail or give errors. --- .../recipes-core/umip/files/umip_ldt_16.c | 43 ++++++++++++------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/umip_ldt_16.c b/meta-luv/recipes-core/umip/files/umip_ldt_16.c index e08c83d91e8..11719dbd2a3 100644 --- a/meta-luv/recipes-core/umip/files/umip_ldt_16.c +++ b/meta-luv/recipes-core/umip/files/umip_ldt_16.c @@ -34,6 +34,14 @@ unsigned short cs_orig; static sig_atomic_t got_signal; +extern int test_passed, test_failed, test_errors; + +static void print_results(void) +{ + printf("RESULTS: passed[%d], failed[%d], error[%d]\n", + test_passed, test_failed, test_errors); +} + void handler(int signum, siginfo_t *info, void *ctx_void) { pr_info("si_signo[%d]\n", info->si_signo); @@ -41,7 +49,7 @@ void handler(int signum, siginfo_t *info, void *ctx_void) pr_info("si_code[%d]\n", info->si_code); pr_info("si_addr[0x%p]\n", info->si_addr); if (signum != SIGSEGV) - pr_error("Received unexpected signal"); + pr_error(test_errors, "Received unexpected signal"); else got_signal = signum; if (info->si_code == SEGV_MAPERR) @@ -51,7 +59,8 @@ void handler(int signum, siginfo_t *info, void *ctx_void) else pr_info("Unknown si_code!\n"); - pr_fail("Whoa! I got a SIGSEGV! Something went wrong!\n"); + pr_fail(test_failed, "Whoa! I got a SIGSEGV! Something went wrong!\n"); + print_results(); exit(1); } @@ -126,7 +135,7 @@ static int setup_data_segments() ret = syscall(SYS_modify_ldt, 1, &desc, sizeof(desc)); if (ret) { - pr_error("Failed to install stack semgnet [%d].\n", ret); + pr_error(test_errors, "Failed to install stack semgnet [%d].\n", ret); return ret; } @@ -137,7 +146,7 @@ static int setup_data_segments() ret = syscall(SYS_modify_ldt, 1, &desc, sizeof(desc)); if (ret) { - pr_error("Failed to install data segment [%d].\n", ret); + pr_error(test_errors, "Failed to install data segment [%d].\n", ret); return ret; } @@ -148,7 +157,7 @@ static int setup_data_segments() ret = syscall(SYS_modify_ldt, 1, &desc, sizeof(desc)); if (ret) { - pr_error("Failed to install data segment [%d].\n", ret); + pr_error(test_errors, "Failed to install data segment [%d].\n", ret); return ret; } @@ -159,7 +168,7 @@ static int setup_data_segments() ret = syscall(SYS_modify_ldt, 1, &desc, sizeof(desc)); if (ret) { - pr_error("Failed to install data segment [%d].\n", ret); + pr_error(test_errors, "Failed to install data segment [%d].\n", ret); return ret; } @@ -170,7 +179,7 @@ static int setup_data_segments() ret = syscall(SYS_modify_ldt, 1, &desc, sizeof(desc)); if (ret) { - pr_error("Failed to install data segment [%d].\n", ret); + pr_error(test_errors, "Failed to install data segment [%d].\n", ret); return ret; } @@ -205,14 +214,14 @@ int run_umip_ldt_test(void) sigemptyset(&action.sa_mask); if (sigaction(SIGSEGV, &action, NULL) < 0) { - pr_error("Could not set the signal handler!"); + pr_error(test_errors, "Could not set the signal handler!"); goto err_out; } code_interim = mmap(NULL, 4096, PROT_WRITE | PROT_READ | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (!code_interim) { - pr_error("Failed to allocate memory for interim code segment!\n"); + pr_error(test_errors, "Failed to allocate memory for interim code segment!\n"); goto err_out; } @@ -221,7 +230,7 @@ int run_umip_ldt_test(void) code_16 = mmap(NULL, CODE_MEM_SIZE, PROT_WRITE | PROT_READ | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (!code_16) { - pr_error("Failed to allocate memory for code segment!\n"); + pr_error(test_errors, "Failed to allocate memory for code segment!\n"); goto err_out; } @@ -233,7 +242,7 @@ int run_umip_ldt_test(void) ret = syscall(SYS_modify_ldt, 1, &code_desc, sizeof(code_desc)); if (ret) { - pr_error("Failed to install interim code segment [%d].\n", ret); + pr_error(test_errors, "Failed to install interim code segment [%d].\n", ret); goto err_out; } @@ -245,12 +254,12 @@ int run_umip_ldt_test(void) ret = syscall(SYS_modify_ldt, 1, &code_desc, sizeof(code_desc)); if (ret) { - pr_error("Failed to install 16-bit code segment [%d].\n", ret); + pr_error(test_errors, "Failed to install 16-bit code segment [%d].\n", ret); goto err_out; } if (setup_data_segments()) { - pr_error("Failed to setup segments [%d].\n", ret); + pr_error(test_errors, "Failed to setup segments [%d].\n", ret); goto err_out; } @@ -348,14 +357,16 @@ int run_umip_ldt_test(void) sigemptyset(&action.sa_mask); if (sigaction(SIGSEGV, &action, NULL) < 0) { - pr_error("Could not remove signal handler!"); - exit(1); + pr_error(test_errors, "Could not remove signal handler!"); + goto err_out; } printf("Exiting...\n"); + print_results(); return 0; err_out: - pr_error("Could not run tests\n"); + pr_error(test_errors, "Could not run tests\n"); + print_results(); return 1; }; From 994219e384134cb693219f8caa0c7301acc11f1b Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Wed, 2 Aug 2017 17:08:58 -0700 Subject: [PATCH 048/109] umip: 32-bit LDT tests: remove unused definitions of expected values Remove unused defintions of the expected values from the code generator. Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/umip_test_gen_32.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/umip_test_gen_32.py b/meta-luv/recipes-core/umip/files/umip_test_gen_32.py index 181a666ae46..08a3da64b87 100644 --- a/meta-luv/recipes-core/umip/files/umip_test_gen_32.py +++ b/meta-luv/recipes-core/umip/files/umip_test_gen_32.py @@ -17,15 +17,6 @@ SEGMENT_SIZE = 262144 CODE_MEM_SIZE = 262144 -#expected values -EXPECTED_SMSW = 0x33 -EXPECTED_SLDT = 0x0 -EXPECTED_STR = 0x0 -EXPECTED_GDT_BASE = 0xfffe0000 -EXPECTED_GDT_LIMIT = 0x0 -EXPECTED_IDT_BASE = 0xffff0000 -EXPECTED_IDT_LIMIT = 0x0 - class Instruction: def __init__(self, name, opcode, modrm_reg, result_bytes, expected_val): self.name = name From 57016b5eb197fa64fda8c933dac3f5f3f1011479 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Thu, 3 Aug 2017 11:51:46 -0700 Subject: [PATCH 049/109] umip: 32-bit LDT tests: update code generator to count test cases Update the code generator to define and increment variables that count the number of passed and failed test cases. Also, define a variable to count the number of errors. Signed-off-by: Ricardo Neri --- .../umip/files/umip_test_gen_32.py | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/umip_test_gen_32.py b/meta-luv/recipes-core/umip/files/umip_test_gen_32.py index 08a3da64b87..677a0721837 100644 --- a/meta-luv/recipes-core/umip/files/umip_test_gen_32.py +++ b/meta-luv/recipes-core/umip/files/umip_test_gen_32.py @@ -17,6 +17,11 @@ SEGMENT_SIZE = 262144 CODE_MEM_SIZE = 262144 +TEST_PASS_CTR_VAR = "test_passed" +TEST_FAIL_CTR_VAR = "test_failed" +TEST_ERROR_CTR_VAR = "test_errors" + + class Instruction: def __init__(self, name, opcode, modrm_reg, result_bytes, expected_val): self.name = name @@ -116,16 +121,14 @@ def get_segment_prefix(segment, register, modrm, sib=0): return segment_str, segment_chk_str -def generate_check_code(comment, segment_chk_str, address, inst): +def generate_check_code(comment, segment_chk_str, address, inst, pass_ctr, fail_ctr): # TODO: Use an enum here if (inst.result_bytes == 2): checkcode = "\tgot = *(unsigned short *)(" + segment_chk_str +" + " + str(my_hex(address)) + ");\n" - checkcode += "\tprintf(\"%s " + comment + ". Got:[0x%x]Exp[0x%x]\\n\",\n" - checkcode += "\t got==expected ? TEST_PASS : TEST_FAIL, got, expected);\n" + checkcode += "\tpr_result(got, expected, \"" + comment + "\", " + pass_ctr + ", " + fail_ctr + ");\n" elif (inst.result_bytes == 6): checkcode = "\tgot = (struct table_desc *)(" + segment_chk_str +" + " + str(my_hex(address)) + ");\n" - checkcode += "\tprintf(\"%s " + comment + ". Got:Base[0x%lx]Limit[0x%x]ExpBase[0x%lx]Limit[0x%x]\\n\",\n" - checkcode += "\t ((got->base == expected->base) && (got->limit == expected->limit)) ? TEST_PASS : TEST_FAIL, got->base, got->limit, expected->base, expected->limit);\n" + checkcode += "\tpr_result_table(got, expected, \"" + comment + "\", " + pass_ctr + ", " + fail_ctr + ");\n" return checkcode def generate_disp(modrm, disp, sib=0): @@ -189,7 +192,7 @@ def generate_code(tc_nr, segment, inst, register, modrm_mod, index, disp): code += mov_reg_str code += code_start + segment_str + opcode_str + modrm_str + disp_str + code_end - checkcode = generate_check_code(comment, segment_chk_str, index + disp, inst) + checkcode = generate_check_code(comment, segment_chk_str, index + disp, inst, TEST_PASS_CTR_VAR, TEST_FAIL_CTR_VAR) return code, checkcode, inst.result_bytes @@ -260,7 +263,7 @@ def generate_code_sib(tc_nr, segment, inst, reg_base, reg_index, modrm_mod, sib_ code += code_start + segment_str + opcode_str + modrm_str + sib_str + disp_str + code_end code += restore_str - checkcode = generate_check_code(comment, segment_chk_str, eff_addr, inst) + checkcode = generate_check_code(comment, segment_chk_str, eff_addr, inst, TEST_PASS_CTR_VAR, TEST_FAIL_CTR_VAR) return code, checkcode, inst.result_bytes @@ -401,7 +404,7 @@ def generate_special_unit_tests(start_tc_nr, segment, inst, start_idx): code = "\t/* " + comment + " */\n" code += code_start + segment_str + opcode_str + modrm_str + disp_str +code_end - checkcode += generate_check_code(comment, segment_chk_str, index, inst) + checkcode += generate_check_code(comment, segment_chk_str, index, inst, TEST_PASS_CTR_VAR, TEST_FAIL_CTR_VAR) index += inst.result_bytes tc_nr += 1 @@ -572,6 +575,10 @@ def generate_test_cases(test_code, check_code): check_code += "#include \"test_umip_ldt_32.h\"\n\n" check_code += "#include \"umip_test_defs.h\"\n\n" check_code += "\n" + check_code +="int " + TEST_PASS_CTR_VAR + ";\n" + check_code +="int " + TEST_FAIL_CTR_VAR + ";\n" + check_code +="int " + TEST_ERROR_CTR_VAR + ";\n" + check_code += "\n" run_check_code = "" From 7ea5ebb8a7ce5f98b339391adfbc08706cd1588f Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Thu, 3 Aug 2017 11:55:47 -0700 Subject: [PATCH 050/109] umip: 32-bit LDT tests: count the number of tests Update test result macros to also count the number of tests cases that pass, fail or give errors. Signed-off-by: Ricardo Neri --- .../recipes-core/umip/files/umip_ldt_32.c | 38 ++++++++++++------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/umip_ldt_32.c b/meta-luv/recipes-core/umip/files/umip_ldt_32.c index bb9adde53c0..85d20f055c9 100644 --- a/meta-luv/recipes-core/umip/files/umip_ldt_32.c +++ b/meta-luv/recipes-core/umip/files/umip_ldt_32.c @@ -33,6 +33,14 @@ unsigned short cs_orig; static sig_atomic_t got_signal; +extern int test_passed, test_failed, test_errors; + +static void print_results(void) +{ + printf("RESULTS: passed[%d], failed[%d], error[%d]\n", + test_passed, test_failed, test_errors); +} + void handler(int signum, siginfo_t *info, void *ctx_void) { pr_info("si_signo[%d]\n", info->si_signo); @@ -40,7 +48,7 @@ void handler(int signum, siginfo_t *info, void *ctx_void) pr_info("si_code[%d]\n", info->si_code); pr_info("si_addr[0x%p]\n", info->si_addr); if (signum != SIGSEGV) - pr_error("Received unexpected signal"); + pr_error(test_errors, "Received unexpected signal"); else got_signal = signum; if (info->si_code == SEGV_MAPERR) @@ -50,7 +58,8 @@ void handler(int signum, siginfo_t *info, void *ctx_void) else pr_info("Unknown si_code!\n"); - pr_fail("Whoa! I got a SIGSEGV! Something went wrong!\n"); + pr_fail(test_failed, "Whoa! I got a SIGSEGV! Something went wrong!\n"); + print_results(); exit(1); } @@ -76,7 +85,7 @@ static int setup_data_segments() ret = syscall(SYS_modify_ldt, 1, &desc, sizeof(desc)); if (ret) { - pr_error("Failed to install stack semgnet [%d].\n", ret); + pr_error(test_errors, "Failed to install stack semgnet [%d].\n", ret); return ret; } @@ -87,7 +96,7 @@ static int setup_data_segments() ret = syscall(SYS_modify_ldt, 1, &desc, sizeof(desc)); if (ret) { - pr_error("Failed to install data segment [%d].\n", ret); + pr_error(test_errors, "Failed to install data segment [%d].\n", ret); return ret; } @@ -98,7 +107,7 @@ static int setup_data_segments() ret = syscall(SYS_modify_ldt, 1, &desc, sizeof(desc)); if (ret) { - pr_error("Failed to install data segment [%d].\n", ret); + pr_error(test_errors, "Failed to install data segment [%d].\n", ret); return ret; } @@ -109,7 +118,7 @@ static int setup_data_segments() ret = syscall(SYS_modify_ldt, 1, &desc, sizeof(desc)); if (ret) { - pr_error("Failed to install data segment [%d].\n", ret); + pr_error(test_errors, "Failed to install data segment [%d].\n", ret); return ret; } @@ -120,7 +129,7 @@ static int setup_data_segments() ret = syscall(SYS_modify_ldt, 1, &desc, sizeof(desc)); if (ret) { - pr_error("Failed to install data segment [%d].\n", ret); + pr_error(test_errors, "Failed to install data segment [%d].\n", ret); return ret; } @@ -153,14 +162,14 @@ int run_umip_ldt_test(void) sigemptyset(&action.sa_mask); if (sigaction(SIGSEGV, &action, NULL) < 0) { - pr_error("Could not set the signal handler!"); + pr_error(test_errors, "Could not set the signal handler!"); goto err_out; } code = mmap(NULL, CODE_MEM_SIZE, PROT_WRITE | PROT_READ | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (!code) { - pr_error("Failed to allocate memory for code segment!\n"); + pr_error(test_errors, "Failed to allocate memory for code segment!\n"); goto err_out; } @@ -171,12 +180,12 @@ int run_umip_ldt_test(void) ret = syscall(SYS_modify_ldt, 1, &code_desc, sizeof(code_desc)); if (ret) { - pr_error("Failed to install code segment [%d].\n", ret); + pr_error(test_errors, "Failed to install code segment [%d].\n", ret); goto err_out; } if (setup_data_segments()) { - pr_error("Failed to setup segments [%d].\n", ret); + pr_error(test_errors, "Failed to setup segments [%d].\n", ret); goto err_out; } @@ -260,15 +269,18 @@ int run_umip_ldt_test(void) sigemptyset(&action.sa_mask); if (sigaction(SIGSEGV, &action, NULL) < 0) { - pr_error("Could not remove signal handler!"); + pr_error(test_errors, "Could not remove signal handler!"); + print_results(); exit(1); } pr_info("Exiting...\n"); + print_results(); return 0; err_out: - pr_error("Could not run tests\n"); + pr_error(test_errors, "Could not run tests\n"); + print_results(); return 1; }; From b1b697a05acd52b220c52bc5a99fec2158b59847 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Thu, 3 Aug 2017 12:19:23 -0700 Subject: [PATCH 051/109] umip: 64-bit LDT tests: update code generator to count test cases Update the code generator to define and increment variables that count the number of passed and failed test cases. Also, define a variable to count the number of errors. Signed-off-by: Ricardo Neri --- .../umip/files/umip_test_gen_64.py | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/umip_test_gen_64.py b/meta-luv/recipes-core/umip/files/umip_test_gen_64.py index 795fc12a29e..7f815ae08b5 100644 --- a/meta-luv/recipes-core/umip/files/umip_test_gen_64.py +++ b/meta-luv/recipes-core/umip/files/umip_test_gen_64.py @@ -17,6 +17,10 @@ SEGMENT_SIZE = 1048576 CODE_MEM_SIZE = 1048576 +TEST_PASS_CTR_VAR = "test_passed" +TEST_FAIL_CTR_VAR = "test_failed" +TEST_ERROR_CTR_VAR = "test_errors" + class Instruction: def __init__(self, name, opcode, modrm_reg, result_bytes, expected_val): self.name = name @@ -132,16 +136,14 @@ def get_segment_prefix(segment, register, modrm, sib=0): return segment_str, segment_chk_str -def generate_check_code(comment, segment_chk_str, address, inst): +def generate_check_code(comment, segment_chk_str, address, inst, pass_ctr, fail_ctr): # TODO: Use an enum here if (inst.result_bytes == 2): checkcode = "\tgot = *(unsigned short *)(" + segment_chk_str +" + " + str(my_hex(address)) + ");\n" - checkcode += "\tprintf(\"%s " + comment + ". Got:[0x%x]Exp[0x%x]\\n\",\n" - checkcode += "\t got==expected ? TEST_PASS : TEST_FAIL, got, expected);\n" + checkcode += "\tpr_result(got, expected, \"" + comment + "\", " + pass_ctr + ", " + fail_ctr + ");\n" elif (inst.result_bytes == 6): checkcode = "\tgot = (struct table_desc *)(" + segment_chk_str +" + " + str(my_hex(address)) + ");\n" - checkcode += "\tprintf(\"%s " + comment + ". Got:Base[0x%lx]Limit[0x%x]ExpBase[0x%lx]Limit[0x%x]\\n\",\n" - checkcode += "\t ((got->base == expected->base) && (got->limit == expected->limit)) ? TEST_PASS : TEST_FAIL, got->base, got->limit, expected->base, expected->limit);\n" + checkcode += "\tpr_result_table(got, expected, \"" + comment + "\", " + pass_ctr + ", " + fail_ctr + ");\n" return checkcode def generate_disp(modrm, disp, sib=0): @@ -210,7 +212,7 @@ def generate_code(tc_nr, segment, inst, register, modrm_mod, index, disp): code += mov_reg_str code += code_start + segment_str + rex_b_str + opcode_str + modrm_str + disp_str + code_end - checkcode = generate_check_code(comment, segment_chk_str, index + disp, inst) + checkcode = generate_check_code(comment, segment_chk_str, index + disp, inst, TEST_PASS_CTR_VAR, TEST_FAIL_CTR_VAR) return code, checkcode, inst.result_bytes @@ -286,7 +288,7 @@ def generate_code_sib(tc_nr, segment, inst, reg_base, reg_index, modrm_mod, sib_ code += code_start + segment_str + rex_b_str + opcode_str + modrm_str + sib_str + disp_str + code_end code += restore_str - checkcode = generate_check_code(comment, segment_chk_str, eff_addr, inst) + checkcode = generate_check_code(comment, segment_chk_str, eff_addr, inst, TEST_PASS_CTR_VAR, TEST_FAIL_CTR_VAR) return code, checkcode, inst.result_bytes @@ -429,7 +431,7 @@ def generate_special_unit_tests(start_tc_nr, segment, inst, start_idx): code += "\t\"1f:\\n\\t\"\n" code += code_start + segment_str + opcode_str + modrm_str + disp_str +code_end - checkcode += generate_check_code(comment, segment_chk_str, index, inst) + checkcode += generate_check_code(comment, segment_chk_str, index, inst, TEST_PASS_CTR_VAR, TEST_FAIL_CTR_VAR) index += inst.result_bytes tc_nr += 1 @@ -598,7 +600,10 @@ def generate_test_cases(test_code, check_code): check_code += "#include \"test_umip_ldt_64.h\"\n\n" check_code += "#include \"umip_test_defs.h\"\n\n" check_code += "\n" - + check_code +="int " + TEST_PASS_CTR_VAR + ";\n" + check_code +="int " + TEST_FAIL_CTR_VAR + ";\n" + check_code +="int " + TEST_ERROR_CTR_VAR + ";\n" + check_code += "\n" run_check_code = "" test_code += "\tasm(\n" From e97d2f8f412f4ae4be7aef72fde42bff2fedae2f Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Thu, 3 Aug 2017 12:20:04 -0700 Subject: [PATCH 052/109] umip: 64-bit LDT tests: count the number of tests Update test result macros to also count the number of tests cases that pass, fail or give errors. Signed-off-by: Ricardo Neri --- .../recipes-core/umip/files/umip_ldt_64.c | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/umip_ldt_64.c b/meta-luv/recipes-core/umip/files/umip_ldt_64.c index 44bb306b360..44f665b359c 100644 --- a/meta-luv/recipes-core/umip/files/umip_ldt_64.c +++ b/meta-luv/recipes-core/umip/files/umip_ldt_64.c @@ -31,6 +31,14 @@ unsigned short old_fs, old_gs; static sig_atomic_t got_signal; +extern int test_passed, test_failed, test_errors; + +static void print_results(void) +{ + printf("RESULTS: passed[%d], failed[%d], error[%d]\n", + test_passed, test_failed, test_errors); +} + void handler(int signum, siginfo_t *info, void *ctx_void) { pr_info("si_signo[%d]\n", info->si_signo); @@ -38,7 +46,7 @@ void handler(int signum, siginfo_t *info, void *ctx_void) pr_info("si_code[%d]\n", info->si_code); pr_info("si_addr[0x%p]\n", info->si_addr); if (signum != SIGSEGV) - pr_error("Received unexpected signal"); + pr_error(test_errors, "Received unexpected signal"); else got_signal = signum; if (info->si_code == SEGV_MAPERR) @@ -48,7 +56,8 @@ void handler(int signum, siginfo_t *info, void *ctx_void) else pr_info("Unknown si_code!\n"); - pr_fail("Whoa! I got a SIGSEGV! Something went wrong!\n"); + pr_fail(test_failed, "Whoa! I got a SIGSEGV! Something went wrong!\n"); + print_results(); exit(1); } @@ -107,7 +116,7 @@ int main(void) sigemptyset(&action.sa_mask); if (sigaction(SIGSEGV, &action, NULL) < 0) { - pr_error("Could not set the signal handler!"); + pr_error(test_errors, "Could not set the signal handler!"); goto err_out; } @@ -125,7 +134,7 @@ int main(void) ret = setup_data_segments(); if (ret) { - pr_error("Failed to setup segments [%d].\n", ret); + pr_error(test_errors, "Failed to setup segments [%d].\n", ret); goto err_out; } @@ -176,14 +185,18 @@ int main(void) sigemptyset(&action.sa_mask); if (sigaction(SIGSEGV, &action, NULL) < 0) { - pr_error("Could not remove signal handler!"); + pr_error(test_errors, "Could not remove signal handler!"); + print_results(); exit(1); } printf("Exiting...\n"); + print_results(); return 0; err_out: - pr_error("Could not run tests\n"); + pr_error(test_errors, "Could not run tests\n"); + print_results(); + return 1; }; From 09b51414522effa6deaba1eec98a82d84e878267 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Thu, 3 Aug 2017 12:20:28 -0700 Subject: [PATCH 053/109] umip: umip_ldt_64: Add a warning about segfault This test program creates a local descriptor table using as segment bases the GS and FS registers. These are used also by glibc for per-thread data. The test program has a yet unidentified bug in the manner in which it handles such registers. While this is investigated, add a warning about this situation. Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/umip_ldt_64.c | 1 + 1 file changed, 1 insertion(+) diff --git a/meta-luv/recipes-core/umip/files/umip_ldt_64.c b/meta-luv/recipes-core/umip/files/umip_ldt_64.c index 44f665b359c..8eaa4e32763 100644 --- a/meta-luv/recipes-core/umip/files/umip_ldt_64.c +++ b/meta-luv/recipes-core/umip/files/umip_ldt_64.c @@ -197,6 +197,7 @@ int main(void) pr_error(test_errors, "Could not run tests\n"); print_results(); + printf("Now you will see a segmentation fault. This is under investigation.\n"); return 1; }; From dbf5e2c3c20f963a4094e5205cd344f93299b740 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Fri, 4 Aug 2017 13:30:54 -0700 Subject: [PATCH 054/109] umip: add utility library for common functionality Instead of repeating the same functions in each test file put them in a single utility library. Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/umip_test_defs.h | 2 ++ meta-luv/recipes-core/umip/files/umip_utils.c | 14 ++++++++++++++ 2 files changed, 16 insertions(+) create mode 100644 meta-luv/recipes-core/umip/files/umip_utils.c diff --git a/meta-luv/recipes-core/umip/files/umip_test_defs.h b/meta-luv/recipes-core/umip/files/umip_test_defs.h index 294819e210b..64f2bbc0bf5 100644 --- a/meta-luv/recipes-core/umip/files/umip_test_defs.h +++ b/meta-luv/recipes-core/umip/files/umip_test_defs.h @@ -76,4 +76,6 @@ static const struct table_desc expected_idt = { .base = EXPECTED_IDT_BASE }; +void print_results(void); + #endif /* _UMIP_TEST_DEFS_H */ diff --git a/meta-luv/recipes-core/umip/files/umip_utils.c b/meta-luv/recipes-core/umip/files/umip_utils.c new file mode 100644 index 00000000000..b406604e914 --- /dev/null +++ b/meta-luv/recipes-core/umip/files/umip_utils.c @@ -0,0 +1,14 @@ +/* + * tests for Intel User-Mode Execution Prevention + * + * GPLv2 + */ +#include + +extern int test_passed, test_failed, test_errors; + +void print_results(void) +{ + printf("RESULTS: passed[%d], failed[%d], error[%d]\n", + test_passed, test_failed, test_errors); +} From 392feead2dc7882895c6e410fee1726b10232a86 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Fri, 4 Aug 2017 14:43:23 -0700 Subject: [PATCH 055/109] umip: use utility library to print test results Now that we have a utility library, use it to print test results. As the library needs to have access to the test cases' counter variables. Hence, the static qualifier needs to be removed. Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/umip_exceptions.c | 8 +------- meta-luv/recipes-core/umip/files/umip_ldt_16.c | 8 +------- meta-luv/recipes-core/umip/files/umip_ldt_32.c | 6 ------ meta-luv/recipes-core/umip/files/umip_ldt_64.c | 6 ------ meta-luv/recipes-core/umip/files/umip_test.c | 12 +++--------- meta-luv/recipes-core/umip/files/umip_test2.c | 8 +------- 6 files changed, 6 insertions(+), 42 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/umip_exceptions.c b/meta-luv/recipes-core/umip/files/umip_exceptions.c index 5e07389be0e..ef4a9c0d26d 100644 --- a/meta-luv/recipes-core/umip/files/umip_exceptions.c +++ b/meta-luv/recipes-core/umip/files/umip_exceptions.c @@ -20,13 +20,7 @@ static sig_atomic_t signal_code; static sig_atomic_t got_signal; -static int test_passed, test_failed, test_errors; - -static void print_results(void) -{ - printf("RESULTS: passed[%d], failed[%d], error[%d]\n", - test_passed, test_failed, test_errors); -} +int test_passed, test_failed, test_errors; static void handler(int signum, siginfo_t *info, void *ctx_void) { diff --git a/meta-luv/recipes-core/umip/files/umip_ldt_16.c b/meta-luv/recipes-core/umip/files/umip_ldt_16.c index 11719dbd2a3..2abd5e83e76 100644 --- a/meta-luv/recipes-core/umip/files/umip_ldt_16.c +++ b/meta-luv/recipes-core/umip/files/umip_ldt_16.c @@ -34,13 +34,7 @@ unsigned short cs_orig; static sig_atomic_t got_signal; -extern int test_passed, test_failed, test_errors; - -static void print_results(void) -{ - printf("RESULTS: passed[%d], failed[%d], error[%d]\n", - test_passed, test_failed, test_errors); -} +int test_passed, test_failed, test_errors; void handler(int signum, siginfo_t *info, void *ctx_void) { diff --git a/meta-luv/recipes-core/umip/files/umip_ldt_32.c b/meta-luv/recipes-core/umip/files/umip_ldt_32.c index 85d20f055c9..83c4aced563 100644 --- a/meta-luv/recipes-core/umip/files/umip_ldt_32.c +++ b/meta-luv/recipes-core/umip/files/umip_ldt_32.c @@ -35,12 +35,6 @@ static sig_atomic_t got_signal; extern int test_passed, test_failed, test_errors; -static void print_results(void) -{ - printf("RESULTS: passed[%d], failed[%d], error[%d]\n", - test_passed, test_failed, test_errors); -} - void handler(int signum, siginfo_t *info, void *ctx_void) { pr_info("si_signo[%d]\n", info->si_signo); diff --git a/meta-luv/recipes-core/umip/files/umip_ldt_64.c b/meta-luv/recipes-core/umip/files/umip_ldt_64.c index 8eaa4e32763..cdbb42e5457 100644 --- a/meta-luv/recipes-core/umip/files/umip_ldt_64.c +++ b/meta-luv/recipes-core/umip/files/umip_ldt_64.c @@ -33,12 +33,6 @@ static sig_atomic_t got_signal; extern int test_passed, test_failed, test_errors; -static void print_results(void) -{ - printf("RESULTS: passed[%d], failed[%d], error[%d]\n", - test_passed, test_failed, test_errors); -} - void handler(int signum, siginfo_t *info, void *ctx_void) { pr_info("si_signo[%d]\n", info->si_signo); diff --git a/meta-luv/recipes-core/umip/files/umip_test.c b/meta-luv/recipes-core/umip/files/umip_test.c index a5103c2d960..a9309686b22 100644 --- a/meta-luv/recipes-core/umip/files/umip_test.c +++ b/meta-luv/recipes-core/umip/files/umip_test.c @@ -22,15 +22,9 @@ #define IDTR_LEN 6 #endif -static int test_passed, test_failed, test_error; +int test_passed, test_failed, test_errors; static sig_atomic_t got_signal; -static void print_results(void) -{ - printf("RESULTS: passed[%d], failed[%d], error[%d]\n", - test_passed, test_failed, test_errors); -} - static void handler(int signum, siginfo_t *info, void *ctx_void) { pr_info("si_signo[%d]\n", info->si_signo); @@ -238,7 +232,7 @@ int main(void) sigemptyset(&action.sa_mask); if (sigaction(SIGSEGV, &action, NULL) < 0) { - pr_error(test_error, "Could not set the signal handler!"); + pr_error(test_errors, "Could not set the signal handler!"); print_results(); exit(1); } @@ -254,7 +248,7 @@ int main(void) sigemptyset(&action.sa_mask); if (sigaction(SIGSEGV, &action, NULL) < 0) { - pr_error(test_error, "Could not remove signal handler!"); + pr_error(test_errors, "Could not remove signal handler!"); print_results(); exit(1); } diff --git a/meta-luv/recipes-core/umip/files/umip_test2.c b/meta-luv/recipes-core/umip/files/umip_test2.c index a8c3f927511..c9018c0acf4 100644 --- a/meta-luv/recipes-core/umip/files/umip_test2.c +++ b/meta-luv/recipes-core/umip/files/umip_test2.c @@ -197,13 +197,7 @@ #define INIT_LDTS INIT_VAL(15151515) static sig_atomic_t got_signal; -static int test_passed, test_failed, test_errors; - -static void print_results(void) -{ - printf("RESULTS: passed[%d], failed[%d], error[%d]\n", - test_passed, test_failed, test_errors); -} +int test_passed, test_failed, test_errors; static unsigned long get_mask(int op_size) { switch (op_size) { From e3abfc70a1225c0e690569e229b6bda1ff9af0d4 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Fri, 4 Aug 2017 14:57:28 -0700 Subject: [PATCH 056/109] umip: umip_test_defs: add a macro to initialize expected signals Depending on the configuration of the test suites and the underlying Linux kernel, different signals are expected, as per the following rules. If there is hardware support for UMIP, the default behavior is: + In 64-bit processes, UMIP-protected instructions are not emulated. In consequence, executing such instructions will invariably cause a #GP(0) (provided that no exeptions of higher priority happened. + In 32-bit processes, only SGDT, SIDT and SMSW are emulated. Hence, executing such instructions should not cause a #GP(0), the Linux kernel will provide emulation for them. The instructions STR and SLDT are not emulated and the #GP(0) exception will be observed. However, it is possible to patch the Linux kernel to provide emulation for all the instructions in both 32-bit and 64-bit processes. If this is the case no #GP(0) exception should be observed (other exceptions can still occur). In such a case, the test programs can be built by #define'ing EMULATE_ALL A couple of macros are provided to deal with the configurations described above. Signed-off-by: Ricardo Neri --- .../recipes-core/umip/files/umip_test_defs.h | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/meta-luv/recipes-core/umip/files/umip_test_defs.h b/meta-luv/recipes-core/umip/files/umip_test_defs.h index 64f2bbc0bf5..db3827cd4e1 100644 --- a/meta-luv/recipes-core/umip/files/umip_test_defs.h +++ b/meta-luv/recipes-core/umip/files/umip_test_defs.h @@ -57,6 +57,44 @@ #define EXPECTED_IDT_BASE 0xffff0000 #define EXPECTED_IDT_LIMIT 0x0 +/* + * EMULATE_ALL implies that all the UMIP-protected instructions are emulated. + * If not defined, the following rules apply: + * + UMIP-protected instructions are not emulated for 64-bit processes. This + * means that we always should get a SIGSEGV signal with code SI_KERNEL. + * + In 32-bit processes, only SGDT, SIDT and SMSW are emulated, STR and + * SLDT should cause a SIGSEGV signal with code SI_CODE. + */ +#ifdef EMULATE_ALL +#define INIT_EXPECTED_SIGNAL(signum, exp_signum, sigcode, exp_sigcode) \ + do{ \ + signum = exp_signum; \ + sigcode = exp_sigcode; \ + } while (0) +#else /* EMULATE_ALL */ +#ifdef __x86_64__ +#define INIT_EXPECTED_SIGNAL(signum, exp_signum, sigcode, exp_sigcode) \ + do{ \ + signum = SIGSEGV; \ + sigcode = SI_KERNEL; \ + } while(0) +#else /* __x86_64__ */ +#define INIT_EXPECTED_SIGNAL(signum, exp_signum, sigcode, exp_sigcode) \ + do{ \ + signum = exp_signum; \ + sigcode = exp_sigcode; \ + } while(0) +#endif /* __x86_64__ */ +#endif /* EMULATE_ALL */ + +#ifdef EMULATE_ALL +#define INIT_EXPECTED_SIGNAL_STR_SLDT(signum, exp_signum, sigcode, exp_sigcode) \ + INIT_EXPECTED_SIGNAL(signum, exp_signum, sigcode, exp_sigcode) +#else +#define INIT_EXPECTED_SIGNAL_STR_SLDT(signum, exp_signum, sigcode, exp_sigcode) \ + INIT_EXPECTED_SIGNAL(signum, SIGSEGV, sigcode, SI_KERNEL) +#endif + struct table_desc { unsigned short limit; unsigned long base; From 616a343d7d6524af25b2f7c91c29570dd4b2f9b3 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Fri, 4 Aug 2017 15:04:20 -0700 Subject: [PATCH 057/109] umip: umip_utils: add a utility function to inspect received signals Add a function to compare the received signals with an expected signal and signal code. This functions expectes the variables got_signal and got_sigcode to be within its scope. Signed-off-by: Ricardo Neri --- .../recipes-core/umip/files/umip_test_defs.h | 1 + meta-luv/recipes-core/umip/files/umip_utils.c | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/meta-luv/recipes-core/umip/files/umip_test_defs.h b/meta-luv/recipes-core/umip/files/umip_test_defs.h index db3827cd4e1..6fde05c4da2 100644 --- a/meta-luv/recipes-core/umip/files/umip_test_defs.h +++ b/meta-luv/recipes-core/umip/files/umip_test_defs.h @@ -115,5 +115,6 @@ static const struct table_desc expected_idt = { }; void print_results(void); +int inspect_signal(int exp_signum, int exp_sigcode); #endif /* _UMIP_TEST_DEFS_H */ diff --git a/meta-luv/recipes-core/umip/files/umip_utils.c b/meta-luv/recipes-core/umip/files/umip_utils.c index b406604e914..b4fec7f526e 100644 --- a/meta-luv/recipes-core/umip/files/umip_utils.c +++ b/meta-luv/recipes-core/umip/files/umip_utils.c @@ -4,11 +4,31 @@ * GPLv2 */ #include +#include +#include +#include "umip_test_defs.h" extern int test_passed, test_failed, test_errors; +extern sig_atomic_t got_signal, got_sigcode; void print_results(void) { printf("RESULTS: passed[%d], failed[%d], error[%d]\n", test_passed, test_failed, test_errors); } + +int inspect_signal(int exp_signum, int exp_sigcode) +{ + if (got_signal == exp_signum && got_sigcode == exp_sigcode) { + if (exp_signum && exp_sigcode) + pr_pass(test_passed, "Received expected signal.\n"); + return 0; + + } else { + if (got_signal && exp_sigcode) + pr_fail(test_failed, "Received unexpected signal.\n"); + else + pr_fail(test_failed, "Did not receive signal. A signal [%d] was expected.\n", exp_signum); + return 1; + } +} From a48cc242850b8dcc59503ebbfb4533f458eb9cd6 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Fri, 4 Aug 2017 15:06:17 -0700 Subject: [PATCH 058/109] umip: umip_test: inspect possible signals after running UMIP-protected instructions After running an UMIP-protected instruction, check if any signal was was sent. If this is the case, compare the signal number and signal code with expected values. Before running the test, initialize got_signal and got_sigcode. This implementation requires to remove the static qualifier to got_signal and got_sigcode. Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/umip_test.c | 65 +++++++++++++++++++- 1 file changed, 62 insertions(+), 3 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/umip_test.c b/meta-luv/recipes-core/umip/files/umip_test.c index a9309686b22..96b0c40b002 100644 --- a/meta-luv/recipes-core/umip/files/umip_test.c +++ b/meta-luv/recipes-core/umip/files/umip_test.c @@ -23,7 +23,8 @@ #endif int test_passed, test_failed, test_errors; -static sig_atomic_t got_signal; +sig_atomic_t got_signal; +sig_atomic_t got_sigcode; static void handler(int signum, siginfo_t *info, void *ctx_void) { @@ -56,13 +57,20 @@ static void call_sgdt() unsigned char val[GDTR_LEN]; unsigned long base = INIT_VAL(89898989); unsigned short limit = 0x3d3d; - int i; + int i, exp_signum, exp_sigcode; + + got_signal = 0; + got_sigcode = 0; + INIT_EXPECTED_SIGNAL(exp_signum, 0, exp_sigcode, 0); for (i = 0; i < GDTR_LEN; i++) val[i] = 0; pr_info("Will issue SGDT and save at [%p]\n", val); asm volatile("sgdt %0" : "=m" (val)); + if(inspect_signal(exp_signum, exp_sigcode)) + return; + limit = val[1] << 8 | val[0]; pr_info("GDT Limit [0x%04x]\n", limit); @@ -90,13 +98,20 @@ static void call_sidt() unsigned char val[IDTR_LEN]; unsigned long base = INIT_VAL(73737373); unsigned short limit = 0x9696; - int i; + int i, exp_signum, exp_sigcode; + + got_signal = 0; + got_sigcode = 0; + INIT_EXPECTED_SIGNAL(exp_signum, 0, exp_sigcode, 0); for (i = 0; i < IDTR_LEN; i++) val[i] = 0; pr_info("Will issue SIDT and save at [%p]\n", val); asm volatile("sidt %0" : "=m" (val)); + if(inspect_signal(exp_signum, exp_sigcode)) + return; + limit = val[1] << 8 | val[0]; pr_info("IDT Limit [0x%04x]\n", limit); @@ -125,10 +140,18 @@ static void call_sldt() unsigned long init_val = INIT_VAL(a1a1a1a1); /* if operand is memory, result is 16-bit */ unsigned short mask = 0xffff; + int exp_signum, exp_sigcode; + + got_signal = 0; + got_sigcode = 0; + INIT_EXPECTED_SIGNAL_STR_SLDT(exp_signum, 0, exp_sigcode, 0); pr_info("Will issue SLDT and save at [%p]\n", &val); asm volatile("sldt %0" : "=m" (val)); + if(inspect_signal(exp_signum, exp_sigcode)) + return; + pr_info("SS for LDT[0x%08lx]\n", val); /* @@ -148,10 +171,19 @@ static void call_smsw() unsigned long val = INIT_VAL(a2a2a2a2); unsigned long init_val = INIT_VAL(a2a2a2a2); unsigned short mask = 0xffff; + int exp_signum, exp_sigcode; + + got_signal = 0; + got_sigcode = 0; + INIT_EXPECTED_SIGNAL(exp_signum, 0, exp_sigcode, 0); + pr_info("Will issue SMSW and save at [%p]\n", &val); asm volatile("smsw %0" : "=m" (val)); + if(inspect_signal(exp_signum, exp_sigcode)) + return; + pr_info("CR0[0x%08lx]\n", val); /* @@ -172,11 +204,21 @@ static void call_str() unsigned int init_val32 = 0xa4a4a4a4; unsigned short val16 = 0xa5a5; unsigned short init_val16 = 0xa5a5; + int exp_signum, exp_sigcode; + + got_signal = 0; + got_sigcode = 0; + INIT_EXPECTED_SIGNAL_STR_SLDT(exp_signum, 0, exp_sigcode, 0); + #if __x86_64__ unsigned long val64 = 0xa3a3a3a3a3a3a3a3; pr_info("Will issue STR and save at m64[0x%p]\n", &val64); asm volatile("str %0" : "=m" (val64)); + + if(inspect_signal(exp_signum, exp_sigcode)) + goto test_m32; + pr_info("SS for TSS[0x%016lx]\n", val64); /* All 64 bits are written */ @@ -184,10 +226,19 @@ static void call_str() pr_pass(test_passed, "Obtained 64-bit expected value\n"); else pr_fail(test_failed, "Obtained 64-bit unexpected value\n"); + +test_m32: #endif + got_signal = 0; + got_sigcode = 0; + pr_info("Will issue STR and save at m32[0x%p]\n", &val32); asm volatile("str %0" : "=m" (val32)); + + if(inspect_signal(exp_signum, exp_sigcode)) + goto test_m16; + pr_info("SS for TSS[0x%08x]\n", val32); /* @@ -202,8 +253,16 @@ static void call_str() else pr_fail(test_failed, "Obtained 32-bit unexpected value\n"); +test_m16: + got_signal = 0; + got_sigcode = 0; + pr_info("Will issue STR and save at m16[0x%p]\n", &val16); asm volatile("str %0" : "=m" (val16)); + + if(inspect_signal(exp_signum, exp_sigcode)) + return; + pr_info("SS for TSS[0x%04x]\n", val16); /* From 389c2fc0769fa908865fc3757760345b0bc4971d Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Fri, 4 Aug 2017 15:32:45 -0700 Subject: [PATCH 059/109] umip: umip_test: do not exit upon receiving a signal Instead, let each test case inspect and handle the signal as per the requirements of specific test cases. Since we need to resume execution after the signal is received, we need to increment the instruction pointer. We are not sure of the size of the instruction size. We asume that the worst case scenario is that of having two prefix bytes, two opcode bytes, one ModRM byte, one SIB byte and 4 displacement bytes. A NOP sled is added after the instruction to ensure we safely resume execution in case we overestimate the size of the instruction. Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/umip_test.c | 30 ++++++++++++------- .../recipes-core/umip/files/umip_test_defs.h | 3 ++ 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/umip_test.c b/meta-luv/recipes-core/umip/files/umip_test.c index 96b0c40b002..900f0cba5ac 100644 --- a/meta-luv/recipes-core/umip/files/umip_test.c +++ b/meta-luv/recipes-core/umip/files/umip_test.c @@ -28,6 +28,8 @@ sig_atomic_t got_sigcode; static void handler(int signum, siginfo_t *info, void *ctx_void) { + ucontext_t *ctx = (ucontext_t *)ctx_void; + pr_info("si_signo[%d]\n", info->si_signo); pr_info("si_errno[%d]\n", info->si_errno); pr_info("si_code[%d]\n", info->si_code); @@ -43,13 +45,19 @@ static void handler(int signum, siginfo_t *info, void *ctx_void) else pr_info("Unknown si_code!\n"); + /* + * Move to the next instruction; to move, increment the instruction + * pointer by 10 bytes. 10 bytes is the size of the instruction + * considering two prefix bytes, two opcode bytes, one + * ModRM byte, one SIB byte and 4 displacement bytes. We have + * a NOP sled after the instruction to ensure we continue execution + * safely in case we overestimate the size of the instruction. + */ #ifdef __x86_64__ - pr_pass(test_passed, "I got a SIGSEGV. UMIP not emulated in 64-bit\n"); + ctx->uc_mcontext.gregs[REG_RIP] += 10; #else - pr_fail(test_failed, "FAIL: Whoa! I got a SIGSEGV. This is an error!\n"); + ctx->uc_mcontext.gregs[REG_EIP] += 10; #endif - print_results(); - exit(1); } static void call_sgdt() @@ -66,7 +74,7 @@ static void call_sgdt() for (i = 0; i < GDTR_LEN; i++) val[i] = 0; pr_info("Will issue SGDT and save at [%p]\n", val); - asm volatile("sgdt %0" : "=m" (val)); + asm volatile("sgdt %0\n" NOP_SLED : "=m" (val)); if(inspect_signal(exp_signum, exp_sigcode)) return; @@ -107,7 +115,7 @@ static void call_sidt() for (i = 0; i < IDTR_LEN; i++) val[i] = 0; pr_info("Will issue SIDT and save at [%p]\n", val); - asm volatile("sidt %0" : "=m" (val)); + asm volatile("sidt %0\n" NOP_SLED : "=m" (val)); if(inspect_signal(exp_signum, exp_sigcode)) return; @@ -147,7 +155,7 @@ static void call_sldt() INIT_EXPECTED_SIGNAL_STR_SLDT(exp_signum, 0, exp_sigcode, 0); pr_info("Will issue SLDT and save at [%p]\n", &val); - asm volatile("sldt %0" : "=m" (val)); + asm volatile("sldt %0\n" NOP_SLED : "=m" (val)); if(inspect_signal(exp_signum, exp_sigcode)) return; @@ -179,7 +187,7 @@ static void call_smsw() pr_info("Will issue SMSW and save at [%p]\n", &val); - asm volatile("smsw %0" : "=m" (val)); + asm volatile("smsw %0\n" NOP_SLED : "=m" (val)); if(inspect_signal(exp_signum, exp_sigcode)) return; @@ -214,7 +222,7 @@ static void call_str() unsigned long val64 = 0xa3a3a3a3a3a3a3a3; pr_info("Will issue STR and save at m64[0x%p]\n", &val64); - asm volatile("str %0" : "=m" (val64)); + asm volatile("str %0\n" NOP_SLED : "=m" (val64)); if(inspect_signal(exp_signum, exp_sigcode)) goto test_m32; @@ -234,7 +242,7 @@ static void call_str() got_sigcode = 0; pr_info("Will issue STR and save at m32[0x%p]\n", &val32); - asm volatile("str %0" : "=m" (val32)); + asm volatile("str %0\n" NOP_SLED : "=m" (val32)); if(inspect_signal(exp_signum, exp_sigcode)) goto test_m16; @@ -258,7 +266,7 @@ static void call_str() got_sigcode = 0; pr_info("Will issue STR and save at m16[0x%p]\n", &val16); - asm volatile("str %0" : "=m" (val16)); + asm volatile("str %0\n" NOP_SLED : "=m" (val16)); if(inspect_signal(exp_signum, exp_sigcode)) return; diff --git a/meta-luv/recipes-core/umip/files/umip_test_defs.h b/meta-luv/recipes-core/umip/files/umip_test_defs.h index 6fde05c4da2..e6c20f69474 100644 --- a/meta-luv/recipes-core/umip/files/umip_test_defs.h +++ b/meta-luv/recipes-core/umip/files/umip_test_defs.h @@ -95,6 +95,9 @@ INIT_EXPECTED_SIGNAL(signum, SIGSEGV, sigcode, SI_KERNEL) #endif +#define NOP_SLED "nop\n" "nop\n" "nop\n" "nop\n" "nop\n" "nop\n" "nop\n" \ + "nop\n" "nop\n" "nop\n" "nop\n" "nop\n" "nop\n" "nop\n" + struct table_desc { unsigned short limit; unsigned long base; From 070c13c463a4e9b5c72a39c0296fdf5fee24a9e0 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Fri, 4 Aug 2017 15:58:41 -0700 Subject: [PATCH 060/109] umip: umip_test2: allow each test case handle received signals Instead of exiting when receiving a signal, let each individual test case to handle the signal and compare it with expected values. When resuming execution we need to increment the instruction pointer to the next instructions. It is difficult to calculate the exact size of the instruction. Instead, we assume the largest value possible: two prefix bytes, two opcode bytes, one ModRM byte, one SIB byte, 4 displacement bytes. We append at the end a NOP sled to ensure we continue execution safely. Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/umip_test2.c | 68 +++++++++++++++---- 1 file changed, 53 insertions(+), 15 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/umip_test2.c b/meta-luv/recipes-core/umip/files/umip_test2.c index c9018c0acf4..f2582940180 100644 --- a/meta-luv/recipes-core/umip/files/umip_test2.c +++ b/meta-luv/recipes-core/umip/files/umip_test2.c @@ -1,3 +1,4 @@ +#define _GNU_SOURCE #include #include #include @@ -20,6 +21,7 @@ PUSH(aux) \ PUSH(reg) \ insn" %%"reg"\n" \ + NOP_SLED \ "mov %%"reg", %%"aux"\n" \ POP(reg) \ "mov %%"aux", %0\n" \ @@ -29,6 +31,7 @@ PUSH(aux) \ PUSH(reg) \ insn" %%e"reg"\n" \ + NOP_SLED \ "mov %%e"reg", %%e"aux"\n" \ POP(reg) \ "movl %%e"aux", %0\n" \ @@ -38,6 +41,7 @@ "push %%"aux"\n" \ "push %%"reg"\n" \ insn" %%"reg"\n" \ + NOP_SLED \ "mov %%"reg", %%"aux"\n" \ "pop %%"reg"\n" \ "mov %%"aux", %0\n" \ @@ -49,7 +53,14 @@ mask = get_mask(op_size); \ if (!mask) \ return -1; \ + \ + got_signal = 0; \ + got_sigcode = 0; \ + \ asm volatile(INSNreg##op_size(insn, reg, aux) : "=m" (val)); \ + \ + if(inspect_signal(exp_signum, exp_sigcode)) \ + break; \ /* \ * Check that the bits that are supposed to change does so \ * as well as that the bits that are not supposed to change \ @@ -116,6 +127,7 @@ "mov %%"reg", %%rax\n" /* make a backup of register under test */ \ "mov %%rsp, %%"reg"\n" /* move rsp to our register under test */ \ insn" "disp"(%%"reg")\n" /* execute our instruction */ \ + NOP_SLED \ "push "disp"(%%"reg")\n" /* save result to stack */ \ "mov %%rax, %%"reg"\n" /* restore register under test */ \ "pop %1\n" /* copy result to our variable */ \ @@ -128,6 +140,7 @@ "mov %%"reg", %%eax\n" /* make a backup of register under test */ \ "mov %%esp, %%"reg"\n" /* move esp to our register under test */ \ insn" "disp"(%%"reg")\n" /* execute our instruction */ \ + NOP_SLED \ "push "disp"(%%"reg")\n" /* save result to stack */ \ "mov %%eax, %%"reg"\n" /* restore register under test */ \ "pop %1\n" /* copy result to our variable */ \ @@ -148,18 +161,25 @@ mask = get_mask(16); \ if (!mask) \ return -1; \ + \ + got_signal = 0; \ + got_sigcode = 0; \ + \ asm volatile(INSNmacro(insn, reg) : "=m" (val): "m"(val) : "%rax"); \ - /* \ - * Check that the bits that are supposed to change does so \ - * as well as that the bits that are not supposed to change \ - * does not change. \ - */ \ - if (((val & mask) == (exp & mask)) && ((exp & ~mask) == (exp & ~mask))) \ - pr_pass(test_passed, "On '%s %s(%s)'! Got [0x%016lx] Exp[0x%016lx]\n", \ - insn, #INSNmacro, reg, val, (exp & mask) | (init & ~mask)); \ - else { \ - pr_fail(test_failed, "On '%s %s(%s)'! Got[0x%016lx] Exp[0x%016lx]\n", \ - insn, #INSNmacro, reg, val, (exp & mask) | (init & ~mask)); \ + \ + if(!inspect_signal(exp_signum, exp_sigcode)) { \ + /* \ + * Check that the bits that are supposed to change does so \ + * as well as that the bits that are not supposed to change \ + * does not change. \ + */ \ + if (((val & mask) == (exp & mask)) && ((exp & ~mask) == (exp & ~mask))) \ + pr_pass(test_passed, "On '%s %s(%s)'! Got [0x%016lx] Exp[0x%016lx]\n", \ + insn, #INSNmacro, reg, val, (exp & mask) | (init & ~mask)); \ + else { \ + pr_fail(test_failed, "On '%s %s(%s)'! Got[0x%016lx] Exp[0x%016lx]\n", \ + insn, #INSNmacro, reg, val, (exp & mask) | (init & ~mask)); \ + }\ } #define CHECK_INSNmem(insn, reg, val, init, exp) \ @@ -196,7 +216,7 @@ #define INIT_MSW INIT_VAL(14141414) #define INIT_LDTS INIT_VAL(15151515) -static sig_atomic_t got_signal; +sig_atomic_t got_signal, got_sigcode; int test_passed, test_failed, test_errors; static unsigned long get_mask(int op_size) { @@ -223,6 +243,9 @@ static int test_str(void) { unsigned long val; unsigned long mask = 0xffff; + int exp_signum, exp_sigcode; + + INIT_EXPECTED_SIGNAL_STR_SLDT(exp_signum, 0, exp_sigcode, 0); pr_info("====Checking STR. Expected value: [0x%x]====\n", expected_tr); pr_info("==Tests for register operands==\n"); @@ -240,6 +263,9 @@ static int test_smsw(void) { unsigned long val; unsigned long mask = 0xffff; + int exp_signum, exp_sigcode; + + INIT_EXPECTED_SIGNAL(exp_signum, 0, exp_sigcode, 0); pr_info("====Checking SMSW. Expected value: [0x%x]====\n", expected_msw); pr_info("==Tests for register operands==\n"); @@ -256,6 +282,9 @@ static int test_sldt(void) { unsigned long val; unsigned long mask = 0xffff; + int exp_signum, exp_sigcode; + + INIT_EXPECTED_SIGNAL(exp_signum, 0, exp_sigcode, 0); pr_info("====Checking SLDT. Expected value: [0x%x]====\n", expected_ldt); pr_info("==Tests for register operands==\n"); @@ -270,6 +299,8 @@ static int test_sldt(void) static void handler(int signum, siginfo_t *info, void *ctx_void) { + ucontext_t *ctx = (ucontext_t *)ctx_void; + pr_info("si_signo[%d]\n", info->si_signo); pr_info("si_errno[%d]\n", info->si_errno); pr_info("si_code[%d]\n", info->si_code); @@ -285,12 +316,19 @@ static void handler(int signum, siginfo_t *info, void *ctx_void) else pr_info("Unknown si_code!\n"); + /* + * Move to the next instruction; to move, increment the instruction + * pointer by 10 bytes. 10 bytes is the size of the instruction + * considering two prefix bytes, two opcode bytes, one + * ModRM byte, one SIB byte and 4 displacement bytes. We have + * a NOP sled after the instruction to ensure we continue execution + * safely in case we overestimate the size of the instruction. + */ #ifdef __x86_64__ - pr_pass(test_passed, "I got a SIGSEGV. UMIP not emulated in 64-bit\n"); + ctx->uc_mcontext.gregs[REG_RIP] += 10; #else - pr_fail(test_failed, "FAIL: Whoa! I got a SIGSEGV. This is an error!\n"); + ctx->uc_mcontext.gregs[REG_EIP] += 10; #endif - exit(1); } int main (void) From 18e27cf5f6c683b835e84160ed91abdbed2c4cac Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Fri, 4 Aug 2017 17:34:25 -0700 Subject: [PATCH 061/109] umip: umip_exceptions: use the NOP_SLED macro Rather than putting a lot of asm nops in many places. Use the the macro definition that we already have. Signed-off-by: Ricardo Neri --- .../recipes-core/umip/files/umip_exceptions.c | 36 +++---------------- 1 file changed, 5 insertions(+), 31 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/umip_exceptions.c b/meta-luv/recipes-core/umip/files/umip_exceptions.c index ef4a9c0d26d..ace527ed2bb 100644 --- a/meta-luv/recipes-core/umip/files/umip_exceptions.c +++ b/meta-luv/recipes-core/umip/files/umip_exceptions.c @@ -95,16 +95,7 @@ static void __test_maperr_pf_##inst(void) \ signal_code = 0; \ \ pr_info("Test page fault because unmapped memory for %s with addr %p\n", #inst, val_bad);\ - asm volatile (#inst" %0\n" \ - "nop\n" \ - "nop\n" \ - "nop\n" \ - "nop\n" \ - "nop\n" \ - "nop\n" \ - "nop\n" \ - "nop\n" \ - "nop\n": "=m"(*val_bad)); \ + asm volatile (#inst" %0\n" NOP_SLED : "=m"(*val_bad)); \ \ if (!got_signal) { \ pr_fail(test_failed, "Signal not received!\n"); \ @@ -133,11 +124,7 @@ static void __test_lock_prefix_##name(void) \ { \ pr_info("Test %s with lock prefix\n", #name); \ /* name (%eax) with the LOCK prefix */ \ - asm volatile(inst \ - "nop\n" \ - "nop\n" \ - "nop\n" \ - "nop\n"); \ + asm volatile(inst NOP_SLED); \ \ if (signal_code != ILL_ILLOPN) { \ pr_fail(test_failed, "Signal code is not what we expect.\n");\ @@ -170,12 +157,7 @@ static void __test_register_operand_##name(void) \ { \ pr_info("Test %s with register operand\n", #name); \ /* name (%eax) with the LOCK prefix */ \ - asm volatile(inst \ - "nop\n" \ - "nop\n" \ - "nop\n" \ - "nop\n" \ - "nop\n"); \ + asm volatile(inst NOP_SLED); \ \ if (signal_code != ILL_ILLOPN) { \ pr_fail(test_failed, "Signal code is not what we expect.\n");\ @@ -213,11 +195,7 @@ static void __test_null_segment_selector_##inst##_##reg(void) \ "mov $0, %ebx\n" \ "mov %bx, %" #reg "\n" \ "smsw %" #reg ":(%eax)\n" \ - "nop\n" \ - "nop\n" \ - "nop\n" \ - "nop\n" \ - "nop\n" \ + NOP_SLED \ "pop %ebx\n" \ "pop %eax\n" \ "pop %" #reg "\n"); \ @@ -349,11 +327,7 @@ static void __test_addresses_outside_segment_##inst##_##sel(void) \ "mov $0x2000, %%eax\n" \ "mov %0, %%" #sel "\n" \ #inst " %%" #sel ":(%%eax)\n" \ - "nop\n" \ - "nop\n" \ - "nop\n" \ - "nop\n" \ - "nop\n" \ + NOP_SLED \ "pop %%ebx\n" \ "pop %%eax\n" \ "pop %%" #sel "\n" \ From 6c29b70d22272eb11807b70e2255a5f6ee683087 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Fri, 4 Aug 2017 18:26:04 -0700 Subject: [PATCH 062/109] umip: umip_exceptions: use existing signal inspection functions Remove local macros and code to inspect the received signal number and signal code. Instead, use the utility function inspect_signal. Signed-off-by: Ricardo Neri --- .../recipes-core/umip/files/umip_exceptions.c | 88 +++++-------------- 1 file changed, 21 insertions(+), 67 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/umip_exceptions.c b/meta-luv/recipes-core/umip/files/umip_exceptions.c index ace527ed2bb..9fc7c6ba8a1 100644 --- a/meta-luv/recipes-core/umip/files/umip_exceptions.c +++ b/meta-luv/recipes-core/umip/files/umip_exceptions.c @@ -17,8 +17,7 @@ #include #include "umip_test_defs.h" -static sig_atomic_t signal_code; -static sig_atomic_t got_signal; +sig_atomic_t got_signal, got_sigcode; int test_passed, test_failed, test_errors; @@ -53,7 +52,7 @@ static void handler(int signum, siginfo_t *info, void *ctx_void) errx(1, "ERROR: Received unexpected signal"); /* Save the signal code */ - signal_code = info->si_code; + got_sigcode = info->si_code; /* * Move to the next instruction. We have a cushion of * several NOPs. Thus, we can safely move 8 positions @@ -65,43 +64,15 @@ static void handler(int signum, siginfo_t *info, void *ctx_void) #endif } -#ifdef __x86_64__ -#define inspect_signal(sigcode) \ - do { \ - if (sigcode != SI_KERNEL) { \ - pr_fail(test_failed, "Signal code is not what we expect.\n");\ - return; \ - } \ - pr_pass(test_passed, "A SEGV_MAPERR page fault was issued.\n"); \ - return; \ - } while (0) -#else -#define inspect_signal(sigcode) \ - do { \ - if (sigcode != SEGV_MAPERR) { \ - pr_fail(test_failed, "Signal code is not what we expect.\n");\ - return; \ - } \ - pr_pass(test_passed, "A SEGV_MAPERR page fault was issued.\n"); \ - return; \ - } while (0) -#endif - #define gen_test_maperr_pf_inst(inst, bad_addr) \ static void __test_maperr_pf_##inst(void) \ { \ unsigned long *val_bad = (unsigned long *)bad_addr; \ \ - signal_code = 0; \ - \ pr_info("Test page fault because unmapped memory for %s with addr %p\n", #inst, val_bad);\ asm volatile (#inst" %0\n" NOP_SLED : "=m"(*val_bad)); \ \ - if (!got_signal) { \ - pr_fail(test_failed, "Signal not received!\n"); \ - return; \ - } \ - inspect_signal(signal_code); \ + inspect_signal(SIGSEGV, SEGV_MAPERR); \ } gen_test_maperr_pf_inst(smsw, 0x100000) @@ -122,19 +93,14 @@ static void test_maperr_pf(void) #define gen_test_lock_prefix_inst(name, inst) \ static void __test_lock_prefix_##name(void) \ { \ + got_signal = 0; \ + got_sigcode = 0; \ + \ pr_info("Test %s with lock prefix\n", #name); \ /* name (%eax) with the LOCK prefix */ \ - asm volatile(inst NOP_SLED); \ + asm volatile(inst NOP_SLED); \ \ - if (signal_code != ILL_ILLOPN) { \ - pr_fail(test_failed, "Signal code is not what we expect.\n");\ - signal_code = 0; \ - return; \ - } \ - pr_pass(test_passed, "An ILL_ILLOPN exception was issued.\n"); \ - signal_code = 0; \ - \ - return; \ + inspect_signal(SIGILL, ILL_ILLOPN); \ } gen_test_lock_prefix_inst(SMSW, ".byte 0xf0, 0xf, 0x1, 0x20\n") @@ -155,18 +121,14 @@ static void test_lock_prefix(void) #define gen_test_register_operand_inst(name, inst) \ static void __test_register_operand_##name(void) \ { \ + got_signal = 0; \ + got_sigcode = 0; \ + \ pr_info("Test %s with register operand\n", #name); \ /* name (%eax) with the LOCK prefix */ \ asm volatile(inst NOP_SLED); \ \ - if (signal_code != ILL_ILLOPN) { \ - pr_fail(test_failed, "Signal code is not what we expect.\n");\ - signal_code = 0; \ - return; \ - } \ - pr_pass(test_passed, "An ILL_ILLOPN exception was issued.\n"); \ - signal_code = 0; \ - \ + inspect_signal(SIGILL, ILL_ILLOPN); \ return; \ } @@ -175,8 +137,6 @@ gen_test_register_operand_inst(SGDT, ".byte 0xf, 0x1, 0xc0\n") static void test_register_operand(void) { - signal_code = 0; - __test_register_operand_SGDT(); __test_register_operand_SIDT(); } @@ -187,6 +147,9 @@ static void test_null_segment_selectors(void) {} #define gen_test_null_segment_selector(inst, reg) \ static void __test_null_segment_selector_##inst##_##reg(void) \ { \ + got_signal = 0; \ + got_sigcode = 0; \ + \ pr_info("Test using null seg sel for " #inst " with " #reg "\n"); \ asm volatile("push %" #reg "\n" \ "push %eax\n" \ @@ -199,14 +162,8 @@ static void __test_null_segment_selector_##inst##_##reg(void) \ "pop %ebx\n" \ "pop %eax\n" \ "pop %" #reg "\n"); \ - if (signal_code != SI_KERNEL) { \ - pr_fail(test_failed, "Signal code is not what we expect.\n"); \ - signal_code = 0; \ - return; \ - } \ - pr_pass(test_passed, "An ILL_ILLOPN exception was issued.\n"); \ - signal_code = 0; \ \ + inspect_signal(SIGSEGV, SI_KERNEL); \ return; \ } @@ -318,6 +275,10 @@ static void __test_addresses_outside_segment_##inst##_##sel(void) \ int ret; \ unsigned short seg_sel; \ \ + got_signal = 0; \ + got_sigcode = 0; \ + \ + \ seg_sel = SEGMENT_SELECTOR(DATA_DESC_INDEX); \ \ pr_info("Test address outside of segment limit for " #inst " with " #sel"\n");\ @@ -334,14 +295,7 @@ static void __test_addresses_outside_segment_##inst##_##sel(void) \ : \ :"m" (seg_sel)); \ \ - if (signal_code != SI_KERNEL) { \ - pr_fail(test_failed, "Signal code is not what we expect.\n"); \ - signal_code = 0; \ - return; \ - } \ - pr_pass(test_passed, "An ILL_ILLOPN exception was issued.\n"); \ - signal_code = 0; \ - \ + inspect_signal(SIGSEGV, SI_KERNEL); \ return; \ } From 3f290be476cd082dba6cac334e62f2af743b3078 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Fri, 4 Aug 2017 18:27:58 -0700 Subject: [PATCH 063/109] umip: umip_exceptions: correct increment of instruction pointer When handling a signal, the instruction pointer needs to be incremented to resume execution. We assume an increment of 10 bytes. This considers two prefix bytes, two prefix opcodes, one ModRM byte, one SIB byte and 4 displacement bytes. Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/umip_exceptions.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/umip_exceptions.c b/meta-luv/recipes-core/umip/files/umip_exceptions.c index 9fc7c6ba8a1..091e9227fde 100644 --- a/meta-luv/recipes-core/umip/files/umip_exceptions.c +++ b/meta-luv/recipes-core/umip/files/umip_exceptions.c @@ -53,14 +53,19 @@ static void handler(int signum, siginfo_t *info, void *ctx_void) /* Save the signal code */ got_sigcode = info->si_code; + /* - * Move to the next instruction. We have a cushion of - * several NOPs. Thus, we can safely move 8 positions + * Move to the next instruction; to move, increment the instruction + * pointer by 10 bytes. 10 bytes is the size of the instruction + * considering two prefix bytes, two opcode bytes, one + * ModRM byte, one SIB byte and 4 displacement bytes. We have + * a NOP sled after the instruction to ensure we continue execution + * safely in case we overestimate the size of the instruction. */ #ifdef __x86_64__ - ctx->uc_mcontext.gregs[REG_RIP] += 8; + ctx->uc_mcontext.gregs[REG_RIP] += 10; #else - ctx->uc_mcontext.gregs[REG_EIP] += 4; + ctx->uc_mcontext.gregs[REG_EIP] += 10; #endif } From b0f34d5b056b03ff2114906dcf9f7da4e37b44ff Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Mon, 7 Aug 2017 11:53:25 -0700 Subject: [PATCH 064/109] umip: unify signal-handling code All of our signal handler do essentially the same thing. Hence, it can be put in a utility function. Some programs, however, need to exit upon receiving a signal, as it means an error condition arose. Thus, add a new global variable exit_on_signal for such cases. Move all relevant code to our utility library. Signed-off-by: Ricardo Neri --- .../recipes-core/umip/files/umip_exceptions.c | 53 +---------------- .../recipes-core/umip/files/umip_ldt_16.c | 31 ++-------- .../recipes-core/umip/files/umip_ldt_32.c | 32 ++--------- .../recipes-core/umip/files/umip_ldt_64.c | 31 ++-------- meta-luv/recipes-core/umip/files/umip_test.c | 42 +------------- meta-luv/recipes-core/umip/files/umip_test2.c | 41 +------------ .../recipes-core/umip/files/umip_test_defs.h | 2 + meta-luv/recipes-core/umip/files/umip_utils.c | 57 ++++++++++++++++++- 8 files changed, 76 insertions(+), 213 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/umip_exceptions.c b/meta-luv/recipes-core/umip/files/umip_exceptions.c index 091e9227fde..ffc9cd4321d 100644 --- a/meta-luv/recipes-core/umip/files/umip_exceptions.c +++ b/meta-luv/recipes-core/umip/files/umip_exceptions.c @@ -6,7 +6,6 @@ * * GPL v2. */ -#define _GNU_SOURCE #include #include #include @@ -17,58 +16,10 @@ #include #include "umip_test_defs.h" -sig_atomic_t got_signal, got_sigcode; +extern sig_atomic_t got_signal, got_sigcode; int test_passed, test_failed, test_errors; -static void handler(int signum, siginfo_t *info, void *ctx_void) -{ - ucontext_t *ctx = (ucontext_t *)ctx_void; - - pr_info("si_signo[%d]\n", info->si_signo); - pr_info("si_errno[%d]\n", info->si_errno); - pr_info("si_code[%d]\n", info->si_code); - pr_info("si_addr[0x%p]\n", info->si_addr); - - got_signal = signum; - - if (signum == SIGSEGV) { - if (info->si_code == SEGV_MAPERR) - pr_info("Signal because of unmapped object.\n"); - else if (info->si_code == SI_KERNEL) - pr_info("Signal because of #GP\n"); - else - pr_info("Unknown si_code!\n"); - } - else if (signum == SIGILL) { - if (info->si_code == SEGV_MAPERR) - pr_info("Signal because of unmapped object.\n"); - else if (info->si_code == ILL_ILLOPN) - pr_info("Signal because of #UD\n"); - else - pr_info("Unknown si_code!\n"); - } - else - errx(1, "ERROR: Received unexpected signal"); - - /* Save the signal code */ - got_sigcode = info->si_code; - - /* - * Move to the next instruction; to move, increment the instruction - * pointer by 10 bytes. 10 bytes is the size of the instruction - * considering two prefix bytes, two opcode bytes, one - * ModRM byte, one SIB byte and 4 displacement bytes. We have - * a NOP sled after the instruction to ensure we continue execution - * safely in case we overestimate the size of the instruction. - */ -#ifdef __x86_64__ - ctx->uc_mcontext.gregs[REG_RIP] += 10; -#else - ctx->uc_mcontext.gregs[REG_EIP] += 10; -#endif -} - #define gen_test_maperr_pf_inst(inst, bad_addr) \ static void __test_maperr_pf_##inst(void) \ { \ @@ -365,7 +316,7 @@ int main (void) PRINT_BITNESS; memset(&action, 0, sizeof(action)); - action.sa_sigaction = &handler; + action.sa_sigaction = &signal_handler; action.sa_flags = SA_SIGINFO; sigemptyset(&action.sa_mask); diff --git a/meta-luv/recipes-core/umip/files/umip_ldt_16.c b/meta-luv/recipes-core/umip/files/umip_ldt_16.c index 2abd5e83e76..0b397e2e1d5 100644 --- a/meta-luv/recipes-core/umip/files/umip_ldt_16.c +++ b/meta-luv/recipes-core/umip/files/umip_ldt_16.c @@ -8,8 +8,6 @@ #include #include #include -#include -#include #include "umip_test_defs.h" #include "test_umip_ldt_16.h" #include "test_umip_code_16.h" @@ -17,6 +15,7 @@ extern unsigned char test_umip[], test_umip_end[]; extern unsigned char interim[], interim_start[], interim_end[]; extern unsigned char finish_testing[]; +extern int exit_on_signal; unsigned short cs_orig; #define CODE_DESC_INDEX 1 @@ -32,32 +31,8 @@ unsigned short cs_orig; #define TI_LDT 1 #define SEGMENT_SELECTOR(index) (RPL3 | (TI_LDT << 2) | (index << 3)) -static sig_atomic_t got_signal; - int test_passed, test_failed, test_errors; -void handler(int signum, siginfo_t *info, void *ctx_void) -{ - pr_info("si_signo[%d]\n", info->si_signo); - pr_info("si_errno[%d]\n", info->si_errno); - pr_info("si_code[%d]\n", info->si_code); - pr_info("si_addr[0x%p]\n", info->si_addr); - if (signum != SIGSEGV) - pr_error(test_errors, "Received unexpected signal"); - else - got_signal = signum; - if (info->si_code == SEGV_MAPERR) - pr_info("Signal because of unmapped object.\n"); - else if (info->si_code == SI_KERNEL) - pr_info("Signal because of #GP\n"); - else - pr_info("Unknown si_code!\n"); - - pr_fail(test_failed, "Whoa! I got a SIGSEGV! Something went wrong!\n"); - print_results(); - exit(1); -} - asm(".pushsection .rodata\n\t" "interim:\n\t" /* this is the return point */ @@ -203,10 +178,12 @@ int run_umip_ldt_test(void) PRINT_BITNESS; memset(&action, 0, sizeof(action)); - action.sa_sigaction = &handler; + action.sa_sigaction = &signal_handler; action.sa_flags = SA_SIGINFO; sigemptyset(&action.sa_mask); + exit_on_signal = 1; + if (sigaction(SIGSEGV, &action, NULL) < 0) { pr_error(test_errors, "Could not set the signal handler!"); goto err_out; diff --git a/meta-luv/recipes-core/umip/files/umip_ldt_32.c b/meta-luv/recipes-core/umip/files/umip_ldt_32.c index 83c4aced563..87e93b2823d 100644 --- a/meta-luv/recipes-core/umip/files/umip_ldt_32.c +++ b/meta-luv/recipes-core/umip/files/umip_ldt_32.c @@ -1,7 +1,6 @@ /* Test cases for UMIP */ /* Copyright Intel Corporation 2017 */ -#define _GNU_SOURCE #include #include #include @@ -10,14 +9,13 @@ #include #include #include -#include -#include #include "umip_test_defs.h" #include "test_umip_ldt_32.h" #include "test_umip_code_32.h" extern unsigned char test_umip[], test_umip_end[]; extern unsigned char finish_testing[]; +extern int exit_on_signal; unsigned short cs_orig; #define CODE_DESC_INDEX 1 @@ -31,32 +29,8 @@ unsigned short cs_orig; #define TI_LDT 1 #define SEGMENT_SELECTOR(index) (RPL3 | (TI_LDT << 2) | (index << 3)) -static sig_atomic_t got_signal; - extern int test_passed, test_failed, test_errors; -void handler(int signum, siginfo_t *info, void *ctx_void) -{ - pr_info("si_signo[%d]\n", info->si_signo); - pr_info("si_errno[%d]\n", info->si_errno); - pr_info("si_code[%d]\n", info->si_code); - pr_info("si_addr[0x%p]\n", info->si_addr); - if (signum != SIGSEGV) - pr_error(test_errors, "Received unexpected signal"); - else - got_signal = signum; - if (info->si_code == SEGV_MAPERR) - pr_info("Signal because of unmapped object.\n"); - else if (info->si_code == SI_KERNEL) - pr_info("Signal because of #GP\n"); - else - pr_info("Unknown si_code!\n"); - - pr_fail(test_failed, "Whoa! I got a SIGSEGV! Something went wrong!\n"); - print_results(); - exit(1); -} - static int setup_data_segments() { int ret; @@ -151,10 +125,12 @@ int run_umip_ldt_test(void) PRINT_BITNESS; memset(&action, 0, sizeof(action)); - action.sa_sigaction = &handler; + action.sa_sigaction = &signal_handler; action.sa_flags = SA_SIGINFO; sigemptyset(&action.sa_mask); + exit_on_signal = 1; + if (sigaction(SIGSEGV, &action, NULL) < 0) { pr_error(test_errors, "Could not set the signal handler!"); goto err_out; diff --git a/meta-luv/recipes-core/umip/files/umip_ldt_64.c b/meta-luv/recipes-core/umip/files/umip_ldt_64.c index cdbb42e5457..69027b0dd64 100644 --- a/meta-luv/recipes-core/umip/files/umip_ldt_64.c +++ b/meta-luv/recipes-core/umip/files/umip_ldt_64.c @@ -10,14 +10,13 @@ #include #include #include -#include -#include #include "umip_test_defs.h" #include "test_umip_ldt_64.h" #include "test_umip_code_64.h" extern unsigned char test_umip[], test_umip_end[]; extern unsigned char finish_testing[]; +extern int exit_on_signal; unsigned long old_fsbase, old_gsbase; unsigned short old_fs, old_gs; @@ -29,32 +28,8 @@ unsigned short old_fs, old_gs; #define TI_LDT 1 #define SEGMENT_SELECTOR(index) (RPL3 | (TI_LDT << 2) | (index << 3)) -static sig_atomic_t got_signal; - extern int test_passed, test_failed, test_errors; -void handler(int signum, siginfo_t *info, void *ctx_void) -{ - pr_info("si_signo[%d]\n", info->si_signo); - pr_info("si_errno[%d]\n", info->si_errno); - pr_info("si_code[%d]\n", info->si_code); - pr_info("si_addr[0x%p]\n", info->si_addr); - if (signum != SIGSEGV) - pr_error(test_errors, "Received unexpected signal"); - else - got_signal = signum; - if (info->si_code == SEGV_MAPERR) - pr_info("Signal because of unmapped object.\n"); - else if (info->si_code == SI_KERNEL) - pr_info("Signal because of #GP\n"); - else - pr_info("Unknown si_code!\n"); - - pr_fail(test_failed, "Whoa! I got a SIGSEGV! Something went wrong!\n"); - print_results(); - exit(1); -} - static int setup_data_segments() { int ret; @@ -105,10 +80,12 @@ int main(void) PRINT_BITNESS; memset(&action, 0, sizeof(action)); - action.sa_sigaction = &handler; + action.sa_sigaction = &signal_handler; action.sa_flags = SA_SIGINFO; sigemptyset(&action.sa_mask); + exit_on_signal = 1; + if (sigaction(SIGSEGV, &action, NULL) < 0) { pr_error(test_errors, "Could not set the signal handler!"); goto err_out; diff --git a/meta-luv/recipes-core/umip/files/umip_test.c b/meta-luv/recipes-core/umip/files/umip_test.c index 900f0cba5ac..80998f7ba33 100644 --- a/meta-luv/recipes-core/umip/files/umip_test.c +++ b/meta-luv/recipes-core/umip/files/umip_test.c @@ -4,11 +4,8 @@ * GPLv2 */ -#define _GNU_SOURCE #include -#include #include -#include #include #include #include @@ -23,42 +20,7 @@ #endif int test_passed, test_failed, test_errors; -sig_atomic_t got_signal; -sig_atomic_t got_sigcode; - -static void handler(int signum, siginfo_t *info, void *ctx_void) -{ - ucontext_t *ctx = (ucontext_t *)ctx_void; - - pr_info("si_signo[%d]\n", info->si_signo); - pr_info("si_errno[%d]\n", info->si_errno); - pr_info("si_code[%d]\n", info->si_code); - pr_info("si_addr[0x%p]\n", info->si_addr); - if (signum != SIGSEGV) - errx(1, "ERROR: Received unexpected signal"); - else - got_signal = signum; - if (info->si_code == SEGV_MAPERR) - pr_info("Signal because of unmapped object.\n"); - else if (info->si_code == SI_KERNEL) - pr_info("Signal because of #GP\n"); - else - pr_info("Unknown si_code!\n"); - - /* - * Move to the next instruction; to move, increment the instruction - * pointer by 10 bytes. 10 bytes is the size of the instruction - * considering two prefix bytes, two opcode bytes, one - * ModRM byte, one SIB byte and 4 displacement bytes. We have - * a NOP sled after the instruction to ensure we continue execution - * safely in case we overestimate the size of the instruction. - */ -#ifdef __x86_64__ - ctx->uc_mcontext.gregs[REG_RIP] += 10; -#else - ctx->uc_mcontext.gregs[REG_EIP] += 10; -#endif -} +extern sig_atomic_t got_signal, got_sigcode; static void call_sgdt() { @@ -294,7 +256,7 @@ int main(void) PRINT_BITNESS; memset(&action, 0, sizeof(action)); - action.sa_sigaction = &handler; + action.sa_sigaction = &signal_handler; action.sa_flags = SA_SIGINFO; sigemptyset(&action.sa_mask); diff --git a/meta-luv/recipes-core/umip/files/umip_test2.c b/meta-luv/recipes-core/umip/files/umip_test2.c index f2582940180..0888a18d15b 100644 --- a/meta-luv/recipes-core/umip/files/umip_test2.c +++ b/meta-luv/recipes-core/umip/files/umip_test2.c @@ -1,8 +1,5 @@ -#define _GNU_SOURCE #include #include -#include -#include #include #include #include "umip_test_defs.h" @@ -216,7 +213,7 @@ #define INIT_MSW INIT_VAL(14141414) #define INIT_LDTS INIT_VAL(15151515) -sig_atomic_t got_signal, got_sigcode; +extern sig_atomic_t got_signal, got_sigcode; int test_passed, test_failed, test_errors; static unsigned long get_mask(int op_size) { @@ -297,40 +294,6 @@ static int test_sldt(void) return 0; } -static void handler(int signum, siginfo_t *info, void *ctx_void) -{ - ucontext_t *ctx = (ucontext_t *)ctx_void; - - pr_info("si_signo[%d]\n", info->si_signo); - pr_info("si_errno[%d]\n", info->si_errno); - pr_info("si_code[%d]\n", info->si_code); - pr_info("si_addr[0x%p]\n", info->si_addr); - if (signum != SIGSEGV) - pr_error(test_errors, "Received unexpected signal"); - else - got_signal = signum; - if (info->si_code == SEGV_MAPERR) - pr_info("Signal because of unmapped object.\n"); - else if (info->si_code == SI_KERNEL) - pr_info("Signal because of #GP\n"); - else - pr_info("Unknown si_code!\n"); - - /* - * Move to the next instruction; to move, increment the instruction - * pointer by 10 bytes. 10 bytes is the size of the instruction - * considering two prefix bytes, two opcode bytes, one - * ModRM byte, one SIB byte and 4 displacement bytes. We have - * a NOP sled after the instruction to ensure we continue execution - * safely in case we overestimate the size of the instruction. - */ -#ifdef __x86_64__ - ctx->uc_mcontext.gregs[REG_RIP] += 10; -#else - ctx->uc_mcontext.gregs[REG_EIP] += 10; -#endif -} - int main (void) { int ret_str, ret_smsw, ret_sldt; @@ -339,7 +302,7 @@ int main (void) PRINT_BITNESS; memset(&action, 0, sizeof(action)); - action.sa_sigaction = &handler; + action.sa_sigaction = &signal_handler; action.sa_flags = SA_SIGINFO; sigemptyset(&action.sa_mask); diff --git a/meta-luv/recipes-core/umip/files/umip_test_defs.h b/meta-luv/recipes-core/umip/files/umip_test_defs.h index e6c20f69474..774f4b1a305 100644 --- a/meta-luv/recipes-core/umip/files/umip_test_defs.h +++ b/meta-luv/recipes-core/umip/files/umip_test_defs.h @@ -1,6 +1,7 @@ #ifndef _UMIP_TEST_DEFS_H #define _UMIP_TEST_DEFS_H #include +#include #define TEST_PASS "\x1b[32m[pass]\x1b[0m " #define TEST_FAIL "\x1b[31m[FAIL]\x1b[0m " @@ -119,5 +120,6 @@ static const struct table_desc expected_idt = { void print_results(void); int inspect_signal(int exp_signum, int exp_sigcode); +void signal_handler(int signum, siginfo_t *info, void *ctx_void); #endif /* _UMIP_TEST_DEFS_H */ diff --git a/meta-luv/recipes-core/umip/files/umip_utils.c b/meta-luv/recipes-core/umip/files/umip_utils.c index b4fec7f526e..930ede038c8 100644 --- a/meta-luv/recipes-core/umip/files/umip_utils.c +++ b/meta-luv/recipes-core/umip/files/umip_utils.c @@ -3,13 +3,15 @@ * * GPLv2 */ +#define _GNU_SOURCE #include #include #include #include "umip_test_defs.h" extern int test_passed, test_failed, test_errors; -extern sig_atomic_t got_signal, got_sigcode; +sig_atomic_t got_signal, got_sigcode; +int exit_on_signal; void print_results(void) { @@ -32,3 +34,56 @@ int inspect_signal(int exp_signum, int exp_sigcode) return 1; } } + +void signal_handler(int signum, siginfo_t *info, void *ctx_void) +{ + ucontext_t *ctx = (ucontext_t *)ctx_void; + + pr_info("si_signo[%d]\n", info->si_signo); + pr_info("si_errno[%d]\n", info->si_errno); + pr_info("si_code[%d]\n", info->si_code); + pr_info("si_addr[0x%p]\n", info->si_addr); + + got_signal = signum; + + if (signum == SIGSEGV) { + if (info->si_code == SEGV_MAPERR) + pr_info("Signal because of unmapped object.\n"); + else if (info->si_code == SI_KERNEL) + pr_info("Signal because of #GP\n"); + else + pr_info("Unknown si_code!\n"); + } else if (signum == SIGILL) { + if (info->si_code == SEGV_MAPERR) + pr_info("Signal because of unmapped object.\n"); + else if (info->si_code == ILL_ILLOPN) + pr_info("Signal because of #UD\n"); + else + pr_info("Unknown si_code!\n"); + } else { + pr_error(test_errors, "Received signal that I cannot handle!\n"); + exit(1); + } + + /* Save the signal code */ + got_sigcode = info->si_code; + + /* + * Move to the next instruction; to move, increment the instruction + * pointer by 10 bytes. 10 bytes is the size of the instruction + * considering two prefix bytes, two opcode bytes, one + * ModRM byte, one SIB byte and 4 displacement bytes. We have + * a NOP sled after the instruction to ensure we continue execution + * safely in case we overestimate the size of the instruction. + */ + + if (exit_on_signal) { + pr_fail(test_failed, "Whoa! I got a signal! Something went wrong!\n"); + exit(1); + } +#ifdef __x86_64__ + ctx->uc_mcontext.gregs[REG_RIP] += 10; +#else + ctx->uc_mcontext.gregs[REG_EIP] += 10; +#endif +} From aea0f0bf1a2c6f41629735168be06baba7f660c6 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Mon, 7 Aug 2017 12:46:37 -0700 Subject: [PATCH 065/109] umip: LDT tests: update test generators to optionally generate code for STR and SLDT STR and SLDT are not emulated unless the kernel is patched for such purpose. Hence, it is useful to update the test generators to only create test code for such instructions if needed. A new command line argument for the script, --emulate-all is used for this purpose; i.e., test code for STR and SLDT will be generated if such parameter is given in the command line. TODO: Much of the code in these generator is repeated and can be put in a single library. This is a good example. Signed-off-by: Ricardo Neri --- .../recipes-core/umip/files/umip_test_gen_16.py | 17 +++++++++++++++++ .../recipes-core/umip/files/umip_test_gen_32.py | 17 +++++++++++++++++ .../recipes-core/umip/files/umip_test_gen_64.py | 17 +++++++++++++++++ 3 files changed, 51 insertions(+) diff --git a/meta-luv/recipes-core/umip/files/umip_test_gen_16.py b/meta-luv/recipes-core/umip/files/umip_test_gen_16.py index 6c788e57de5..70609661930 100644 --- a/meta-luv/recipes-core/umip/files/umip_test_gen_16.py +++ b/meta-luv/recipes-core/umip/files/umip_test_gen_16.py @@ -8,6 +8,8 @@ TODO: Add support for 16-bit address encodings TODO: Add support for SiB address encodings""" +import argparse + MODRM_MO0 = 0 MODRM_MO1 = 1 MODRM_MO2 = 2 @@ -399,7 +401,22 @@ def generate_test_cases(test_code, check_code): return test_code, check_code, header_info, index +def parse_args(): + parser = argparse.ArgumentParser() + parser.add_argument("--emulate-all", help="Test all UMIP-protected instructions. Otherwise, test code for STR and SLDT will not be generated", + action="store_true") + + args = parser.parse_args() + if args.emulate_all == False: + print ("Test code will not be generated for instructions SLDT and STR") + INSTS.remove(SLDT) + INSTS.remove(STR) + else: + print ("Generate test code for all UMIP-protected instructions") + def write_test_files(): + parse_args() + check_code = "" test_code = "/* This is an autogenerated file. If you intend to debug, better to debug the generating script. */\n\n" test_code += "\n" diff --git a/meta-luv/recipes-core/umip/files/umip_test_gen_32.py b/meta-luv/recipes-core/umip/files/umip_test_gen_32.py index 677a0721837..c501f085cb2 100644 --- a/meta-luv/recipes-core/umip/files/umip_test_gen_32.py +++ b/meta-luv/recipes-core/umip/files/umip_test_gen_32.py @@ -8,6 +8,8 @@ TODO: Add support for 16-bit address encodings TODO: Add support for SiB address encodings""" +import argparse + MODRM_MO0 = 0 MODRM_MO1 = 1 MODRM_MO2 = 2 @@ -627,7 +629,22 @@ def generate_test_cases(test_code, check_code): return test_code, check_code, header_info, index +def parse_args(): + parser = argparse.ArgumentParser() + parser.add_argument("--emulate-all", help="Test all UMIP-protected instructions. Otherwise, test code for STR and SLDT will not be generated.", + action="store_true") + + args = parser.parse_args() + if args.emulate_all == False: + print ("Test code will not be generated for instructions SLDT and STR") + INSTS.remove(SLDT) + INSTS.remove(STR) + else: + print ("Generate test code for all UMIP-protected instructions") + def write_test_files(): + parse_args() + check_code = "" test_code = "/* This is an autogenerated file. If you intend to debug, better to debug the generating script. */\n\n" test_code += "\n" diff --git a/meta-luv/recipes-core/umip/files/umip_test_gen_64.py b/meta-luv/recipes-core/umip/files/umip_test_gen_64.py index 7f815ae08b5..a8472b66a3d 100644 --- a/meta-luv/recipes-core/umip/files/umip_test_gen_64.py +++ b/meta-luv/recipes-core/umip/files/umip_test_gen_64.py @@ -8,6 +8,8 @@ TODO: Add support for 16-bit address encodings TODO: Add support for SiB address encodings""" +import argparse + MODRM_MO0 = 0 MODRM_MO1 = 1 MODRM_MO2 = 2 @@ -637,7 +639,22 @@ def generate_test_cases(test_code, check_code): return test_code, check_code, header_info, index +def parse_args(): + parser = argparse.ArgumentParser() + parser.add_argument("--emulate-all", help="Test all UMIP-protected instructions. Otherwise, test code for STR and SLDT will not be generated.", + action="store_true") + + args = parser.parse_args() + if args.emulate_all == False: + print ("Test code will not be generated for instructions SLDT and STR") + INSTS.remove(SLDT) + INSTS.remove(STR) + else: + print ("Generate test code for all UMIP-protected instructions") + def write_test_files(): + parse_args() + check_code = "" test_code = "/* This is an autogenerated file. If you intend to debug, better to debug the generating script. */\n\n" test_code += "\n" From ac2e9c040cbeb759a4fa63c12416fe409e8a545f Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Mon, 7 Aug 2017 13:03:02 -0700 Subject: [PATCH 066/109] umip: Makefile: use CFLAGS when building Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/Makefile | 30 +++++++++++------------ 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/Makefile b/meta-luv/recipes-core/umip/files/Makefile index fba4b3a4204..de34dfb50af 100644 --- a/meta-luv/recipes-core/umip/files/Makefile +++ b/meta-luv/recipes-core/umip/files/Makefile @@ -1,30 +1,30 @@ x86_64: - $(CC) -o umip_test2_64 umip_test2.c - $(CC) -o umip_test_64 umip_test.c - $(CC) -o umip_exceptions_64 umip_exceptions.c + $(CC) $(CFLAGS) -o umip_test2_64 umip_test2.c + $(CC) $(CFLAGS) -o umip_test_64 umip_test.c + $(CC) $(CFLAGS) -o umip_exceptions_64 umip_exceptions.c python umip_test_gen_64.py - $(CC) -c test_umip_ldt_64.c - $(CC) -c umip_ldt_64.c - $(CC) -o umip_ldt_64 test_umip_ldt_64.o umip_ldt_64.o + $(CC) $(CFLAGS) -c test_umip_ldt_64.c + $(CC) $(CFLAGS) -c umip_ldt_64.c + $(CC) $(CFLAGS) -o umip_ldt_64 test_umip_ldt_64.o umip_ldt_64.o i586: i686 i686: # fo 32-bit builds, use -m32 if building independently - $(CC) -o umip_test2_32 umip_test2.c - $(CC) -o umip_test_32 umip_test.c - $(CC) -o umip_exceptions_32 umip_exceptions.c + $(CC) $(CFLAGS) -o umip_test2_32 umip_test2.c + $(CC) $(CFLAGS) -o umip_test_32 umip_test.c + $(CC) $(CFLAGS) -o umip_exceptions_32 umip_exceptions.c python umip_test_gen_32.py - $(CC) -c test_umip_ldt_32.c - $(CC) -c umip_ldt_32.c - $(CC) -o umip_ldt_32 test_umip_ldt_32.o umip_ldt_32.o + $(CC) $(CFLAGS) -c test_umip_ldt_32.c + $(CC) $(CFLAGS) -c umip_ldt_32.c + $(CC) $(CFLAGS) -o umip_ldt_32 test_umip_ldt_32.o umip_ldt_32.o python umip_test_gen_16.py - $(CC) -c test_umip_ldt_16.c - $(CC) -c umip_ldt_16.c - $(CC) -o umip_ldt_16 test_umip_ldt_16.o umip_ldt_16.o + $(CC) $(CFLAGS) -c test_umip_ldt_16.c + $(CC) $(CFLAGS) -c umip_ldt_16.c + $(CC) $(CFLAGS) -o umip_ldt_16 test_umip_ldt_16.o umip_ldt_16.o .PHONY: clean From 7033462ce368fe2db92fa8482a603c2c19893d25 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Mon, 7 Aug 2017 13:13:17 -0700 Subject: [PATCH 067/109] umip: umip_utils: build all programs using the new utility library Update Makefile for this purpose. Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/Makefile | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/Makefile b/meta-luv/recipes-core/umip/files/Makefile index de34dfb50af..18ee415f5c1 100644 --- a/meta-luv/recipes-core/umip/files/Makefile +++ b/meta-luv/recipes-core/umip/files/Makefile @@ -1,30 +1,34 @@ x86_64: - $(CC) $(CFLAGS) -o umip_test2_64 umip_test2.c - $(CC) $(CFLAGS) -o umip_test_64 umip_test.c - $(CC) $(CFLAGS) -o umip_exceptions_64 umip_exceptions.c + $(CC) $(CFLAGS) -c umip_utils.c -o umip_utils_64.o + + $(CC) $(CFLAGS) -o umip_test2_64 umip_utils_64.o umip_test2.c + $(CC) $(CFLAGS) -o umip_test_64 umip_utils_64.o umip_test.c + $(CC) $(CFLAGS) -o umip_exceptions_64 umip_utils_64.o umip_exceptions.c python umip_test_gen_64.py $(CC) $(CFLAGS) -c test_umip_ldt_64.c $(CC) $(CFLAGS) -c umip_ldt_64.c - $(CC) $(CFLAGS) -o umip_ldt_64 test_umip_ldt_64.o umip_ldt_64.o + $(CC) $(CFLAGS) -o umip_ldt_64 test_umip_ldt_64.o umip_ldt_64.o umip_utils_64.o i586: i686 i686: # fo 32-bit builds, use -m32 if building independently - $(CC) $(CFLAGS) -o umip_test2_32 umip_test2.c - $(CC) $(CFLAGS) -o umip_test_32 umip_test.c - $(CC) $(CFLAGS) -o umip_exceptions_32 umip_exceptions.c + $(CC) $(CFLAGS) -c umip_utils.c -o umip_utils_32.o + + $(CC) $(CFLAGS) -o umip_test2_32 umip_utils_32.o umip_test2.c + $(CC) $(CFLAGS) -o umip_test_32 umip_utils_32.o umip_test.c + $(CC) $(CFLAGS) -o umip_exceptions_32 umip_utils_32.o umip_exceptions.c python umip_test_gen_32.py $(CC) $(CFLAGS) -c test_umip_ldt_32.c $(CC) $(CFLAGS) -c umip_ldt_32.c - $(CC) $(CFLAGS) -o umip_ldt_32 test_umip_ldt_32.o umip_ldt_32.o + $(CC) $(CFLAGS) -o umip_ldt_32 test_umip_ldt_32.o umip_ldt_32.o umip_utils_32.o python umip_test_gen_16.py $(CC) $(CFLAGS) -c test_umip_ldt_16.c $(CC) $(CFLAGS) -c umip_ldt_16.c - $(CC) $(CFLAGS) -o umip_ldt_16 test_umip_ldt_16.o umip_ldt_16.o + $(CC) $(CFLAGS) -o umip_ldt_16 test_umip_ldt_16.o umip_ldt_16.o umip_utils_32.o .PHONY: clean From 7a24f24145de392600b44490d4f41f4751a725df Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Mon, 7 Aug 2017 13:44:59 -0700 Subject: [PATCH 068/109] umip: build test programs for emulation of all UMIP-protected instructions It is convenient to have versions of the test programs that test both the required functionality of UMIP emulation (i.e., emulate only 32-bit processes and only the instructions SIDT, SGDT and SMSW) and the complete emulation: emulate all UMIP-protected instructions in both 32 and 64-bit processes. Update our Makefile for this purpose. Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/Makefile | 45 +++++++++++++++++++++-- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/Makefile b/meta-luv/recipes-core/umip/files/Makefile index 18ee415f5c1..218ab35e4c5 100644 --- a/meta-luv/recipes-core/umip/files/Makefile +++ b/meta-luv/recipes-core/umip/files/Makefile @@ -1,4 +1,4 @@ -x86_64: +x86_64_minimal: $(CC) $(CFLAGS) -c umip_utils.c -o umip_utils_64.o $(CC) $(CFLAGS) -o umip_test2_64 umip_utils_64.o umip_test2.c @@ -10,10 +10,22 @@ x86_64: $(CC) $(CFLAGS) -c umip_ldt_64.c $(CC) $(CFLAGS) -o umip_ldt_64 test_umip_ldt_64.o umip_ldt_64.o umip_utils_64.o -i586: i686 +x86_64_emul_all: CFLAGS += -DEMULATE_ALL +x86_64_emul_all: + $(CC) $(CFLAGS) -c umip_utils.c -o umip_utils_64.o + + $(CC) $(CFLAGS) -o umip_test2_64_emul_all umip_utils_64.o umip_test2.c + $(CC) $(CFLAGS) -o umip_test_64_emul_all umip_utils_64.o umip_test.c + $(CC) $(CFLAGS) -o umip_exceptions_64_emul_all umip_utils_64.o umip_exceptions.c -i686: - # fo 32-bit builds, use -m32 if building independently + python umip_test_gen_64.py --emulate-all + $(CC) $(CFLAGS) -c test_umip_ldt_64.c + $(CC) $(CFLAGS) -c umip_ldt_64.c + $(CC) $(CFLAGS) -o umip_ldt_64_emul_all test_umip_ldt_64.o umip_ldt_64.o umip_utils_64.o + +x86_64: x86_64_minimal x86_64_emul_all + +i686_minimal: $(CC) $(CFLAGS) -c umip_utils.c -o umip_utils_32.o $(CC) $(CFLAGS) -o umip_test2_32 umip_utils_32.o umip_test2.c @@ -30,14 +42,39 @@ i686: $(CC) $(CFLAGS) -c umip_ldt_16.c $(CC) $(CFLAGS) -o umip_ldt_16 test_umip_ldt_16.o umip_ldt_16.o umip_utils_32.o +i686_emul_all: CFLAGS += -DEMULATE_ALL +i686_emul_all: + $(CC) $(CFLAGS) -c umip_utils.c -o umip_utils_32.o + + $(CC) $(CFLAGS) -o umip_test2_32_emul_all umip_utils_32.o umip_test2.c + $(CC) $(CFLAGS) -o umip_test_32_emul_all umip_utils_32.o umip_test.c + $(CC) $(CFLAGS) -o umip_exceptions_32_emul_all umip_utils_32.o umip_exceptions.c + + python umip_test_gen_32.py --emulate-all + $(CC) $(CFLAGS) -c test_umip_ldt_32.c + $(CC) $(CFLAGS) -c umip_ldt_32.c + $(CC) $(CFLAGS) -o umip_ldt_32_emul_all test_umip_ldt_32.o umip_ldt_32.o umip_utils_32.o + + python umip_test_gen_16.py --emulate-all + $(CC) $(CFLAGS) -c test_umip_ldt_16.c + $(CC) $(CFLAGS) -c umip_ldt_16.c + $(CC) $(CFLAGS) -o umip_ldt_16_emul_all test_umip_ldt_16.o umip_ldt_16.o umip_utils_32.o + +i686: i686_minimal i686_emul_all +i586: i686 .PHONY: clean clean: rm -f *.o rm -f umip_test rm -f umip_test2_64 + rm -f umip_test2_64_emul_all rm -f umip_test2_32 + rm -f umip_test2_32_emul_all rm -f umip_pf rm -f umip_ldt_64 + rm -f umip_ldt_64_emul_all rm -f umip_ldt_32 + rm -f umip_ldt_32_emul_all rm -f umip_ldt_16 + rm -f umip_ldt_16_emul_all From ee7c8c2fb02a8d41fa5408a80ab7b66a15994fbe Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Mon, 7 Aug 2017 14:26:13 -0700 Subject: [PATCH 069/109] umip: Makefile: add missing files when cleaning The .clean target was mssing several binaries that needed to be deleted. Add them. Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/Makefile | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/Makefile b/meta-luv/recipes-core/umip/files/Makefile index 218ab35e4c5..a584e9ca50b 100644 --- a/meta-luv/recipes-core/umip/files/Makefile +++ b/meta-luv/recipes-core/umip/files/Makefile @@ -66,12 +66,18 @@ i586: i686 .PHONY: clean clean: rm -f *.o - rm -f umip_test + rm -f umip_test_64 + rm -f umip_test_64_emul_all + rm -f umip_test_32 + rm -f umip_test_32_emul_all rm -f umip_test2_64 rm -f umip_test2_64_emul_all rm -f umip_test2_32 rm -f umip_test2_32_emul_all - rm -f umip_pf + rm -f umip_exceptions_64 + rm -f umip_exceptions_64_emul_all + rm -f umip_exceptions_32 + rm -f umip_exceptions_32_emul_all rm -f umip_ldt_64 rm -f umip_ldt_64_emul_all rm -f umip_ldt_32 From 51bb33bcbb0672d0169de39359cf77d903ad3cae Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Mon, 7 Aug 2017 14:31:25 -0700 Subject: [PATCH 070/109] umip: umip_test: rename as umip_test_basic The file umip_test.c provide a set of basic tests for the emulation of UMIP-protected instructions. Thus, rename it to refect this fact. Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/Makefile | 16 ++++++++-------- .../files/{umip_test.c => umip_test_basic.c} | 0 2 files changed, 8 insertions(+), 8 deletions(-) rename meta-luv/recipes-core/umip/files/{umip_test.c => umip_test_basic.c} (100%) diff --git a/meta-luv/recipes-core/umip/files/Makefile b/meta-luv/recipes-core/umip/files/Makefile index a584e9ca50b..c487af653f6 100644 --- a/meta-luv/recipes-core/umip/files/Makefile +++ b/meta-luv/recipes-core/umip/files/Makefile @@ -2,7 +2,7 @@ x86_64_minimal: $(CC) $(CFLAGS) -c umip_utils.c -o umip_utils_64.o $(CC) $(CFLAGS) -o umip_test2_64 umip_utils_64.o umip_test2.c - $(CC) $(CFLAGS) -o umip_test_64 umip_utils_64.o umip_test.c + $(CC) $(CFLAGS) -o umip_test_basic_64 umip_utils_64.o umip_test_basic.c $(CC) $(CFLAGS) -o umip_exceptions_64 umip_utils_64.o umip_exceptions.c python umip_test_gen_64.py @@ -15,7 +15,7 @@ x86_64_emul_all: $(CC) $(CFLAGS) -c umip_utils.c -o umip_utils_64.o $(CC) $(CFLAGS) -o umip_test2_64_emul_all umip_utils_64.o umip_test2.c - $(CC) $(CFLAGS) -o umip_test_64_emul_all umip_utils_64.o umip_test.c + $(CC) $(CFLAGS) -o umip_test_basic_64_emul_all umip_utils_64.o umip_test_basic.c $(CC) $(CFLAGS) -o umip_exceptions_64_emul_all umip_utils_64.o umip_exceptions.c python umip_test_gen_64.py --emulate-all @@ -29,7 +29,7 @@ i686_minimal: $(CC) $(CFLAGS) -c umip_utils.c -o umip_utils_32.o $(CC) $(CFLAGS) -o umip_test2_32 umip_utils_32.o umip_test2.c - $(CC) $(CFLAGS) -o umip_test_32 umip_utils_32.o umip_test.c + $(CC) $(CFLAGS) -o umip_test_basic_32 umip_utils_32.o umip_test_basic.c $(CC) $(CFLAGS) -o umip_exceptions_32 umip_utils_32.o umip_exceptions.c python umip_test_gen_32.py @@ -47,7 +47,7 @@ i686_emul_all: $(CC) $(CFLAGS) -c umip_utils.c -o umip_utils_32.o $(CC) $(CFLAGS) -o umip_test2_32_emul_all umip_utils_32.o umip_test2.c - $(CC) $(CFLAGS) -o umip_test_32_emul_all umip_utils_32.o umip_test.c + $(CC) $(CFLAGS) -o umip_test_basic_32_emul_all umip_utils_32.o umip_test_basic.c $(CC) $(CFLAGS) -o umip_exceptions_32_emul_all umip_utils_32.o umip_exceptions.c python umip_test_gen_32.py --emulate-all @@ -66,10 +66,10 @@ i586: i686 .PHONY: clean clean: rm -f *.o - rm -f umip_test_64 - rm -f umip_test_64_emul_all - rm -f umip_test_32 - rm -f umip_test_32_emul_all + rm -f umip_test_basic_64 + rm -f umip_test_basic_64_emul_all + rm -f umip_test_basic_32 + rm -f umip_test_basic_32_emul_all rm -f umip_test2_64 rm -f umip_test2_64_emul_all rm -f umip_test2_32 diff --git a/meta-luv/recipes-core/umip/files/umip_test.c b/meta-luv/recipes-core/umip/files/umip_test_basic.c similarity index 100% rename from meta-luv/recipes-core/umip/files/umip_test.c rename to meta-luv/recipes-core/umip/files/umip_test_basic.c From 85e33076cc61f161ca96e8969533c680fc71b633 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Mon, 7 Aug 2017 14:33:37 -0700 Subject: [PATCH 071/109] umip: umip_test2: Rename as umip_test_opnds The file umip_test2.c provides tests to evaluate the emulation of UMIP- protected instructions when using both registers and memory locations as operands. Hence, give this file a more appropriate name. Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/Makefile | 16 ++++++++-------- .../files/{umip_test2.c => umip_test_opnds.c} | 0 2 files changed, 8 insertions(+), 8 deletions(-) rename meta-luv/recipes-core/umip/files/{umip_test2.c => umip_test_opnds.c} (100%) diff --git a/meta-luv/recipes-core/umip/files/Makefile b/meta-luv/recipes-core/umip/files/Makefile index c487af653f6..4b980f0bab1 100644 --- a/meta-luv/recipes-core/umip/files/Makefile +++ b/meta-luv/recipes-core/umip/files/Makefile @@ -1,7 +1,7 @@ x86_64_minimal: $(CC) $(CFLAGS) -c umip_utils.c -o umip_utils_64.o - $(CC) $(CFLAGS) -o umip_test2_64 umip_utils_64.o umip_test2.c + $(CC) $(CFLAGS) -o umip_test_opnds_64 umip_utils_64.o umip_test_opnds.c $(CC) $(CFLAGS) -o umip_test_basic_64 umip_utils_64.o umip_test_basic.c $(CC) $(CFLAGS) -o umip_exceptions_64 umip_utils_64.o umip_exceptions.c @@ -14,7 +14,7 @@ x86_64_emul_all: CFLAGS += -DEMULATE_ALL x86_64_emul_all: $(CC) $(CFLAGS) -c umip_utils.c -o umip_utils_64.o - $(CC) $(CFLAGS) -o umip_test2_64_emul_all umip_utils_64.o umip_test2.c + $(CC) $(CFLAGS) -o umip_test_opnds_64_emul_all umip_utils_64.o umip_test_opnds.c $(CC) $(CFLAGS) -o umip_test_basic_64_emul_all umip_utils_64.o umip_test_basic.c $(CC) $(CFLAGS) -o umip_exceptions_64_emul_all umip_utils_64.o umip_exceptions.c @@ -28,7 +28,7 @@ x86_64: x86_64_minimal x86_64_emul_all i686_minimal: $(CC) $(CFLAGS) -c umip_utils.c -o umip_utils_32.o - $(CC) $(CFLAGS) -o umip_test2_32 umip_utils_32.o umip_test2.c + $(CC) $(CFLAGS) -o umip_test_opnds_32 umip_utils_32.o umip_test_opnds.c $(CC) $(CFLAGS) -o umip_test_basic_32 umip_utils_32.o umip_test_basic.c $(CC) $(CFLAGS) -o umip_exceptions_32 umip_utils_32.o umip_exceptions.c @@ -46,7 +46,7 @@ i686_emul_all: CFLAGS += -DEMULATE_ALL i686_emul_all: $(CC) $(CFLAGS) -c umip_utils.c -o umip_utils_32.o - $(CC) $(CFLAGS) -o umip_test2_32_emul_all umip_utils_32.o umip_test2.c + $(CC) $(CFLAGS) -o umip_test_opnds_32_emul_all umip_utils_32.o umip_test_opnds.c $(CC) $(CFLAGS) -o umip_test_basic_32_emul_all umip_utils_32.o umip_test_basic.c $(CC) $(CFLAGS) -o umip_exceptions_32_emul_all umip_utils_32.o umip_exceptions.c @@ -70,10 +70,10 @@ clean: rm -f umip_test_basic_64_emul_all rm -f umip_test_basic_32 rm -f umip_test_basic_32_emul_all - rm -f umip_test2_64 - rm -f umip_test2_64_emul_all - rm -f umip_test2_32 - rm -f umip_test2_32_emul_all + rm -f umip_test_opnds_64 + rm -f umip_test_opnds_64_emul_all + rm -f umip_test_opnds_32 + rm -f umip_test_opnds_32_emul_all rm -f umip_exceptions_64 rm -f umip_exceptions_64_emul_all rm -f umip_exceptions_32 diff --git a/meta-luv/recipes-core/umip/files/umip_test2.c b/meta-luv/recipes-core/umip/files/umip_test_opnds.c similarity index 100% rename from meta-luv/recipes-core/umip/files/umip_test2.c rename to meta-luv/recipes-core/umip/files/umip_test_opnds.c From 9d8346e0ba7a011d54368a2ae101324e806a408e Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Mon, 7 Aug 2017 14:58:28 -0700 Subject: [PATCH 072/109] umip: umip_utils: beautify print_results Aesthetic changes for grammar and syntax correctness. Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/umip_utils.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/meta-luv/recipes-core/umip/files/umip_utils.c b/meta-luv/recipes-core/umip/files/umip_utils.c index 930ede038c8..a8108c864c7 100644 --- a/meta-luv/recipes-core/umip/files/umip_utils.c +++ b/meta-luv/recipes-core/umip/files/umip_utils.c @@ -15,7 +15,7 @@ int exit_on_signal; void print_results(void) { - printf("RESULTS: passed[%d], failed[%d], error[%d]\n", + printf("RESULTS: passed[%d], failed[%d], errors[%d].\n", test_passed, test_failed, test_errors); } From d94f62beba41f525551cc81f3808a568a5e6ae13 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Mon, 7 Aug 2017 15:51:12 -0700 Subject: [PATCH 073/109] umip: rewrite UMIP_README Write a better UMIP_README that explains how to build the test programs, what is being tested, how to intepret the result and the variants for 32 and 64 bits as well as the option to test complete emulation of the UMIP-protected instructions. Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/UMIP_README | 214 ++++++++++++++++--- 1 file changed, 184 insertions(+), 30 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/UMIP_README b/meta-luv/recipes-core/umip/files/UMIP_README index 3fc82ded37c..bc94f4e9e6a 100644 --- a/meta-luv/recipes-core/umip/files/UMIP_README +++ b/meta-luv/recipes-core/umip/files/UMIP_README @@ -1,31 +1,185 @@ -A quick guide for UMIP testing - -* Use umip_test for a very basic test that exercises the instructions smsw, - str, sidt, sldt and sgdt from protected mode using the global descriptor - table. -* Use test2_32 for a collection of tests that exercises the instructions - str, sldt and smsw for register and memory operands. For registers, - operand sizes are of 16 and 32 bits. Memory operands are always 16-bits - long. Addresses in these tests are computed using only the ModRM - byte. Also, these tests use the normal __USER_DS segment with a base - address of zero. -* DEPRECATED. Now UMIP is made to support only 32-bit builds of the kernel. - Use test2_64 for a collection of tests that exercises the instructions - str, sldt and smsw for register and memory operands. For registers, - operand sizes are of 16, 32 and 64 bits. Memory operands are always - 16-bit long. Addresses in these tests are computed using only the ModRM - byte. Also, these tests use the normal __USER_DS segment with a base + A QUICK GUIDE FOR UMIP TESTING + +Contents + + I. For the impatient + II. What is UMIP and how is supported in the Linux kernel? +III. Testing UMIP + a) Overview + b) Testing full emulation + c) Testing 64-bit processes + d) Testing 32-bit processes + e) Testing output format + f) The test programs + + ============================================================ +I. For the impatient + +If all you have is 10 seconds, all you need to know is this: + +Assuming you have a 64-bit distro install, here is what you need to do: + + 1. If in Ubuntu: + $ sudo apt-get install ia32-libs + If in other distros, not sure; a similar package + 2. $ make x86_64 + 3. $ CFLAGS=-m32 make i586 + 4. $ umip_test_basic_64 + 5. $ umip_test_basic_32 + 6. $ umip_test_opnds_64 + 7. $ umip_test_opnds_32 + 8. $ umip_exceptions_64 + 9. $ umip_exceptions_32 + 10. $ umip_ldt_64 + 11. $ umip_ldt_32 + 12. $ umip_ldt_16 + +For each of the steps 4 through 12, look for this output + + RESULTS: passed[X], failed[0], errors[0]. + +You should have 0 failures and 0 errors. + +You are done. + + ============================================================ +II. What is UMIP and how is supported in the Linux kernel? + +User-mode Instruction Prevention, UMIP is a feature present in new Intel +processors that prevents the execution of the instructions SGDT, SIDT, SLDT, +SMSW and STR while running in CLP>0. In such a case, a general protection fault +exception is issued. + +Functionality has been put in the kernel to trap this general protection fault +exception. When this happens, the kernel provides to the caller dummy values +as the result for these instructions. However, emulation is not done in every +case. It is done only for 32-bit processes and only for the instructions SGDT, +SIDT and SMSW. For 64-bit processes no emulation is provideded. The instructions +STR and SLDT are not emulated in any case. + +However, the Linux kernel can be patched to provide emulation in all cases. + + ============================================================ +III. Testing UMIP + +a) == Overview === + +I have written several programs to test the behavior described above. Each +program can be built for 32-bit and 64-bit processes. The makefile builds both +versions and adds _32 or _64 suffixes to the executable test programs. + +b) == Achieving emulation for everything + +Also, each program can be built to test emulation for all the instructions in +both 64 and 32-bits and for all the instructions. The makefile achieves this by +adding -DEMULATE_ALL to the CFLAGS; the python scripts that generate code are +run with the option --emulate-all. Test programs built with this configuration +are added the suffix _emul_all. + +Hence, the naming convention for the test programs is: + + test_program_32/64_[emul_all] + +c) == Testing 64-bit processes + +The makefile can the test programs for all these configurations. + +To build the 64-bit test programs you can do + + $ make x86_64 + +This will build the programs umip_test_basic, umip_test_opnds, umip_exceptions, +and umip_ldt_64; see section III.a) for the relevant naming convention. + +In its default version, these programs will verify a general protection fault +is seen when executing the UMIP-protected instructions. In the _emul_all +version, programs will verify that emulation is successful and no exceptions are +generated in the process (please see the umip_exceptions special case below). + +Both the default and _emul_all versions are built with the aforementioned +command. + +d) == Testing 32-bit processes + +In a 64-bit distro, you will need to install the 32-bit compatibility libraries. +In Ubuntu, this is done with: + + $ sudo apt-get install ia32-libs + +In other distros, a similar procedure can be followed. + +Then, the 32-bit test programs can be built as: + + $ CFLAGS=-m32 make i586 + +This will build the programs umip_test_basic, umip_test_opnds, umip_exceptions, +umip_ldt_32, and umip_ldt_16; see section III.a) for the relevant naming +convention. + +In its default version, these programs will verify that emulation is successful +for the instructions SGDT, SIDT and SMSW. They will also verify that a general +proctection fault is seen for the instructions SLDT and STR. + +Both the default and _emul_all versions are built with the aforementioned +commans. + +e) == The testing output format + +Test programs use the following output format + +[info] To print information about a test case. +[pass] To inform that a test case passed. +[FAIL] To inform that a test case failed. + +RESULTS: passed[X], failed[Y], error[Z] + +Ideally, you should see all test case passed. This means that you should see +0 failures and 0 errors. + +f) The test programs + +Refer to section III.a) for the naming convention. + +* umip_est_basic. Use umip_test for a very basic test that exercises the + instructions smsw, str, sidt, sldt and sgdt from protected mode using the + global descriptortable. + +* umip_test_opnds. Use umip_test_opnds for a collection of tests that + exercises the instructions str, sldt and smsw for register and memory + operands. For registers, operand sizes are of 16 and 32 bits. Memory operands + are always 16-bits long. Addresses in these tests are computed using only the + ModRM byte. Also, these tests use the normal __USER_DS segment with a base address of zero. -* Use umip_exceptions to verify that a page fault is correctly issued when - UMIP-emulated code tries to write in an invalid memory address of the user - space memory. -* Use umip_ldt to verify all the possible 32-bit address encodings in the - UMIP-emulated instructions. This includes all the ModRM, and SiB displacement - combinations. Furthermore, this test configure a Local Descriptor Table - whose segments have base addresses that are different from the __USER_DS - segment. -* Use umip_ldt_16 to verify all the possible 16-bit address encodings in the - UMIP-emulated instructions. This includes all the ModRM and displacement - combinations. Furthermore, this test configure a Local Descriptor Table - whose segments have base addresses that are different from the __USER_DS - segment. Also, the code segment is configured for 16-bit addresses. + +* umip_exceptions. Use umip_exceptions that the emulation code issues the + exceptions for each of the UMIP-instructions as described in the Intel + Software Development Manual. Priority of the exceptions should be that + mentioned in the Volume 3, Section 16.9 of the aforementioned manual. That is, + for instance, if the instructions can lead to a general protection fault + exception and an illegal operand exception, verify that the latter is seen. + +* umip_ldt_64. Use umip_ldt_64 to verify all the possible 64-bit address + encodings in the UMIP-protected instructions. This includes all the ModRM, + and SiB displacement combinations. Furthermore, this test configures a Local + Descriptor Table whose segments have base addresses that are different from + the __USER_DS segment. Since this program runs in long mode, segmentation is + implemented using the FS and GS registers. The purpose of this test is to + verify that the emulation code is capable of succesfully computing linear + address out of the instruction operand and segment descriptor. + +* umip_ldt_32. Use umip_ldt_32 to verify all the possible 32-bit address + encodings in the UMIP-protected instructions. This includes all the ModRM, + and SiB displacement combinations. Furthermore, this test configures a Local + Descriptor Table whose segments have base addresses that are different from + the __USER_DS segment. The purpose of this test is to verify that the + emulation code is capable of succesfully computing linear address out of the + instruction operand and segment descriptor. + +* umip_ldt_16. Use umip_ldt_16 to verify all the possible 16-bit address + encodings in the UMIP-protected instructions. This includes all the ModRM and + displacement combinations. Furthermore, this test configure a Local Descriptor + Table with segments that have base addresses different than the __USER_DS + segment. Also, the code segment is configured for 16-bit addresses. The + purpose of this test is to verify that the emulation code is capable of + succesfully computing linear address out of the instruction operand and + segment descriptor. From 4b79d1ec78f2ebf20969db189a39a6c7c9da0a23 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Mon, 7 Aug 2017 18:45:46 -0700 Subject: [PATCH 074/109] umip: update recipe for renamed source files Some source files were updated for clarity. Update the recipe accordingly. Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/umip-tests_0.1.bb | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/meta-luv/recipes-core/umip/umip-tests_0.1.bb b/meta-luv/recipes-core/umip/umip-tests_0.1.bb index eaa0fe1d937..5a13c01af5a 100644 --- a/meta-luv/recipes-core/umip/umip-tests_0.1.bb +++ b/meta-luv/recipes-core/umip/umip-tests_0.1.bb @@ -6,8 +6,9 @@ LIC_FILES_CHKSUM = "file://COPYING;md5=751419260aa954499f7abaabaa882bbe" SRC_URI="file://umip_test_defs.h \ - file://umip_test.c \ - file://umip_test2.c \ + file://umip_utils.c \ + file://umip_test_basic.c \ + file://umip_test_opnds.c \ file://umip_exceptions.c \ file://COPYING \ file://Makefile \ @@ -35,22 +36,22 @@ do_install() { install -d ${D}${bindir} install -m 744 ${WORKDIR}/UMIP_README ${D}${bindir} if [ "${TARGET_ARCH}" = "x86_64" ]; then - install -m 755 ${WORKDIR}/umip_test_64 ${D}${bindir} + install -m 755 ${WORKDIR}/umip_test_basic_64 ${D}${bindir} install -m 755 ${WORKDIR}/umip_exceptions_64 ${D}${bindir} - install -m 755 ${WORKDIR}/umip_test2_64 ${D}${bindir} + install -m 755 ${WORKDIR}/umip_test_opnds_64 ${D}${bindir} install -m 755 ${WORKDIR}/umip_ldt_64 ${D}${bindir} fi if [ "${TARGET_ARCH}" = "i586" ]; then - install -m 755 ${WORKDIR}/umip_test_32 ${D}${bindir} + install -m 755 ${WORKDIR}/umip_test_basic_32 ${D}${bindir} install -m 755 ${WORKDIR}/umip_exceptions_32 ${D}${bindir} - install -m 755 ${WORKDIR}/umip_test2_32 ${D}${bindir} + install -m 755 ${WORKDIR}/umip_test_opnds_32 ${D}${bindir} install -m 755 ${WORKDIR}/umip_ldt_32 ${D}${bindir} install -m 755 ${WORKDIR}/umip_ldt_16 ${D}${bindir} fi if [ "${TARGET_ARCH}" = "i686" ]; then - install -m 755 ${WORKDIR}/umip_test_32 ${D}${bindir} + install -m 755 ${WORKDIR}/umip_test_basic_32 ${D}${bindir} install -m 755 ${WORKDIR}/umip_exceptions_32 ${D}${bindir} - install -m 755 ${WORKDIR}/umip_test2_32 ${D}${bindir} + install -m 755 ${WORKDIR}/umip_test_opnds_32 ${D}${bindir} install -m 755 ${WORKDIR}/umip_ldt_32 ${D}${bindir} install -m 755 ${WORKDIR}/umip_ldt_16 ${D}${bindir} fi From cb0324bc2f3b5728380771656226dd9caa442c37 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Tue, 8 Aug 2017 11:24:50 -0700 Subject: [PATCH 075/109] umip: LDT tests: move declaration of code and data segment memory to .c files Instead of keeping them in the autogenerated header file, move them to the source file. This also requires to add extern declarations when used elsewhere. Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/umip_ldt_16.c | 6 ++++++ meta-luv/recipes-core/umip/files/umip_ldt_32.c | 5 +++++ meta-luv/recipes-core/umip/files/umip_ldt_64.c | 2 ++ .../recipes-core/umip/files/umip_test_gen_16.py | 14 +++++++------- .../recipes-core/umip/files/umip_test_gen_32.py | 12 ++++++------ .../recipes-core/umip/files/umip_test_gen_64.py | 8 ++++---- 6 files changed, 30 insertions(+), 17 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/umip_ldt_16.c b/meta-luv/recipes-core/umip/files/umip_ldt_16.c index 0b397e2e1d5..9663172660f 100644 --- a/meta-luv/recipes-core/umip/files/umip_ldt_16.c +++ b/meta-luv/recipes-core/umip/files/umip_ldt_16.c @@ -15,6 +15,12 @@ extern unsigned char test_umip[], test_umip_end[]; extern unsigned char interim[], interim_start[], interim_end[]; extern unsigned char finish_testing[]; +extern unsigned char data[SEGMENT_SIZE]; +extern unsigned char data_es[SEGMENT_SIZE]; +extern unsigned char data_fs[SEGMENT_SIZE]; +extern unsigned char data_gs[SEGMENT_SIZE]; +extern unsigned char stack_32[SEGMENT_SIZE]; +extern unsigned char stack[SEGMENT_SIZE]; extern int exit_on_signal; unsigned short cs_orig; diff --git a/meta-luv/recipes-core/umip/files/umip_ldt_32.c b/meta-luv/recipes-core/umip/files/umip_ldt_32.c index 87e93b2823d..c45fdd452e1 100644 --- a/meta-luv/recipes-core/umip/files/umip_ldt_32.c +++ b/meta-luv/recipes-core/umip/files/umip_ldt_32.c @@ -15,6 +15,11 @@ extern unsigned char test_umip[], test_umip_end[]; extern unsigned char finish_testing[]; +extern unsigned char data[SEGMENT_SIZE]; +extern unsigned char data_es[SEGMENT_SIZE]; +extern unsigned char data_fs[SEGMENT_SIZE]; +extern unsigned char data_gs[SEGMENT_SIZE]; +extern unsigned char stack[SEGMENT_SIZE]; extern int exit_on_signal; unsigned short cs_orig; diff --git a/meta-luv/recipes-core/umip/files/umip_ldt_64.c b/meta-luv/recipes-core/umip/files/umip_ldt_64.c index 69027b0dd64..d3d29f7c7c8 100644 --- a/meta-luv/recipes-core/umip/files/umip_ldt_64.c +++ b/meta-luv/recipes-core/umip/files/umip_ldt_64.c @@ -16,6 +16,8 @@ extern unsigned char test_umip[], test_umip_end[]; extern unsigned char finish_testing[]; +extern unsigned char data_fs[SEGMENT_SIZE]; +extern unsigned char data_gs[SEGMENT_SIZE]; extern int exit_on_signal; unsigned long old_fsbase, old_gsbase; unsigned short old_fs, old_gs; diff --git a/meta-luv/recipes-core/umip/files/umip_test_gen_16.py b/meta-luv/recipes-core/umip/files/umip_test_gen_16.py index 70609661930..b196c62dbaa 100644 --- a/meta-luv/recipes-core/umip/files/umip_test_gen_16.py +++ b/meta-luv/recipes-core/umip/files/umip_test_gen_16.py @@ -329,13 +329,6 @@ def generate_test_cases(test_code, check_code): header_info += "#define SEGMENT_SIZE " + str(SEGMENT_SIZE) + "\n" header_info += "#define CODE_MEM_SIZE " + str(CODE_MEM_SIZE) + "\n" header_info += "\n" - header_info += "unsigned char data[SEGMENT_SIZE];\n" - header_info += "unsigned char data_es[SEGMENT_SIZE];\n" - header_info += "unsigned char data_fs[SEGMENT_SIZE];\n" - header_info += "unsigned char data_gs[SEGMENT_SIZE];\n" - header_info += "unsigned char stack_32[SEGMENT_SIZE];\n" - header_info += "unsigned char stack[SEGMENT_SIZE];\n" - header_info += "\n" header_info += "void check_results(void);\n" header_info += "\n" @@ -348,6 +341,13 @@ def generate_test_cases(test_code, check_code): check_code +="int " + TEST_FAIL_CTR_VAR + ";\n" check_code +="int " + TEST_ERROR_CTR_VAR + ";\n" check_code += "\n" + check_code += "unsigned char data[SEGMENT_SIZE];\n" + check_code += "unsigned char data_es[SEGMENT_SIZE];\n" + check_code += "unsigned char data_fs[SEGMENT_SIZE];\n" + check_code += "unsigned char data_gs[SEGMENT_SIZE];\n" + check_code += "unsigned char stack_32[SEGMENT_SIZE];\n" + check_code += "unsigned char stack[SEGMENT_SIZE];\n" + check_code += "\n" run_check_code = "" diff --git a/meta-luv/recipes-core/umip/files/umip_test_gen_32.py b/meta-luv/recipes-core/umip/files/umip_test_gen_32.py index c501f085cb2..e1bf7e9af17 100644 --- a/meta-luv/recipes-core/umip/files/umip_test_gen_32.py +++ b/meta-luv/recipes-core/umip/files/umip_test_gen_32.py @@ -563,12 +563,6 @@ def generate_test_cases(test_code, check_code): header_info += "#define SEGMENT_SIZE " + str(SEGMENT_SIZE) + "\n" header_info += "#define CODE_MEM_SIZE " + str(CODE_MEM_SIZE) + "\n" header_info += "\n" - header_info += "unsigned char data[SEGMENT_SIZE];\n" - header_info += "unsigned char data_es[SEGMENT_SIZE];\n" - header_info += "unsigned char data_fs[SEGMENT_SIZE];\n" - header_info += "unsigned char data_gs[SEGMENT_SIZE];\n" - header_info += "unsigned char stack[SEGMENT_SIZE];\n" - header_info += "\n" header_info += "void check_results(void);\n" header_info += "\n" @@ -581,6 +575,12 @@ def generate_test_cases(test_code, check_code): check_code +="int " + TEST_FAIL_CTR_VAR + ";\n" check_code +="int " + TEST_ERROR_CTR_VAR + ";\n" check_code += "\n" + check_code += "unsigned char data[SEGMENT_SIZE];\n" + check_code += "unsigned char data_es[SEGMENT_SIZE];\n" + check_code += "unsigned char data_fs[SEGMENT_SIZE];\n" + check_code += "unsigned char data_gs[SEGMENT_SIZE];\n" + check_code += "unsigned char stack[SEGMENT_SIZE];\n" + check_code += "\n" run_check_code = "" diff --git a/meta-luv/recipes-core/umip/files/umip_test_gen_64.py b/meta-luv/recipes-core/umip/files/umip_test_gen_64.py index a8472b66a3d..11d5588546b 100644 --- a/meta-luv/recipes-core/umip/files/umip_test_gen_64.py +++ b/meta-luv/recipes-core/umip/files/umip_test_gen_64.py @@ -588,12 +588,9 @@ def generate_tests_all_insts(seg, start_index, start_test_nr): def generate_test_cases(test_code, check_code): header_info = "/* ******************** AUTOGENERATED CODE ******************** */\n" + header_info += "\n" header_info += "#define SEGMENT_SIZE " + str(SEGMENT_SIZE) + "\n" header_info += "#define CODE_MEM_SIZE " + str(CODE_MEM_SIZE) + "\n" - header_info += "\n" - header_info += "unsigned char data_fs[SEGMENT_SIZE];\n" - header_info += "unsigned char data_gs[SEGMENT_SIZE];\n" - header_info += "\n" header_info += "void check_results(void);\n" header_info += "\n" @@ -606,6 +603,9 @@ def generate_test_cases(test_code, check_code): check_code +="int " + TEST_FAIL_CTR_VAR + ";\n" check_code +="int " + TEST_ERROR_CTR_VAR + ";\n" check_code += "\n" + check_code += "unsigned char data_fs[SEGMENT_SIZE];\n" + check_code += "unsigned char data_gs[SEGMENT_SIZE];\n" + check_code += "\n" run_check_code = "" test_code += "\tasm(\n" From ab2aa4bec3989e8da99dace494556a4229e68541 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Tue, 8 Aug 2017 11:25:12 -0700 Subject: [PATCH 076/109] umip: Makefile: make emul_all targets depend on their minimal counterparts This is needed as both will invoke the pyhton code generation scripts. When building in parallel, this can cause problems as multiple instances of the same script would write to the same file. To prevent this, add a dependency to force compilation in a sequential manner. Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/Makefile b/meta-luv/recipes-core/umip/files/Makefile index 4b980f0bab1..4d62c75d6a3 100644 --- a/meta-luv/recipes-core/umip/files/Makefile +++ b/meta-luv/recipes-core/umip/files/Makefile @@ -11,7 +11,7 @@ x86_64_minimal: $(CC) $(CFLAGS) -o umip_ldt_64 test_umip_ldt_64.o umip_ldt_64.o umip_utils_64.o x86_64_emul_all: CFLAGS += -DEMULATE_ALL -x86_64_emul_all: +x86_64_emul_all: x86_64_minimal $(CC) $(CFLAGS) -c umip_utils.c -o umip_utils_64.o $(CC) $(CFLAGS) -o umip_test_opnds_64_emul_all umip_utils_64.o umip_test_opnds.c @@ -43,7 +43,7 @@ i686_minimal: $(CC) $(CFLAGS) -o umip_ldt_16 test_umip_ldt_16.o umip_ldt_16.o umip_utils_32.o i686_emul_all: CFLAGS += -DEMULATE_ALL -i686_emul_all: +i686_emul_all: i686_minimal $(CC) $(CFLAGS) -c umip_utils.c -o umip_utils_32.o $(CC) $(CFLAGS) -o umip_test_opnds_32_emul_all umip_utils_32.o umip_test_opnds.c From 31e0960e805975e672cb6207309f86f15438bb3c Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Tue, 8 Aug 2017 11:42:04 -0700 Subject: [PATCH 077/109] umip: install the _emul_all variants It is convenient to have the _emul_all variants of tests programs. These variants allow to test emulation for all the UMIP-protected instructions as well as for both 64 and 32 bit processes. Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/umip-tests_0.1.bb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/meta-luv/recipes-core/umip/umip-tests_0.1.bb b/meta-luv/recipes-core/umip/umip-tests_0.1.bb index 5a13c01af5a..6900182085e 100644 --- a/meta-luv/recipes-core/umip/umip-tests_0.1.bb +++ b/meta-luv/recipes-core/umip/umip-tests_0.1.bb @@ -40,6 +40,10 @@ do_install() { install -m 755 ${WORKDIR}/umip_exceptions_64 ${D}${bindir} install -m 755 ${WORKDIR}/umip_test_opnds_64 ${D}${bindir} install -m 755 ${WORKDIR}/umip_ldt_64 ${D}${bindir} + install -m 755 ${WORKDIR}/umip_test_basic_64_emul_all ${D}${bindir} + install -m 755 ${WORKDIR}/umip_exceptions_64_emul_all ${D}${bindir} + install -m 755 ${WORKDIR}/umip_test_opnds_64_emul_all ${D}${bindir} + install -m 755 ${WORKDIR}/umip_ldt_64_emul_all ${D}${bindir} fi if [ "${TARGET_ARCH}" = "i586" ]; then install -m 755 ${WORKDIR}/umip_test_basic_32 ${D}${bindir} @@ -47,6 +51,11 @@ do_install() { install -m 755 ${WORKDIR}/umip_test_opnds_32 ${D}${bindir} install -m 755 ${WORKDIR}/umip_ldt_32 ${D}${bindir} install -m 755 ${WORKDIR}/umip_ldt_16 ${D}${bindir} + install -m 755 ${WORKDIR}/umip_test_basic_32_emul_all ${D}${bindir} + install -m 755 ${WORKDIR}/umip_exceptions_32_emul_all ${D}${bindir} + install -m 755 ${WORKDIR}/umip_test_opnds_32_emul_all ${D}${bindir} + install -m 755 ${WORKDIR}/umip_ldt_32_emul_all ${D}${bindir} + install -m 755 ${WORKDIR}/umip_ldt_16_emul_all ${D}${bindir} fi if [ "${TARGET_ARCH}" = "i686" ]; then install -m 755 ${WORKDIR}/umip_test_basic_32 ${D}${bindir} @@ -54,6 +63,11 @@ do_install() { install -m 755 ${WORKDIR}/umip_test_opnds_32 ${D}${bindir} install -m 755 ${WORKDIR}/umip_ldt_32 ${D}${bindir} install -m 755 ${WORKDIR}/umip_ldt_16 ${D}${bindir} + install -m 755 ${WORKDIR}/umip_test_basic_32_emul_all ${D}${bindir} + install -m 755 ${WORKDIR}/umip_exceptions_32_emul_all ${D}${bindir} + install -m 755 ${WORKDIR}/umip_test_opnds_32_emul_all ${D}${bindir} + install -m 755 ${WORKDIR}/umip_ldt_32_emul_all ${D}${bindir} + install -m 755 ${WORKDIR}/umip_ldt_16_emul_all ${D}${bindir} fi } From 738a6e6818f050f8405aaebaed9ec40b4af1cebf Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Thu, 10 Aug 2017 15:44:42 -0700 Subject: [PATCH 078/109] umip: umip_utils: improve signal handling Modify signal handling function to prevent or allow further test case validation. If a signal was expected and none was received, the test case should fails. Likewise, if a signal was expected but the signal number and/or signal code does not match what was expected, the case fails. However, if a signal was received and it matches the expected signal the test case passes. Conversely, if a signal not expected but one is received, the test case fails. In all the aforementioned cases there is no need to inspect the instructions' result. Only when a signal was not expect and none is received the instructions' results are relevant. The function inspect_signal is updated to reflect the described behavior. Also, the return value is used to indicate whether further results inspection is needed. Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/umip_utils.c | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/meta-luv/recipes-core/umip/files/umip_utils.c b/meta-luv/recipes-core/umip/files/umip_utils.c index a8108c864c7..3aa64f2cb55 100644 --- a/meta-luv/recipes-core/umip/files/umip_utils.c +++ b/meta-luv/recipes-core/umip/files/umip_utils.c @@ -19,6 +19,7 @@ void print_results(void) test_passed, test_failed, test_errors); } +#if 0 int inspect_signal(int exp_signum, int exp_sigcode) { if (got_signal == exp_signum && got_sigcode == exp_sigcode) { @@ -34,6 +35,51 @@ int inspect_signal(int exp_signum, int exp_sigcode) return 1; } } +#else + +/** + * Inspect the contents of exp_signum and exp_sigcode to determine if they match + * the received got_signal signal and signal code, if any. A return value of + * 1 means that the test case is complete and no further action is needed (e.g., + * examine values returned by instructions. A return value of 0 means that signal + * processing is not relevant for the caller can proceed with further test case + * validation. + */ +int inspect_signal(exp_signum, exp_sigcode) +{ + /* If we expect signal, make sure it is the one we expect. */ + if (exp_signum) { + /* A signal was received, examine it */ + if (got_signal == exp_signum) { + if (got_sigcode == exp_sigcode) { + /* All is good. Test case is complete. */ + pr_pass(test_passed, "Received expected signal and code.\n"); + return 1; + } else { + pr_fail(test_failed, "Received wrong signal code. Expected si_code[%d].\n", exp_sigcode); + return 1; + } + } else { + if (got_signal) { + /* Wrong signal */ + pr_fail(test_failed, "Received wrong signal. Expected [%d]\n", exp_signum); + return 1; + } else { + /* signal did not come */ + pr_fail(test_failed, "A signal [%d] was expected. None was not received.\n", exp_signum); + return 1; + } + } + } else { /* If no signal is expected, make sure we did not receive one */ + if (got_signal) { + pr_fail(test_failed, "Received unexpected signal.\n"); + return 1; + } else { /* Signal is not relevant.*/ + return 0; + } + } +} +#endif void signal_handler(int signum, siginfo_t *info, void *ctx_void) { From d0804b8fb28e9d61b1609f882c006e8f559c18f7 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Thu, 10 Aug 2017 16:48:14 -0700 Subject: [PATCH 079/109] umip: umip_test_opnds: correct local variable access due to unbalanced %(r|e)sp Assembly test code must handle the stack pointer carefull so that it has the same value it has before entering the test code. This is important as we handle several automatic variables. Some compilers reference local variables as offsets from the base pointer; others do it as offsets from the stack pointer. This patch focuses on making sure the stack pointer is preserved. We need not to worry about the case when the base pointer is the test register; the test code already restores the test regiter after the test. Signed-off-by: Ricardo Neri --- .../recipes-core/umip/files/umip_test_opnds.c | 97 ++++++++++++++----- 1 file changed, 75 insertions(+), 22 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/umip_test_opnds.c b/meta-luv/recipes-core/umip/files/umip_test_opnds.c index 0888a18d15b..60c1b61dbe5 100644 --- a/meta-luv/recipes-core/umip/files/umip_test_opnds.c +++ b/meta-luv/recipes-core/umip/files/umip_test_opnds.c @@ -118,30 +118,83 @@ /* Memory operands */ #if __x86_64__ #define INSNmem(insn, reg, disp) \ - "push %%rax\n" /* rax used to copy values around. Make a copy */ \ - "mov %0, %%rax\n" /* Init our variable. Split into two instructions */ \ - "mov %%rax, "disp"(%%rsp)\n" \ - "mov %%"reg", %%rax\n" /* make a backup of register under test */ \ - "mov %%rsp, %%"reg"\n" /* move rsp to our register under test */ \ - insn" "disp"(%%"reg")\n" /* execute our instruction */ \ - NOP_SLED \ - "push "disp"(%%"reg")\n" /* save result to stack */ \ - "mov %%rax, %%"reg"\n" /* restore register under test */ \ - "pop %1\n" /* copy result to our variable */ \ - "pop %%rax\n" /* restore rax */ + /* + * Move initialization value to %eax. Do it before changing %esp as some\ + * compilers refer local variables as an offset from %esp. This code \ + * causes %eax to be clobbered. This is OK as long it is not used in \ + * the caller function. \ + */ \ + "mov %0, %%rax\n" \ + /* \ + * Make a backup of our scratch memory. Adjust wrt %rsp according to the\ + * two stack pushes below. \ + */ \ + "push ("disp"-0x8-0x8)(%%rsp)\n" \ + /* Make a backup of contents of test register */ \ + "push %%"reg"\n" \ + /* Write our initialization value in scratch memory. */ \ + "mov %%rax, "disp"(%%rsp)\n" \ + /* Make test register point to our scratch memory. */ \ + "mov %%rsp, %%"reg"\n" \ + /* Run our test: register-indirect addressing with an offset. */ \ + insn" "disp"(%%"reg")\n" \ + NOP_SLED \ + /* Save result to %rax */ \ + "mov "disp"(%%rsp), %%rax\n" \ + /* Restore test register */ \ + "pop %%"reg"\n" \ + /* \ + * Restore scratch memory. Adjust offset wrt %rsp according to the \ + * two stack pops above \ + */ \ + "pop ("disp"-0x8-0x8)(%%rsp)\n" \ + /* \ + * Since some compilers refer local variables as an offset from %esp, \ + * the test result can only be saved to a local variable only once %esp \ + * has been restored by an equal number of stack pops and pushes. \ + */ \ + "mov %%rax, %0\n" #else #define INSNmem(insn, reg, disp) \ - "push %%eax\n" /* eax used to copy values around. Make a copy */ \ - "mov %0, %%eax\n" /* Init our variable. Split into two instructions */ \ - "mov %%eax, "disp"(%%esp)\n" \ - "mov %%"reg", %%eax\n" /* make a backup of register under test */ \ - "mov %%esp, %%"reg"\n" /* move esp to our register under test */ \ - insn" "disp"(%%"reg")\n" /* execute our instruction */ \ - NOP_SLED \ - "push "disp"(%%"reg")\n" /* save result to stack */ \ - "mov %%eax, %%"reg"\n" /* restore register under test */ \ - "pop %1\n" /* copy result to our variable */ \ - "pop %%eax\n" /* restore eax */ + /* + * Move initialization value to %eax. Do it before changing %esp as some\ + * compilers refer local variables as an offset from %esp. This code \ + * causes %eax to be clobbered. This is OK as long it is not used in \ + * the caller function. \ + */ \ + "mov %0, %%eax\n" \ + /* \ + * Make a backup of our scratch memory. Adjust wrt %rsp according to the\ + * two stack pushes below. \ + */ \ + "push ("disp"-0x4-0x4)(%%esp)\n" \ + /* Make a backup of contents of test register */ \ + "push %%"reg"\n" \ + /* \ + * Write our initialization value in scratch memory. Adjust offset \ + * according to the number of previous stack pushes. \ + */ \ + "mov %%eax, "disp"(%%esp)\n" \ + /* Make test register point to our scratch memory. */ \ + "mov %%esp, %%"reg"\n" \ + /* Run our test: register-indirect addressing with an offset. */ \ + insn" "disp"(%%"reg")\n" \ + NOP_SLED \ + /* Save result to %eax */ \ + "mov "disp"(%%esp), %%eax\n" \ + /* Restore test register */ \ + "pop %%"reg"\n" \ + /* \ + * Restore scratch memory. Adjust offset wrt %rsp according to the \ + * two stack pops above \ + */ \ + "pop ("disp"-0x4-0x4)(%%esp)\n" \ + /* \ + * Since some compilers refer local variables as an offset from %esp, \ + * the test result can only be saved to a local variable only once %esp \ + * has been restored by an equal number of stack pops and pushes. \ + */ \ + "mov %%eax, %0\n" #endif /* From eb44f402e6bf56278e01b9b18831f93ece879f69 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Thu, 10 Aug 2017 17:51:04 -0700 Subject: [PATCH 080/109] umip: umip_test_opnd: use a scratch register in memory tests We were using %(r|e)ax register as a scratch register for register-indirect addressing tests. The scratch register is used to copy the test result into a local variable. However, the test register is restored at the end of the test before saving the test result. This means that when %(r|e)ax is the test register, its value is retored at the end of the test and we lose the test result. Hence, add the ability to specify which register is used as scratch. When using the macro, we use mostly registers %(r|e)ax, %(r|e)cx and %(r|e)dx as scratch registers. These registers do no need to be preserved in the System V calling convention. Also, our function is simple enough to not use these registers to operate. Signed-off-by: Ricardo Neri --- .../recipes-core/umip/files/umip_test_opnds.c | 94 ++++++++++--------- 1 file changed, 49 insertions(+), 45 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/umip_test_opnds.c b/meta-luv/recipes-core/umip/files/umip_test_opnds.c index 60c1b61dbe5..7592c348f51 100644 --- a/meta-luv/recipes-core/umip/files/umip_test_opnds.c +++ b/meta-luv/recipes-core/umip/files/umip_test_opnds.c @@ -117,14 +117,14 @@ /* Memory operands */ #if __x86_64__ -#define INSNmem(insn, reg, disp) \ +#define INSNmem(insn, reg, scratch, disp) \ /* - * Move initialization value to %eax. Do it before changing %esp as some\ - * compilers refer local variables as an offset from %esp. This code \ - * causes %eax to be clobbered. This is OK as long it is not used in \ - * the caller function. \ + * Move initialization value to %scratch. Do it before changing %rsp as \ + * some compilers refer local variables as an offset from %esp. This \ + * code causes %scratch to be clobbered. This is OK as long it is not \ + * used in the caller function. \ */ \ - "mov %0, %%rax\n" \ + "mov %0, %%"scratch"\n" \ /* \ * Make a backup of our scratch memory. Adjust wrt %rsp according to the\ * two stack pushes below. \ @@ -133,14 +133,14 @@ /* Make a backup of contents of test register */ \ "push %%"reg"\n" \ /* Write our initialization value in scratch memory. */ \ - "mov %%rax, "disp"(%%rsp)\n" \ + "mov %%"scratch", "disp"(%%rsp)\n" \ /* Make test register point to our scratch memory. */ \ "mov %%rsp, %%"reg"\n" \ /* Run our test: register-indirect addressing with an offset. */ \ insn" "disp"(%%"reg")\n" \ NOP_SLED \ /* Save result to %rax */ \ - "mov "disp"(%%rsp), %%rax\n" \ + "mov "disp"(%%rsp), %%"scratch"\n" \ /* Restore test register */ \ "pop %%"reg"\n" \ /* \ @@ -153,16 +153,16 @@ * the test result can only be saved to a local variable only once %esp \ * has been restored by an equal number of stack pops and pushes. \ */ \ - "mov %%rax, %0\n" + "mov %%"scratch", %0\n" #else -#define INSNmem(insn, reg, disp) \ +#define INSNmem(insn, reg, scratch, disp) \ /* - * Move initialization value to %eax. Do it before changing %esp as some\ - * compilers refer local variables as an offset from %esp. This code \ - * causes %eax to be clobbered. This is OK as long it is not used in \ - * the caller function. \ + * Move initialization value to %scratch. Do it before changing %esp as \ + * some compilers refer local variables as an offset from %esp. This \ + * code causes %scratch to be clobbered. This is OK as long it is not \ + * used in the caller function. \ */ \ - "mov %0, %%eax\n" \ + "mov %0, %%"scratch"\n" \ /* \ * Make a backup of our scratch memory. Adjust wrt %rsp according to the\ * two stack pushes below. \ @@ -174,14 +174,14 @@ * Write our initialization value in scratch memory. Adjust offset \ * according to the number of previous stack pushes. \ */ \ - "mov %%eax, "disp"(%%esp)\n" \ + "mov %%"scratch", "disp"(%%esp)\n" \ /* Make test register point to our scratch memory. */ \ "mov %%esp, %%"reg"\n" \ /* Run our test: register-indirect addressing with an offset. */ \ insn" "disp"(%%"reg")\n" \ NOP_SLED \ /* Save result to %eax */ \ - "mov "disp"(%%esp), %%eax\n" \ + "mov "disp"(%%esp), %%"scratch"\n" \ /* Restore test register */ \ "pop %%"reg"\n" \ /* \ @@ -194,7 +194,7 @@ * the test result can only be saved to a local variable only once %esp \ * has been restored by an equal number of stack pops and pushes. \ */ \ - "mov %%eax, %0\n" + "mov %%"scratch", %0\n" #endif /* @@ -202,10 +202,10 @@ * Instead, it uses -0x0(%%reg). No displacement is OK unless the SIB byte * is used with RBP. */ -#define INSNmemdisp8(insn, reg) INSNmem(insn, reg, "-0x80") -#define INSNmemdisp32(insn, reg) INSNmem(insn, reg, "-0x1000") +#define INSNmemdisp8(insn, reg, scratch) INSNmem(insn, reg, scratch, "-0x80") +#define INSNmemdisp32(insn, reg, scratch) INSNmem(insn, reg, scratch, "-0x1000") -#define CHECK_INSNmemdisp(INSNmacro, insn, reg, val, init, exp) \ +#define CHECK_INSNmemdisp(INSNmacro, insn, reg, scratch, val, init, exp) \ val = init; \ /* Memory operands are always treated as 16-bit locations */ \ mask = get_mask(16); \ @@ -215,7 +215,7 @@ got_signal = 0; \ got_sigcode = 0; \ \ - asm volatile(INSNmacro(insn, reg) : "=m" (val): "m"(val) : "%rax"); \ + asm volatile(INSNmacro(insn, reg, scratch) : "=m" (val): "m"(val) : "%"scratch""); \ \ if(!inspect_signal(exp_signum, exp_sigcode)) { \ /* \ @@ -232,34 +232,38 @@ }\ } -#define CHECK_INSNmem(insn, reg, val, init, exp) \ - CHECK_INSNmemdisp(INSNmemdisp8, insn, reg, val, init, exp) \ - CHECK_INSNmemdisp(INSNmemdisp32, insn, reg, val, init, exp) +#define CHECK_INSNmem(insn, reg, scratch, val, init, exp) \ + CHECK_INSNmemdisp(INSNmemdisp8, insn, reg, scratch, val, init, exp) \ + CHECK_INSNmemdisp(INSNmemdisp32, insn, reg, scratch, val, init, exp) #if __x86_64__ #define CHECK_ALLmem(insn, val, init, exp) \ - CHECK_INSNmem(insn, "rax", val, init, exp) \ - CHECK_INSNmem(insn, "rcx", val, init, exp) \ - CHECK_INSNmem(insn, "rdx", val, init, exp) \ - CHECK_INSNmem(insn, "rbp", val, init, exp) \ - CHECK_INSNmem(insn, "rsi", val, init, exp) \ - CHECK_INSNmem(insn, "rdi", val, init, exp) \ - CHECK_INSNmem(insn, "r8", val, init, exp) \ - CHECK_INSNmem(insn, "r9", val, init, exp) \ - CHECK_INSNmem(insn, "r10", val, init, exp) \ - CHECK_INSNmem(insn, "r11", val, init, exp) \ - CHECK_INSNmem(insn, "r12", val, init, exp) \ - CHECK_INSNmem(insn, "r13", val, init, exp) \ - CHECK_INSNmem(insn, "r14", val, init, exp) \ - CHECK_INSNmem(insn, "r15", val, init, exp) + CHECK_INSNmem(insn, "rax", "rcx", val, init, exp) \ + CHECK_INSNmem(insn, "rcx", "rax", val, init, exp) \ + CHECK_INSNmem(insn, "rdx", "rax", val, init, exp) \ + CHECK_INSNmem(insn, "rbx", "rax", val, init, exp) \ + CHECK_INSNmem(insn, "rsp", "rax", val, init, exp) \ + CHECK_INSNmem(insn, "rbp", "rax", val, init, exp) \ + CHECK_INSNmem(insn, "rsi", "rax", val, init, exp) \ + CHECK_INSNmem(insn, "rdi", "rax", val, init, exp) \ + CHECK_INSNmem(insn, "r8", "rax", val, init, exp) \ + CHECK_INSNmem(insn, "r9", "rax", val, init, exp) \ + CHECK_INSNmem(insn, "r10", "rax", val, init, exp) \ + CHECK_INSNmem(insn, "r11", "rax", val, init, exp) \ + CHECK_INSNmem(insn, "r12", "rax", val, init, exp) \ + CHECK_INSNmem(insn, "r13", "rax", val, init, exp) \ + CHECK_INSNmem(insn, "r14", "rax", val, init, exp) \ + CHECK_INSNmem(insn, "r15", "rax", val, init, exp) #else #define CHECK_ALLmem(insn, val, init, exp) \ - CHECK_INSNmem(insn, "eax", val, init, exp) \ - CHECK_INSNmem(insn, "ecx", val, init, exp) \ - CHECK_INSNmem(insn, "edx", val, init, exp) \ - CHECK_INSNmem(insn, "ebp", val, init, exp) \ - CHECK_INSNmem(insn, "esi", val, init, exp) \ - CHECK_INSNmem(insn, "edi", val, init, exp) + CHECK_INSNmem(insn, "eax", "ecx", val, init, exp) \ + CHECK_INSNmem(insn, "ecx", "eax", val, init, exp) \ + CHECK_INSNmem(insn, "edx", "eax", val, init, exp) \ + CHECK_INSNmem(insn, "ebx", "eax", val, init, exp) \ + CHECK_INSNmem(insn, "esp", "eax", val, init, exp) \ + CHECK_INSNmem(insn, "ebp", "eax", val, init, exp) \ + CHECK_INSNmem(insn, "esi", "eax", val, init, exp) \ + CHECK_INSNmem(insn, "edi", "eax", val, init, exp) #endif #define INIT_SS INIT_VAL(13131313) From 43385b38215b7fd17ff9a977d36baf66c8220567 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Fri, 11 Aug 2017 14:13:30 -0700 Subject: [PATCH 081/109] umip: umip_test_opnds: correct local variable access due to unbalanced %(r|e)sp Assembly test code must handle the stack pointer carefully so that it has the same value it had before entering the test code. This is important as we handle several automatic variables. Some compilers reference local variables as offsets from the base pointer; others do it as offsets from the stack pointer. This patch focuses on making sure the stack pointer is preserved. We need not to worry about the case when the base pointer is the test register; the test code already restores the test regiter after the test. When testing instruction that use registers as operands, two scratch registers are needed. The first scratch register is used to initialize the test register and to retrieve the test result. The second one is used solely to restor the stack pointer. This is needed in case the register under test is such register. After testing we must immediately restore it in order to keep our stack operations as well as our references to local variables correct. Signed-off-by: Ricardo Neri --- .../recipes-core/umip/files/umip_test_opnds.c | 186 ++++++++++++------ 1 file changed, 125 insertions(+), 61 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/umip_test_opnds.c b/meta-luv/recipes-core/umip/files/umip_test_opnds.c index 7592c348f51..eb99d7d8b28 100644 --- a/meta-luv/recipes-core/umip/files/umip_test_opnds.c +++ b/meta-luv/recipes-core/umip/files/umip_test_opnds.c @@ -9,42 +9,106 @@ #if __x86_64__ #define PUSH(reg) "push %%r"reg"\n" #define POP(reg) "pop %%r"reg"\n" +#define SAVE_SP(reg) "mov %%rsp, %%r"reg"\n" +#define RESTORE_SP(reg) "mov %%r"reg", %%rsp\n" #else #define PUSH(reg) "push %%e"reg"\n" #define POP(reg) "pop %%e"reg"\n" +#define SAVE_SP(reg) "mov %%esp, %%e"reg"\n" +#define RESTORE_SP(reg) "mov %%e"reg", %%esp\n" #endif -#define INSNreg16(insn, reg, aux) \ - PUSH(aux) \ - PUSH(reg) \ - insn" %%"reg"\n" \ - NOP_SLED \ - "mov %%"reg", %%"aux"\n" \ - POP(reg) \ - "mov %%"aux", %0\n" \ - POP(aux) - -#define INSNreg32(insn, reg, aux) \ - PUSH(aux) \ - PUSH(reg) \ - insn" %%e"reg"\n" \ - NOP_SLED \ - "mov %%e"reg", %%e"aux"\n" \ - POP(reg) \ - "movl %%e"aux", %0\n" \ - POP(aux) - -#define INSNreg64(insn, reg, aux) \ - "push %%"aux"\n" \ - "push %%"reg"\n" \ - insn" %%"reg"\n" \ - NOP_SLED \ - "mov %%"reg", %%"aux"\n" \ - "pop %%"reg"\n" \ - "mov %%"aux", %0\n" \ - "pop %%"aux"\n" - -#define CHECK_INSN(op_size, insn, reg, val, aux, init, exp) \ +#define INSNreg16(insn, reg, scratch, scratch_sp) \ + /* + * Move initialization value to %scratch. Do it before changing %rsp as \ + * some compilers refer local variables as an offset from %esp. This \ + * code causes %scratch to be clobbered. This is OK as long it is not \ + * used in the caller function. \ + */ \ + "mov %0, %%"scratch"\n" \ + /* Make a backup of test register */ \ + PUSH(reg) \ + /* Write our initialization value in test register */ \ + "mov %%"scratch", %%"reg"\n" \ + /* In case we are testing %esp, make a backup to restore after test */ \ + SAVE_SP(scratch_sp) \ + /* Run our test: register operands */ \ + insn" %%"reg"\n" \ + NOP_SLED \ + /* Save result to scratch */ \ + "mov %%"reg", %%"scratch"\n" \ + /* Restore %esp */ \ + RESTORE_SP(scratch_sp) \ + /* Restore test register */ \ + POP(reg) \ + /* \ + * Since some compilers refer local variables as an offset from %esp, \ + * the test result can only be saved to a local variable only once %esp \ + * has been restored by an equal number of stack pops and pushes. \ + */ \ + "mov %%"scratch", %1\n" + +#define INSNreg32(insn, reg, scratch, scratch_sp) \ + /* + * Move initialization value to %scratch. Do it before changing %rsp as \ + * some compilers refer local variables as an offset from %esp. This \ + * code causes %scratch to be clobbered. This is OK as long it is not \ + * used in the caller function. \ + */ \ + "mov %0, %%e"scratch"\n" \ + /* Make a backup of test register */ \ + PUSH(reg) \ + /* Write our initialization value in test register */ \ + "mov %%e"scratch", %%e"reg"\n" \ + /* In case we are testing %esp, make a backup to restore after test */ \ + SAVE_SP(scratch_sp) \ + /* Run our test: register operands */ \ + insn" %%e"reg"\n" \ + NOP_SLED \ + /* Save result to scratch */ \ + "mov %%e"reg", %%e"scratch"\n" \ + /* Restore %esp */ \ + RESTORE_SP(scratch_sp) \ + /* Restore test register */ \ + POP(reg) \ + /* \ + * Since some compilers refer local variables as an offset from %esp, \ + * the test result can only be saved to a local variable only once %esp \ + * has been restored by an equal number of stack pops and pushes. \ + */ \ + "mov %%e"scratch", %1\n" + +#define INSNreg64(insn, reg, scratch, scratch_sp) \ + /* + * Move initialization value to %scratch. Do it before changing %rsp as \ + * some compilers refer local variables as an offset from %esp. This \ + * code causes %scratch to be clobbered. This is OK as long it is not \ + * used in the caller function. \ + */ \ + "mov %0, %%"scratch"\n" \ + /* Make a backup of test register */ \ + "push %%"reg"\n" \ + /* Write our initialization value in test register */ \ + "mov %%"scratch", %%"reg"\n" \ + /* In case we are testing %esp, make a backup to restore after test */ \ + "mov %%rsp, %%"scratch_sp"\n" \ + /* Run our test: register operands */ \ + insn" %%"reg"\n" \ + NOP_SLED \ + /* Save result to scratch */ \ + "mov %%"reg", %%"scratch"\n" \ + /* Restore %rsp */ \ + "mov %%"scratch_sp", %%rsp\n" \ + /* Restore test register */ \ + "pop %%"reg"\n" \ + /* \ + * Since some compilers refer local variables as an offset from %rsp, \ + * the test result can only be saved to a local variable only once %rsp \ + * has been restored by an equal number of stack pops and pushes. \ + */ \ + "mov %%"scratch", %1\n" + +#define CHECK_INSN(op_size, insn, reg, val, scratch, scratch_sp, init, exp) \ do { \ val = init; \ mask = get_mask(op_size); \ @@ -54,7 +118,7 @@ got_signal = 0; \ got_sigcode = 0; \ \ - asm volatile(INSNreg##op_size(insn, reg, aux) : "=m" (val)); \ + asm volatile(INSNreg##op_size(insn, reg, scratch, scratch_sp) : "=m" (val) : "m" (val): "%"scratch, "%"scratch_sp ); \ \ if(inspect_signal(exp_signum, exp_sigcode)) \ break; \ @@ -73,37 +137,37 @@ } while(0); #define CHECK_ALLreg32(insn, val, init, exp) \ - CHECK_INSN(16, insn, "ax", val, "bx", init, exp); \ - CHECK_INSN(16, insn, "cx", val, "ax", init, exp); \ - CHECK_INSN(16, insn, "dx", val, "ax", init, exp); \ - CHECK_INSN(16, insn, "bx", val, "ax", init, exp); \ - CHECK_INSN(16, insn, "bp", val, "ax", init, exp); \ - CHECK_INSN(16, insn, "si", val, "ax", init, exp); \ - CHECK_INSN(16, insn, "di", val, "ax", init, exp); \ - CHECK_INSN(32, insn, "ax", val, "bx", init, exp); \ - CHECK_INSN(32, insn, "cx", val, "ax", init, exp); \ - CHECK_INSN(32, insn, "dx", val, "ax", init ,exp); \ - CHECK_INSN(32, insn, "bx", val, "ax", init ,exp); \ - CHECK_INSN(32, insn, "bp", val, "ax", init, exp);\ - CHECK_INSN(32, insn, "si", val, "ax", init, exp); \ - CHECK_INSN(32, insn, "di", val, "ax", init, exp); \ + CHECK_INSN(16, insn, "ax", val, "cx", "dx", init, exp); \ + CHECK_INSN(16, insn, "cx", val, "ax", "dx", init, exp); \ + CHECK_INSN(16, insn, "dx", val, "ax", "cx", init, exp); \ + CHECK_INSN(16, insn, "bx", val, "ax", "dx", init, exp); \ + CHECK_INSN(16, insn, "bp", val, "ax", "dx", init, exp); \ + CHECK_INSN(16, insn, "si", val, "ax", "dx", init, exp); \ + CHECK_INSN(16, insn, "di", val, "ax", "dx", init, exp); \ + CHECK_INSN(32, insn, "ax", val, "cx", "dx", init, exp); \ + CHECK_INSN(32, insn, "cx", val, "ax", "dx", init, exp); \ + CHECK_INSN(32, insn, "dx", val, "ax", "cx", init ,exp); \ + CHECK_INSN(32, insn, "bx", val, "ax", "dx", init ,exp); \ + CHECK_INSN(32, insn, "bp", val, "ax", "dx", init, exp); \ + CHECK_INSN(32, insn, "si", val, "ax", "dx", init, exp); \ + CHECK_INSN(32, insn, "di", val, "ax", "dx", init, exp); #define CHECK_ALLreg64(insn, val, init, exp) \ - CHECK_INSN(64, insn, "rax", val, "rbx", init, exp); \ - CHECK_INSN(64, insn, "rcx", val, "rax", init, exp); \ - CHECK_INSN(64, insn, "rdx", val, "rax", init, exp); \ - CHECK_INSN(64, insn, "rbx", val, "rax", init, exp); \ - CHECK_INSN(64, insn, "rbp", val, "rax", init, exp); \ - CHECK_INSN(64, insn, "rsi", val, "rax", init, exp); \ - CHECK_INSN(64, insn, "rdi", val, "rax", init, exp); \ - CHECK_INSN(64, insn, "r8", val, "rax", init, exp); \ - CHECK_INSN(64, insn, "r9", val, "rax", init, exp); \ - CHECK_INSN(64, insn, "r10", val, "rax", init, exp); \ - CHECK_INSN(64, insn, "r11", val, "rax", init, exp); \ - CHECK_INSN(64, insn, "r12", val, "rax", init, exp); \ - CHECK_INSN(64, insn, "r13", val, "rax", init, exp); \ - CHECK_INSN(64, insn, "r14", val, "rax", init, exp); \ - CHECK_INSN(64, insn, "r15", val, "rax", init, exp); + CHECK_INSN(64, insn, "rax", val, "rcx", "rdx", init, exp); \ + CHECK_INSN(64, insn, "rcx", val, "rax", "rdx", init, exp); \ + CHECK_INSN(64, insn, "rdx", val, "rax", "rcx", init, exp); \ + CHECK_INSN(64, insn, "rbx", val, "rax", "rdx", init, exp); \ + CHECK_INSN(64, insn, "rbp", val, "rax", "rdx", init, exp); \ + CHECK_INSN(64, insn, "rsi", val, "rax", "rdx", init, exp); \ + CHECK_INSN(64, insn, "rdi", val, "rax", "rdx", init, exp); \ + CHECK_INSN(64, insn, "r8", val, "rax", "rdx", init, exp); \ + CHECK_INSN(64, insn, "r9", val, "rax", "rdx", init, exp); \ + CHECK_INSN(64, insn, "r10", val, "rax", "rdx", init, exp); \ + CHECK_INSN(64, insn, "r11", val, "rax", "rdx", init, exp); \ + CHECK_INSN(64, insn, "r12", val, "rax", "rdx", init, exp); \ + CHECK_INSN(64, insn, "r13", val, "rax", "rdx", init, exp); \ + CHECK_INSN(64, insn, "r14", val, "rax", "rdx", init, exp); \ + CHECK_INSN(64, insn, "r15", val, "rax", "rdx", init, exp); #if __x86_64__ #define CHECK_ALLreg(insn, val, init, exp) \ From 91e516c43aac235ac44ef5eea3d8dca0406e45ae Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Fri, 11 Aug 2017 14:31:12 -0700 Subject: [PATCH 082/109] umip: umip_test_opnds: print test results with the correct size In order to have a readable test output, test results are prefixed with a zeros so that they reflect the size of the data being printed. However, in 32-bit builds, it does not make sense to print numbers of 16 digits. Hence, only print as many as prepend zeros as needed. Signed-off-by: Ricardo Neri --- .../recipes-core/umip/files/umip_test_opnds.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/umip_test_opnds.c b/meta-luv/recipes-core/umip/files/umip_test_opnds.c index eb99d7d8b28..292b7fe7a3d 100644 --- a/meta-luv/recipes-core/umip/files/umip_test_opnds.c +++ b/meta-luv/recipes-core/umip/files/umip_test_opnds.c @@ -11,11 +11,13 @@ #define POP(reg) "pop %%r"reg"\n" #define SAVE_SP(reg) "mov %%rsp, %%r"reg"\n" #define RESTORE_SP(reg) "mov %%r"reg", %%rsp\n" +#define PFX_ZEROS "16" #else #define PUSH(reg) "push %%e"reg"\n" #define POP(reg) "pop %%e"reg"\n" #define SAVE_SP(reg) "mov %%esp, %%e"reg"\n" #define RESTORE_SP(reg) "mov %%e"reg", %%esp\n" +#define PFX_ZEROS "8" #endif #define INSNreg16(insn, reg, scratch, scratch_sp) \ @@ -128,10 +130,14 @@ * does not change. \ */ \ if (((val & mask) == (exp & mask)) && ((exp & ~mask) == (exp & ~mask))) \ - pr_pass(test_passed, " On %s-bit '%s %s'! Got [0x%016lx] Exp[0x%016lx]\n", \ + pr_pass(test_passed, " On %s-bit '%s %s'! " \ + "Got [0x%0"PFX_ZEROS"lx] " \ + "Exp[0x%0"PFX_ZEROS"lx]\n", \ #op_size, insn, reg, val, (val&mask) | (init&~mask)); \ else { \ - pr_fail(test_failed, "On %s-bit '%s %s'! Got[0x%016lx] Exp[0x%016lx]\n", \ + pr_fail(test_failed, " On %s-bit '%s %s'! " \ + "Got[0x%0"PFX_ZEROS"lx] " \ + "Exp[0x%0"PFX_ZEROS"lx]\n", \ #op_size, insn, reg, val, (val&mask) | (init&~mask)); \ } \ } while(0); @@ -288,10 +294,14 @@ * does not change. \ */ \ if (((val & mask) == (exp & mask)) && ((exp & ~mask) == (exp & ~mask))) \ - pr_pass(test_passed, "On '%s %s(%s)'! Got [0x%016lx] Exp[0x%016lx]\n", \ + pr_pass(test_passed, "On '%s %s(%s)'! " \ + "Got [0x%0"PFX_ZEROS"lx] " \ + "Exp[0x%0"PFX_ZEROS"lx]\n", \ insn, #INSNmacro, reg, val, (exp & mask) | (init & ~mask)); \ else { \ - pr_fail(test_failed, "On '%s %s(%s)'! Got[0x%016lx] Exp[0x%016lx]\n", \ + pr_fail(test_failed, "On '%s %s(%s)'! " \ + "Got[0x%0"PFX_ZEROS"lx] " \ + "Exp[0x%0"PFX_ZEROS"lx]\n", \ insn, #INSNmacro, reg, val, (exp & mask) | (init & ~mask)); \ }\ } From 9f7dbbc5cb59c24c69cfe9d26dcd9345bb51eeee Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Fri, 11 Aug 2017 17:05:46 -0700 Subject: [PATCH 083/109] umip: umip_test_opnds: print initialization value Print the value with which test registers and memory are initialized. This helps to verify that the portion of the test register/memory holds a test result of correct size. This is: * Only 2 bytes for memory operands * Only 2 bytes for register operands such as %ax, %cx, %dx, ... * Only 4 bytes for register operands such as %eax, %ecx, %edx, ... * Only 8 bytes for register operands such as %rax, %rcx, %rdx, ... Signed-off-by: Ricardo Neri --- .../recipes-core/umip/files/umip_test_opnds.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/umip_test_opnds.c b/meta-luv/recipes-core/umip/files/umip_test_opnds.c index 292b7fe7a3d..a7782e408ce 100644 --- a/meta-luv/recipes-core/umip/files/umip_test_opnds.c +++ b/meta-luv/recipes-core/umip/files/umip_test_opnds.c @@ -377,10 +377,12 @@ static int test_str(void) pr_info("====Checking STR. Expected value: [0x%x]====\n", expected_tr); pr_info("==Tests for register operands==\n"); - pr_info("Value should be saved at [0x%p]\n", &val); + pr_info("Value should be saved at [0x%p]. Init value[0x%0"PFX_ZEROS"x]\n", + &val, expected_tr); CHECK_ALLreg("str", val, INIT_SS, (unsigned long)expected_tr); pr_info("==Tests for memory operands==\n"); - pr_info("Value should be saved at [0x%p]\n", &val); + pr_info("Value should be saved at [0x%p]. Init value[0x%0"PFX_ZEROS"x]\n", + &val, expected_tr); CHECK_ALLmem("str", val, INIT_SS, (unsigned long)expected_tr); /* TODO: check with addressing using SIB byte */ return 0; @@ -397,10 +399,12 @@ static int test_smsw(void) pr_info("====Checking SMSW. Expected value: [0x%x]====\n", expected_msw); pr_info("==Tests for register operands==\n"); - pr_info("Value should be saved at [0x%p]\n", &val); + pr_info("Value should be saved at [0x%p]. Init value[0x%0"PFX_ZEROS"x]\n", + &val, expected_msw); CHECK_ALLreg("smsw", val, INIT_MSW, (unsigned long)expected_msw); pr_info("==Tests for memory operands==\n"); - pr_info("Value should be saved at [0x%p]\n", &val); + pr_info("Value should be saved at [0x%p]. Init value[0x%0"PFX_ZEROS"x]\n", + &val, expected_msw); CHECK_ALLmem("smsw", val, INIT_MSW, (unsigned long)expected_msw); /* TODO: check with addressing using SIB byte */ return 0; @@ -416,10 +420,12 @@ static int test_sldt(void) pr_info("====Checking SLDT. Expected value: [0x%x]====\n", expected_ldt); pr_info("==Tests for register operands==\n"); - pr_info("Value should be saved at [0x%p]\n", &val); + pr_info("Value should be saved at [0x%p]. Init value[0x%0"PFX_ZEROS"x]\n", + &val, expected_ldt); CHECK_ALLreg("sldt", val, INIT_LDTS, (unsigned long)expected_ldt); pr_info("==Tests for memory operands==\n"); - pr_info("Value should be saved at [0x%p]\n", &val); + pr_info("Value should be saved at [0x%p]. Init value[0x%0"PFX_ZEROS"x]\n", + &val, expected_ldt); CHECK_ALLmem("sldt", val, INIT_LDTS, (unsigned long)expected_ldt); /* TODO: check with addressing using SIB byte */ return 0; From a10095fa468d75c91d565f225638079363fe5454 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Fri, 11 Aug 2017 19:59:27 -0700 Subject: [PATCH 084/109] umip: umip_test_opnds: print correct expected value Tests for register operand incorrectly print the obtained value instead of the expected value. The correct way of printing the expected value is to AND the expected value with the mask of changed bytes. The result is ORed with intit value ANDed with the mask of bytes that are not expected to change. Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/umip_test_opnds.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/umip_test_opnds.c b/meta-luv/recipes-core/umip/files/umip_test_opnds.c index a7782e408ce..c76b224973b 100644 --- a/meta-luv/recipes-core/umip/files/umip_test_opnds.c +++ b/meta-luv/recipes-core/umip/files/umip_test_opnds.c @@ -133,12 +133,12 @@ pr_pass(test_passed, " On %s-bit '%s %s'! " \ "Got [0x%0"PFX_ZEROS"lx] " \ "Exp[0x%0"PFX_ZEROS"lx]\n", \ - #op_size, insn, reg, val, (val&mask) | (init&~mask)); \ + #op_size, insn, reg, val, (exp&mask) | (init&~mask)); \ else { \ pr_fail(test_failed, " On %s-bit '%s %s'! " \ "Got[0x%0"PFX_ZEROS"lx] " \ "Exp[0x%0"PFX_ZEROS"lx]\n", \ - #op_size, insn, reg, val, (val&mask) | (init&~mask)); \ + #op_size, insn, reg, val, (exp&mask) | (init&~mask)); \ } \ } while(0); From a6f4804cde2b6816813d0a871fa8b43fc40f71a2 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Mon, 14 Aug 2017 10:00:43 -0700 Subject: [PATCH 085/109] umip: definitions: update expected return value of SMSW Emulation code generates the 64-bit value of 0x80050033. This means that the bits CR0.PE, CR0.MP, CR0.ET, CR0.NE, CR0.WP, CR0.AM and CR0.PG are enabled. Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/umip_test_defs.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/meta-luv/recipes-core/umip/files/umip_test_defs.h b/meta-luv/recipes-core/umip/files/umip_test_defs.h index 774f4b1a305..bcc951f2655 100644 --- a/meta-luv/recipes-core/umip/files/umip_test_defs.h +++ b/meta-luv/recipes-core/umip/files/umip_test_defs.h @@ -50,7 +50,7 @@ #define INIT_VAL(val) (0x##val) #endif -#define EXPECTED_SMSW 0x33 +#define EXPECTED_SMSW 0x80050033 #define EXPECTED_SLDT 0x0 #define EXPECTED_STR 0x0 #define EXPECTED_GDT_BASE 0xfffe0000 From 4ae9b93e0d8aefa7ee0dc65eb9ad4b52168f3914 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Mon, 14 Aug 2017 10:57:18 -0700 Subject: [PATCH 086/109] umip: use 64-bit variables for expected results Some of the tests involve 64-bit operands. Hence, we should use 64-bit variables to store the expected results in order to perform results' validation correctly. Also, update printf modifiers accordingly. Signed-off-by: Ricardo Neri --- .../recipes-core/umip/files/umip_test_defs.h | 6 +++--- .../recipes-core/umip/files/umip_test_opnds.c | 18 +++++++++--------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/umip_test_defs.h b/meta-luv/recipes-core/umip/files/umip_test_defs.h index bcc951f2655..8e613e496e8 100644 --- a/meta-luv/recipes-core/umip/files/umip_test_defs.h +++ b/meta-luv/recipes-core/umip/files/umip_test_defs.h @@ -104,9 +104,9 @@ struct table_desc { unsigned long base; } __attribute__((packed)); -static const unsigned short expected_msw = EXPECTED_SMSW; -static const unsigned short expected_ldt = EXPECTED_SLDT; -static const unsigned short expected_tr = EXPECTED_STR; +static const unsigned long expected_msw = EXPECTED_SMSW; +static const unsigned long expected_ldt = EXPECTED_SLDT; +static const unsigned long expected_tr = EXPECTED_STR; static const struct table_desc expected_gdt = { .limit = EXPECTED_GDT_LIMIT, diff --git a/meta-luv/recipes-core/umip/files/umip_test_opnds.c b/meta-luv/recipes-core/umip/files/umip_test_opnds.c index c76b224973b..e1b7dd8f45d 100644 --- a/meta-luv/recipes-core/umip/files/umip_test_opnds.c +++ b/meta-luv/recipes-core/umip/files/umip_test_opnds.c @@ -375,13 +375,13 @@ static int test_str(void) INIT_EXPECTED_SIGNAL_STR_SLDT(exp_signum, 0, exp_sigcode, 0); - pr_info("====Checking STR. Expected value: [0x%x]====\n", expected_tr); + pr_info("====Checking STR. Expected value: [0x%lx]====\n", expected_tr); pr_info("==Tests for register operands==\n"); - pr_info("Value should be saved at [0x%p]. Init value[0x%0"PFX_ZEROS"x]\n", + pr_info("Value should be saved at [0x%p]. Init value[0x%0"PFX_ZEROS"lx]\n", &val, expected_tr); CHECK_ALLreg("str", val, INIT_SS, (unsigned long)expected_tr); pr_info("==Tests for memory operands==\n"); - pr_info("Value should be saved at [0x%p]. Init value[0x%0"PFX_ZEROS"x]\n", + pr_info("Value should be saved at [0x%p]. Init value[0x%0"PFX_ZEROS"lx]\n", &val, expected_tr); CHECK_ALLmem("str", val, INIT_SS, (unsigned long)expected_tr); /* TODO: check with addressing using SIB byte */ @@ -397,13 +397,13 @@ static int test_smsw(void) INIT_EXPECTED_SIGNAL(exp_signum, 0, exp_sigcode, 0); - pr_info("====Checking SMSW. Expected value: [0x%x]====\n", expected_msw); + pr_info("====Checking SMSW. Expected valuE: [0x%lx]====\n", expected_msw); pr_info("==Tests for register operands==\n"); - pr_info("Value should be saved at [0x%p]. Init value[0x%0"PFX_ZEROS"x]\n", + pr_info("Value should be saved at [0x%p]. Init value[0x%0"PFX_ZEROS"lx]\n", &val, expected_msw); CHECK_ALLreg("smsw", val, INIT_MSW, (unsigned long)expected_msw); pr_info("==Tests for memory operands==\n"); - pr_info("Value should be saved at [0x%p]. Init value[0x%0"PFX_ZEROS"x]\n", + pr_info("Value should be saved at [0x%p]. Init value[0x%0"PFX_ZEROS"lx]\n", &val, expected_msw); CHECK_ALLmem("smsw", val, INIT_MSW, (unsigned long)expected_msw); /* TODO: check with addressing using SIB byte */ @@ -418,13 +418,13 @@ static int test_sldt(void) INIT_EXPECTED_SIGNAL(exp_signum, 0, exp_sigcode, 0); - pr_info("====Checking SLDT. Expected value: [0x%x]====\n", expected_ldt); + pr_info("====Checking SLDT. Expected value: [0x%lx]====\n", expected_ldt); pr_info("==Tests for register operands==\n"); - pr_info("Value should be saved at [0x%p]. Init value[0x%0"PFX_ZEROS"x]\n", + pr_info("Value should be saved at [0x%p]. Init value[0x%0"PFX_ZEROS"lx]\n", &val, expected_ldt); CHECK_ALLreg("sldt", val, INIT_LDTS, (unsigned long)expected_ldt); pr_info("==Tests for memory operands==\n"); - pr_info("Value should be saved at [0x%p]. Init value[0x%0"PFX_ZEROS"x]\n", + pr_info("Value should be saved at [0x%p]. Init value[0x%0"PFX_ZEROS"lx]\n", &val, expected_ldt); CHECK_ALLmem("sldt", val, INIT_LDTS, (unsigned long)expected_ldt); /* TODO: check with addressing using SIB byte */ From 1fd36cc2000928bef6e863b990788e505dcf328c Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Mon, 14 Aug 2017 11:59:59 -0700 Subject: [PATCH 087/109] umip: umip_test_basic: correct test result verification to 16-bit only These tests write results to memory locations. In these cases, the instructions STR, SMSW and SLDT return a 16-bit value. Hence, we need to verify that only those 16-bit values, not a whole 32-bit or 64-bit value is written to memory. Signed-off-by: Ricardo Neri --- .../recipes-core/umip/files/umip_test_basic.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/umip_test_basic.c b/meta-luv/recipes-core/umip/files/umip_test_basic.c index 80998f7ba33..4e17fe2cdae 100644 --- a/meta-luv/recipes-core/umip/files/umip_test_basic.c +++ b/meta-luv/recipes-core/umip/files/umip_test_basic.c @@ -129,7 +129,7 @@ static void call_sldt() * as well as that the bits that are not supposed to change * does not change. */ \ - if ((val & mask) == expected_ldt && + if ((val & mask) == (expected_ldt & mask) && (val & ~mask) == (init_val & ~mask)) pr_pass(test_passed, "Obtained expected value\n"); else @@ -161,7 +161,7 @@ static void call_smsw() * as well as that the bits that are not supposed to change * does not change. */ \ - if ((val & mask) == expected_msw && + if ((val & mask) == (expected_msw & mask) && (val & ~mask) == (init_val & ~mask)) pr_pass(test_passed, "Obtained expected value\n"); else @@ -182,6 +182,7 @@ static void call_str() #if __x86_64__ unsigned long val64 = 0xa3a3a3a3a3a3a3a3; + unsigned long init_val64 = 0xa3a3a3a3a3a3a3a3; pr_info("Will issue STR and save at m64[0x%p]\n", &val64); asm volatile("str %0\n" NOP_SLED : "=m" (val64)); @@ -191,8 +192,14 @@ static void call_str() pr_info("SS for TSS[0x%016lx]\n", val64); - /* All 64 bits are written */ - if (val64 == expected_tr) + /* + * Check that the bits that are supposed to change does so + * as well as that the bits that are not supposed to change + * does not change. Since operand is memory, value will be + * 16-bit. + */ + if ((val64 & 0xffff) == (expected_tr & 0xffff) && + (val64 & ~0xffff) == (init_val64 & ~0xffff)) pr_pass(test_passed, "Obtained 64-bit expected value\n"); else pr_fail(test_failed, "Obtained 64-bit unexpected value\n"); @@ -217,7 +224,7 @@ static void call_str() * does not change. Since operand is memory, value will be * 16-bit. */ - if ((val32 & 0xffff) == expected_tr && + if ((val32 & 0xffff) == (expected_tr & 0xffff) && (val32 & ~0xffff) == (init_val32 & ~0xffff)) pr_pass(test_passed, "Obtained 32-bit expected value\n"); else @@ -241,7 +248,7 @@ static void call_str() * does not change. Since operand is memory, value will be * 16-bit. */ - if ((val16 & 0xffff) == expected_tr && + if ((val16 & 0xffff) == (expected_tr & 0xffff) && (val16 & ~0xffff) == (init_val16 & ~0xffff)) pr_pass(test_passed, "Obtained 16-bit expected value\n"); else From db81cfcaed1dbbae35752ce6d174bd68c55a47d5 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Tue, 15 Aug 2017 10:16:24 -0700 Subject: [PATCH 088/109] umip: umip_ldt_16: use labels for inline asm arguments This makes it easier to read variable assignments. Signed-off-by: Ricardo Neri --- .../recipes-core/umip/files/umip_ldt_16.c | 37 ++++++++++--------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/umip_ldt_16.c b/meta-luv/recipes-core/umip/files/umip_ldt_16.c index 9663172660f..6cbfca117bf 100644 --- a/meta-luv/recipes-core/umip/files/umip_ldt_16.c +++ b/meta-luv/recipes-core/umip/files/umip_ldt_16.c @@ -270,10 +270,10 @@ int run_umip_ldt_test(void) "push %%esi\n\t" "push %%ebp\n\t" /* set new data segment */ - "mov %0, %%ds\n\t" - "mov %1, %%es\n\t" - "mov %2, %%fs\n\t" - "mov %3, %%gs\n\t" + "mov %[test_ds_16], %%ds\n\t" + "mov %[test_es_16], %%es\n\t" + "mov %[test_fs_16], %%fs\n\t" + "mov %[test_gs_16], %%gs\n\t" /* * Save current stack settings and pass them to the new code segment * via registers. We will save these as soon as we setup a new stack @@ -285,24 +285,24 @@ int run_umip_ldt_test(void) * Give interim code the new stack segment. We cannot set it here as * we need it to jump to the test code via retf */ - "mov %4, %%ecx\n\t" + "mov %[interim_ss], %%ecx\n\t" /* * Pass the code segment selector to the interim code so that it knows * where to return */ "mov %%cs, %%edx\n\t" - /* - * Pass the code and stacks segment selectors of the 16-bit code - * as we only know them from this context. - */ - "mov %5, %%esi\n\t" - "mov %6, %%edi\n\t" /* - *ljmp only takes constants. Instead use retf, which takes + * Pass the code and stacks segment selectors of the 16-bit code + * as we only know them from this context. + */ + "mov %[test_ss_16], %%esi\n\t" + "mov %[test_cs_16], %%edi\n\t" + /* + *ljmp only takes constants. Instead use retf, which takes * instruction pointer and code segment selector from the stack - */ - "push %7\n\t" - "push %8\n\t" + */ + "push %[interim_cs]\n\t" + "push %[interim_start_addr]\n\t" "retf \n\t" "finish_testing:\n\t" /* restore our stack */ @@ -321,8 +321,11 @@ int run_umip_ldt_test(void) "pop %%es\n\t" "pop %%ds\n\t" : - :"m"(test_ds_16), "m"(test_es_16), "m"(test_fs_16), "m"(test_gs_16), - "m"(interim_ss), "m"(test_ss_16), "m"(test_cs_16), "m"(interim_cs), "m"(interim_start_addr) + : [test_ds_16]"m"(test_ds_16), [test_es_16]"m"(test_es_16), + [test_fs_16]"m"(test_fs_16), [test_gs_16]"m"(test_gs_16), + [interim_ss]"m"(interim_ss), [test_ss_16]"m"(test_ss_16), + [test_cs_16]"m"(test_cs_16), [interim_cs]"m"(interim_cs), + [interim_start_addr]"m"(interim_start_addr) ); pr_info("===Test results===\n"); From 7e6226350e0f578c7e339d3f37fb9b28761de6c0 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Tue, 15 Aug 2017 10:39:20 -0700 Subject: [PATCH 089/109] umip: use -fno-omit-frame-pointer Local variables are referenced via the frame pointer, (e|r)bp or the stack pointer, (e|r)sp. Even though we have sanitized some of our programs to restore the stack pointer before using local variables, it is not possible to do this for all test programs (e.g., programs for local descriptor tables). Hence, tell compiler to refer to local variables using the frame pointer instead. Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/Makefile | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/Makefile b/meta-luv/recipes-core/umip/files/Makefile index 4d62c75d6a3..f6dc503a7f6 100644 --- a/meta-luv/recipes-core/umip/files/Makefile +++ b/meta-luv/recipes-core/umip/files/Makefile @@ -1,3 +1,4 @@ +x86_64_minimal: CFLAGS += -fno-omit-frame-pointer x86_64_minimal: $(CC) $(CFLAGS) -c umip_utils.c -o umip_utils_64.o @@ -10,7 +11,7 @@ x86_64_minimal: $(CC) $(CFLAGS) -c umip_ldt_64.c $(CC) $(CFLAGS) -o umip_ldt_64 test_umip_ldt_64.o umip_ldt_64.o umip_utils_64.o -x86_64_emul_all: CFLAGS += -DEMULATE_ALL +x86_64_emul_all: CFLAGS += -DEMULATE_ALL -fno-omit-frame-pointer x86_64_emul_all: x86_64_minimal $(CC) $(CFLAGS) -c umip_utils.c -o umip_utils_64.o @@ -25,6 +26,7 @@ x86_64_emul_all: x86_64_minimal x86_64: x86_64_minimal x86_64_emul_all +i686_minimal: CFLAGS += -fno-omit-frame-pointer i686_minimal: $(CC) $(CFLAGS) -c umip_utils.c -o umip_utils_32.o @@ -42,7 +44,7 @@ i686_minimal: $(CC) $(CFLAGS) -c umip_ldt_16.c $(CC) $(CFLAGS) -o umip_ldt_16 test_umip_ldt_16.o umip_ldt_16.o umip_utils_32.o -i686_emul_all: CFLAGS += -DEMULATE_ALL +i686_emul_all: CFLAGS += -DEMULATE_ALL -fno-omit-frame-pointer i686_emul_all: i686_minimal $(CC) $(CFLAGS) -c umip_utils.c -o umip_utils_32.o From 707a0b5d27a471d0554fa25df00cb1cf04bb8149 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Wed, 16 Aug 2017 10:17:35 -0700 Subject: [PATCH 090/109] umip: umip_exceptions: make page-fault tests check for SI_KERNEL code Some of the instructions that UMIP protects are not always emulated (i.e., str and sldt). In such cases, a SIGSEGV with SI_KERNEL code is received instead of the page fault. Update the tests for page-faults to take the expected signal number and signal code as arguments to be used when inspecting the received signal. This also requires having two sets of signal number and signal code: one for smsw, sgdt and sidt, for which SIGSEGV/SEG_MAPERR is always expected; and another for sldt and sidt, for which SIGSEGV/SEG_MAPERR or SIGSEGV/SI_KERNEL are expected, depending on whether str and sldt are emulated. Signed-off-by: Ricardo Neri --- .../recipes-core/umip/files/umip_exceptions.c | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/umip_exceptions.c b/meta-luv/recipes-core/umip/files/umip_exceptions.c index ffc9cd4321d..6748fa02863 100644 --- a/meta-luv/recipes-core/umip/files/umip_exceptions.c +++ b/meta-luv/recipes-core/umip/files/umip_exceptions.c @@ -21,14 +21,14 @@ extern sig_atomic_t got_signal, got_sigcode; int test_passed, test_failed, test_errors; #define gen_test_maperr_pf_inst(inst, bad_addr) \ -static void __test_maperr_pf_##inst(void) \ +static void __test_maperr_pf_##inst(int exp_signum, int exp_sigcode) \ { \ unsigned long *val_bad = (unsigned long *)bad_addr; \ \ pr_info("Test page fault because unmapped memory for %s with addr %p\n", #inst, val_bad);\ asm volatile (#inst" %0\n" NOP_SLED : "=m"(*val_bad)); \ \ - inspect_signal(SIGSEGV, SEGV_MAPERR); \ + inspect_signal(exp_signum, exp_sigcode); \ } gen_test_maperr_pf_inst(smsw, 0x100000) @@ -39,11 +39,18 @@ gen_test_maperr_pf_inst(sldt, 0x100000) static void test_maperr_pf(void) { - __test_maperr_pf_smsw(); - __test_maperr_pf_sidt(); - __test_maperr_pf_sgdt(); - __test_maperr_pf_str(); - __test_maperr_pf_sldt(); + int exp_signum, exp_sigcode; + int exp_signum_str_sldt, exp_sigcode_str_sldt; + + INIT_EXPECTED_SIGNAL(exp_signum, SIGSEGV, exp_sigcode, SEGV_MAPERR); + INIT_EXPECTED_SIGNAL_STR_SLDT(exp_signum_str_sldt, SIGSEGV, + exp_sigcode_str_sldt, SEGV_MAPERR); + + __test_maperr_pf_smsw(exp_signum, exp_sigcode); + __test_maperr_pf_sidt(exp_signum, exp_sigcode); + __test_maperr_pf_sgdt(exp_signum, exp_sigcode); + __test_maperr_pf_str(exp_signum_str_sldt, exp_sigcode_str_sldt); + __test_maperr_pf_sldt(exp_signum_str_sldt, exp_sigcode_str_sldt); } #define gen_test_lock_prefix_inst(name, inst) \ From 336778db16ca95a7c0175301311b780a4ce49fe7 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Wed, 16 Aug 2017 10:26:46 -0700 Subject: [PATCH 091/109] umip: umip_exceptions: initialize signal number and code before test If a test generates a signal, subsequent tests should initialize the variables that hold the signal number and code. Otherwise, if no signal is received, the old, wrong, values will be used when when comparing with the expected values for a particular tests. Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/umip_exceptions.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/meta-luv/recipes-core/umip/files/umip_exceptions.c b/meta-luv/recipes-core/umip/files/umip_exceptions.c index 6748fa02863..310eb45b768 100644 --- a/meta-luv/recipes-core/umip/files/umip_exceptions.c +++ b/meta-luv/recipes-core/umip/files/umip_exceptions.c @@ -25,7 +25,11 @@ static void __test_maperr_pf_##inst(int exp_signum, int exp_sigcode) \ { \ unsigned long *val_bad = (unsigned long *)bad_addr; \ \ - pr_info("Test page fault because unmapped memory for %s with addr %p\n", #inst, val_bad);\ + got_signal = 0; \ + got_sigcode = 0; \ + \ + pr_info("Test page fault because unmapped memory for %s with addr %p\n",\ + #inst, val_bad); \ asm volatile (#inst" %0\n" NOP_SLED : "=m"(*val_bad)); \ \ inspect_signal(exp_signum, exp_sigcode); \ From 82cfc22029b1550d74ae55934791292d214f4d6b Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Wed, 16 Aug 2017 10:32:04 -0700 Subject: [PATCH 092/109] umip: umip_test_opnds: use SIGSEGV/SI_KERNEL for str and sldt Unless the kernel is patched, the instructions str and sldt will not be emulated. In such cases, these instructions will generate a SIGSEGV/SI_KERNEL signa number and code pair. Hence, use the macro INIT_EXPECTED_SIGNAL_STR_SLDT. This macro initializes the expected values with the correct values. Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/umip_test_opnds.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/meta-luv/recipes-core/umip/files/umip_test_opnds.c b/meta-luv/recipes-core/umip/files/umip_test_opnds.c index e1b7dd8f45d..86aa6402223 100644 --- a/meta-luv/recipes-core/umip/files/umip_test_opnds.c +++ b/meta-luv/recipes-core/umip/files/umip_test_opnds.c @@ -416,7 +416,7 @@ static int test_sldt(void) unsigned long mask = 0xffff; int exp_signum, exp_sigcode; - INIT_EXPECTED_SIGNAL(exp_signum, 0, exp_sigcode, 0); + INIT_EXPECTED_SIGNAL_STR_SLDT(exp_signum, 0, exp_sigcode, 0); pr_info("====Checking SLDT. Expected value: [0x%lx]====\n", expected_ldt); pr_info("==Tests for register operands==\n"); From 46af992eb955f7640e8f910ff8dcb78689e8fa27 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Wed, 16 Aug 2017 10:45:06 -0700 Subject: [PATCH 093/109] umip: umip_utils: use exit_on_signal codes to determine test status If a program needs to exit on signal, it is considered that the test program failed and needs to terminate. However, there may be cases in which receiving a signal is expected (e.g, when a test program is built without -DEMULATE_ALL). In that case, exit_on_signal can be used to determine if the test generating the signal passed or failed. Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/umip_utils.c | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/umip_utils.c b/meta-luv/recipes-core/umip/files/umip_utils.c index 3aa64f2cb55..1efdcdc1112 100644 --- a/meta-luv/recipes-core/umip/files/umip_utils.c +++ b/meta-luv/recipes-core/umip/files/umip_utils.c @@ -11,6 +11,13 @@ extern int test_passed, test_failed, test_errors; sig_atomic_t got_signal, got_sigcode; + +/* + * Use: + * 0 no exit on signal + * 1 receiving a signal means the test failed + * 2 receiving a signal means the test passed + */ int exit_on_signal; void print_results(void) @@ -114,6 +121,16 @@ void signal_handler(int signum, siginfo_t *info, void *ctx_void) /* Save the signal code */ got_sigcode = info->si_code; + if (exit_on_signal) { + if (exit_on_signal == 1) + pr_fail(test_failed, "Whoa! I got a signal! Something went wrong!\n"); + else if (exit_on_signal == 2) + pr_pass(test_passed, "I got the expected signal.\n"); + else + pr_fail(test_failed, "I don't know what to do on exit.\n"); + exit(1); + } + /* * Move to the next instruction; to move, increment the instruction * pointer by 10 bytes. 10 bytes is the size of the instruction @@ -122,11 +139,6 @@ void signal_handler(int signum, siginfo_t *info, void *ctx_void) * a NOP sled after the instruction to ensure we continue execution * safely in case we overestimate the size of the instruction. */ - - if (exit_on_signal) { - pr_fail(test_failed, "Whoa! I got a signal! Something went wrong!\n"); - exit(1); - } #ifdef __x86_64__ ctx->uc_mcontext.gregs[REG_RIP] += 10; #else From d86efbbc9437bd3af5d68ec661648b4013aebe7d Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Wed, 16 Aug 2017 11:01:51 -0700 Subject: [PATCH 094/109] umip: umip_ldt_64: set exit on signal as passed test case When this program is run with EMULATE_ALL undefined, no emulation for any of the UMIP-protected instructions is expected. Thus, it is expected to receive SIGSEGV/SI_KERNEL; the test passs. On the other hand, if EMULATE_ALL is defined, the test programs should not receive any signal and should exit if one is received. In this case, the test failed. Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/umip_ldt_64.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/meta-luv/recipes-core/umip/files/umip_ldt_64.c b/meta-luv/recipes-core/umip/files/umip_ldt_64.c index d3d29f7c7c8..b25cef72a51 100644 --- a/meta-luv/recipes-core/umip/files/umip_ldt_64.c +++ b/meta-luv/recipes-core/umip/files/umip_ldt_64.c @@ -86,7 +86,11 @@ int main(void) action.sa_flags = SA_SIGINFO; sigemptyset(&action.sa_mask); +#ifdef EMULATE_ALL exit_on_signal = 1; +#else + exit_on_signal = 2; +#endif if (sigaction(SIGSEGV, &action, NULL) < 0) { pr_error(test_errors, "Could not set the signal handler!"); From c68e56471e79d31310ff8cc4e5ddaa3260d12e9e Mon Sep 17 00:00:00 2001 From: Pengfei Xu Date: Wed, 23 Aug 2017 10:25:16 +0000 Subject: [PATCH 095/109] umip: umip_*: add support for command line arguments Command line arguments are great for below 3 umip c files: umip_test_basic.c add arguments: Usage: [g][i][l][m][t][a] g Test sgdt i Test sidt l Test sldt m Test smsw t Test str a Test all umip_test_opnds.c add arguments: Usage: [l][m][t][a] l Test sldt register operands m Test smsw register operands t Test str register operands a Test all umip_exceptions.c add arguments: Usage: [m][l][r][n][d][a] m Test test_maperr_pf l Test test_lock_prefix r Test test_register_operand n Test test_null_segment_selectors(TODO) d Test test_addresses_outside_segment(TODO) a Test all Signed-off-by: Pengfei Xu Signed-off-by: Ricardo Neri --- .../recipes-core/umip/files/umip_exceptions.c | 67 +++++++++++++++++-- .../recipes-core/umip/files/umip_test_basic.c | 57 ++++++++++++++-- .../recipes-core/umip/files/umip_test_opnds.c | 54 +++++++++++++-- 3 files changed, 162 insertions(+), 16 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/umip_exceptions.c b/meta-luv/recipes-core/umip/files/umip_exceptions.c index 310eb45b768..033cbc5c6a5 100644 --- a/meta-luv/recipes-core/umip/files/umip_exceptions.c +++ b/meta-luv/recipes-core/umip/files/umip_exceptions.c @@ -320,7 +320,18 @@ static int test_addresses_outside_segment(void) } #endif -int main (void) +void usage(void) +{ + printf("Usage: [m][l][r][n][d][a]\n"); + printf("m Test test_maperr_pf\n"); + printf("l Test test_lock_prefix\n"); + printf("r Test test_register_operand\n"); + printf("n Test test_null_segment_selectors(TODO)\n"); + printf("d Test test_addresses_outside_segment(TODO)\n"); + printf("a Test all\n"); +} + +int main (int argc, char *argv[]) { struct sigaction action; @@ -330,6 +341,23 @@ int main (void) action.sa_sigaction = &signal_handler; action.sa_flags = SA_SIGINFO; sigemptyset(&action.sa_mask); + char parm; + + if (argc == 1) + { + usage(); + exit(1); + } + else if (argc == 2) + { + sscanf(argv[1], "%c", &parm); + pr_info("1 parameters: parm=%c\n", parm); + } + else + { + sscanf(argv[1], "%c", &parm); + pr_info("Just get parm=%c\n", parm); + } if (sigaction(SIGSEGV, &action, NULL) < 0) { pr_error(test_errors, "Could not set the signal handler for SIGSEGV!\n"); @@ -341,11 +369,38 @@ int main (void) exit(1); } - test_maperr_pf(); - test_lock_prefix(); - test_register_operand(); - test_null_segment_selectors(); - test_addresses_outside_segment(); + switch (parm) + { + case 'a' : pr_info("Test all.\n"); + pr_info("***Test test_maperr_pf next***\n"); + test_maperr_pf(); + pr_info("***Test test_lock_prefix next***\n"); + test_lock_prefix(); + pr_info("***Test test_register_operand next***\n"); + test_register_operand(); + pr_info("***Test test_null_segment_selectors next***\n"); + test_null_segment_selectors(); + pr_info("***Test test_addresses_outside_segment next***\n"); + test_addresses_outside_segment(); + break; + case 'm' : pr_info("***Test test_maperr_pf next***\n"); + test_maperr_pf(); + break; + case 'l' : pr_info("***Test test_lock_prefix next***\n"); + test_lock_prefix(); + break; + case 'r' : pr_info("***Test test_register_operand next***\n"); + test_register_operand(); + break; + case 'n' : pr_info("***Test test_null_segment_selectors next***"); + test_null_segment_selectors(); + break; + case 'd' : pr_info("***Test test_addresses_outside_segment next***"); + test_addresses_outside_segment(); + break; + default: usage(); + exit(1); + } memset(&action, 0, sizeof(action)); action.sa_handler = SIG_DFL; diff --git a/meta-luv/recipes-core/umip/files/umip_test_basic.c b/meta-luv/recipes-core/umip/files/umip_test_basic.c index 4e17fe2cdae..24bcf8afe75 100644 --- a/meta-luv/recipes-core/umip/files/umip_test_basic.c +++ b/meta-luv/recipes-core/umip/files/umip_test_basic.c @@ -256,7 +256,18 @@ static void call_str() } -int main(void) +void usage(void) +{ + printf("Usage: [g][i][l][m][t][a]\n"); + printf("g Test sgdt\n"); + printf("i Test sidt\n"); + printf("l Test sldt\n"); + printf("m Test smsw\n"); + printf("t Test str\n"); + printf("a Test all\n"); +} + +int main(int argc, char *argv[]) { struct sigaction action; @@ -266,6 +277,23 @@ int main(void) action.sa_sigaction = &signal_handler; action.sa_flags = SA_SIGINFO; sigemptyset(&action.sa_mask); + char parm; + + if (argc == 1) + { + usage(); + exit(1); + } + else if (argc == 2) + { + sscanf(argv[1], "%c", &parm); + pr_info("1 parameters: parm=%c\n", parm); + } + else + { + sscanf(argv[1], "%c", &parm); + pr_info("Just get parm=%c\n", parm); + } if (sigaction(SIGSEGV, &action, NULL) < 0) { pr_error(test_errors, "Could not set the signal handler!"); @@ -273,11 +301,28 @@ int main(void) exit(1); } - call_sgdt(); - call_sidt(); - call_sldt(); - call_smsw(); - call_str(); + switch (parm) + { + case 'a' : pr_info("Test all.\n"); + call_sgdt(); + call_sidt(); + call_sldt(); + call_smsw(); + call_str(); + break; + case 'g' : call_sgdt(); + break; + case 'i' : call_sidt(); + break; + case 'l' : call_sldt(); + break; + case 'm' : call_smsw(); + break; + case 't' : call_str(); + break; + default: usage(); + exit(1); + } memset(&action, 0, sizeof(action)); action.sa_handler = SIG_DFL; diff --git a/meta-luv/recipes-core/umip/files/umip_test_opnds.c b/meta-luv/recipes-core/umip/files/umip_test_opnds.c index 86aa6402223..70d006efcb6 100644 --- a/meta-luv/recipes-core/umip/files/umip_test_opnds.c +++ b/meta-luv/recipes-core/umip/files/umip_test_opnds.c @@ -431,7 +431,16 @@ static int test_sldt(void) return 0; } -int main (void) +void usage(void) +{ + printf("Usage: [l][m][t][a]\n"); + printf("l Test sldt register operands\n"); + printf("m Test smsw register operands\n"); + printf("t Test str register operands\n"); + printf("a Test all\n"); +} + +int main (int argc, char *argv[]) { int ret_str, ret_smsw, ret_sldt; struct sigaction action; @@ -442,6 +451,23 @@ int main (void) action.sa_sigaction = &signal_handler; action.sa_flags = SA_SIGINFO; sigemptyset(&action.sa_mask); + char parm; + + if (argc == 1) + { + usage(); + exit(1); + } + else if (argc == 2) + { + sscanf(argv[1], "%c", &parm); + pr_info("1 parameters: parm=%c\n", parm); + } + else + { + sscanf(argv[1], "%c", &parm); + pr_info("Just get parm=%c\n", parm); + } if (sigaction(SIGSEGV, &action, NULL) < 0) { pr_error(test_errors, "Could not set the signal handler!"); @@ -449,9 +475,29 @@ int main (void) } pr_info("***Starting tests***\n"); - ret_str = test_str(); - ret_smsw = test_smsw(); - ret_sldt = test_sldt(); + + switch (parm) + { + case 'a' : pr_info("Test all.\n"); + pr_info("***Test str next***\n"); + ret_str = test_str(); + pr_info("***Test smsw next***\n"); + ret_smsw = test_smsw(); + pr_info("***Test sldt next***\n"); + ret_sldt = test_sldt(); + break; + case 't' : pr_info("***Test str next***\n"); + ret_str = test_str(); + break; + case 'm' : pr_info("***Test smsw next***\n"); + ret_smsw = test_smsw(); + break; + case 'l' : pr_info("***Test sldt next***\n"); + ret_sldt = test_sldt(); + break; + default: usage(); + exit(1); + } if (ret_str || ret_smsw || ret_sldt) pr_info("***Test completed with errors str[%d] smsw[%d] sldt[%d]\n", From 33ec321f8823523194dd6cf6e7e91ac4b77953fb Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Tue, 10 Oct 2017 14:06:57 -0700 Subject: [PATCH 096/109] umip_ldt_64: utilize ARCH_SET_FS/GS syscalls correctly Unlike ARCH_GET_FS/GS, ARCH_SET_FS/GS take as argument the actual value to be used as the bases of these two segments, not a pointer to a variable containing the value. Reported-by: Pengfei Xu Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/umip_ldt_64.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/umip_ldt_64.c b/meta-luv/recipes-core/umip/files/umip_ldt_64.c index b25cef72a51..56b29056ce7 100644 --- a/meta-luv/recipes-core/umip/files/umip_ldt_64.c +++ b/meta-luv/recipes-core/umip/files/umip_ldt_64.c @@ -121,8 +121,8 @@ int main(void) asm volatile("movw %%fs, %0" : "=m" (old_fs)); asm volatile("movw %%gs, %0" : "=m" (old_gs)); - syscall(SYS_arch_prctl, ARCH_SET_FS, (unsigned long)&data_fs); - syscall(SYS_arch_prctl, ARCH_SET_GS, (unsigned long)&data_gs); + syscall(SYS_arch_prctl, ARCH_SET_FS, (unsigned long)data_fs); + syscall(SYS_arch_prctl, ARCH_SET_GS, (unsigned long)data_gs); asm(/* make a backup of everything */ "push %%rax\n\t" @@ -151,8 +151,9 @@ int main(void) asm volatile("movw %0,%%fs" : :"m" (old_fs)); asm volatile("movw %0, %%gs" : : "m" (old_gs)); - syscall(SYS_arch_prctl, ARCH_SET_FS, &old_fsbase); - syscall(SYS_arch_prctl, ARCH_SET_GS, &old_gsbase); + + syscall(SYS_arch_prctl, ARCH_SET_FS, old_fsbase); + syscall(SYS_arch_prctl, ARCH_SET_GS, old_gsbase); printf("===Test results===\n"); check_results(); From 8ad470066b6209fa316ecdc5986f2540a843bb08 Mon Sep 17 00:00:00 2001 From: Pengfei Xu Date: Fri, 15 Sep 2017 09:51:18 +0800 Subject: [PATCH 097/109] umip: umip_utils.c: Solve one compile warning "type of 'exp_signum' defaults to 'int'" Fix below warning when "cc -fno-omit-frame-pointer -c umip_utils.c -o umip_utils_64.o" umip_utils.c: In function 'inspect_signal': umip_utils.c:55:5: warning: type of 'exp_signum' defaults to 'int' [-Wimplicit-int] int inspect_signal(exp_signum, exp_sigcode) ^~~~~~~~~~~~~~ umip_utils.c:55:5: warning: type of 'exp_sigcode' defaults to 'int' [-Wimplicit-int] Signed-off-by: Pengfei Xu Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/umip_utils.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/meta-luv/recipes-core/umip/files/umip_utils.c b/meta-luv/recipes-core/umip/files/umip_utils.c index 1efdcdc1112..48fee633975 100644 --- a/meta-luv/recipes-core/umip/files/umip_utils.c +++ b/meta-luv/recipes-core/umip/files/umip_utils.c @@ -52,7 +52,7 @@ int inspect_signal(int exp_signum, int exp_sigcode) * processing is not relevant for the caller can proceed with further test case * validation. */ -int inspect_signal(exp_signum, exp_sigcode) +int inspect_signal(int exp_signum, int exp_sigcode) { /* If we expect signal, make sure it is the one we expect. */ if (exp_signum) { From 25ee12c39546d9bd46387b4af2d91cededcca006 Mon Sep 17 00:00:00 2001 From: Pengfei Xu Date: Fri, 15 Sep 2017 10:44:56 +0800 Subject: [PATCH 098/109] umip: umip_exceptions.c: Solve one warning: should void test_addresses_outside_segment cc -fno-omit-frame-pointer -m32 -o umip_exceptions_32 umip_utils_32.o umip_exceptions.c ... umip_exceptions.c:296:3: warning: 'return' with no value, in function returning non-void return; ^~~~~~ umip_exceptions.c:290:12: note: declared here static int test_addresses_outside_segment(void) ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Signed-off-by: Pengfei Xu Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/umip_exceptions.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/meta-luv/recipes-core/umip/files/umip_exceptions.c b/meta-luv/recipes-core/umip/files/umip_exceptions.c index 033cbc5c6a5..f57ebe2b334 100644 --- a/meta-luv/recipes-core/umip/files/umip_exceptions.c +++ b/meta-luv/recipes-core/umip/files/umip_exceptions.c @@ -287,7 +287,7 @@ gen_test_addresses_outside_segment(sldt, gs) gen_test_addresses_outside_segment(sgdt, gs) gen_test_addresses_outside_segment(sidt, gs) -static int test_addresses_outside_segment(void) +static void test_addresses_outside_segment(void) { int ret; From 04cc0b3bee58465cd4e857d2657b9253c1acb846 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Tue, 10 Oct 2017 15:14:46 -0700 Subject: [PATCH 099/109] umip_utils: add a cleanup function for exit on signal There are cases in which some cleanup may be need to be done when exiting from a signal. Our signal handler cannot know what kind of cleanup is needed. Thus, simply call a function that users of the handler can implement as needed. The cleanup function is NULL by default. Thus, users of our handler does not need to implement it if not needed. Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/umip_utils.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/meta-luv/recipes-core/umip/files/umip_utils.c b/meta-luv/recipes-core/umip/files/umip_utils.c index 48fee633975..1807e2136ac 100644 --- a/meta-luv/recipes-core/umip/files/umip_utils.c +++ b/meta-luv/recipes-core/umip/files/umip_utils.c @@ -10,6 +10,7 @@ #include "umip_test_defs.h" extern int test_passed, test_failed, test_errors; +void (*cleanup)(void) = NULL; sig_atomic_t got_signal, got_sigcode; /* @@ -128,6 +129,10 @@ void signal_handler(int signum, siginfo_t *info, void *ctx_void) pr_pass(test_passed, "I got the expected signal.\n"); else pr_fail(test_failed, "I don't know what to do on exit.\n"); + + if (cleanup) + (*cleanup)(); + exit(1); } From 578e7f049ef6c6a245ce86c3ea7c3b9c2430fbc6 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Tue, 10 Oct 2017 15:21:06 -0700 Subject: [PATCH 100/109] umip_ldt_64: implement a cleanup function for the signal handler The signal handler provides functionality to invoke a cleanup function before exiting. In the case of this program, we must ensure that we restore the FS/GS base addresses for glibc to work properly. This cleanup the same that is needed when tests run successfully. Thus, this same function is called once tests are complete. Reported-by: Pengfei Xu Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/umip_ldt_64.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/umip_ldt_64.c b/meta-luv/recipes-core/umip/files/umip_ldt_64.c index 56b29056ce7..ac51b30985f 100644 --- a/meta-luv/recipes-core/umip/files/umip_ldt_64.c +++ b/meta-luv/recipes-core/umip/files/umip_ldt_64.c @@ -19,6 +19,7 @@ extern unsigned char finish_testing[]; extern unsigned char data_fs[SEGMENT_SIZE]; extern unsigned char data_gs[SEGMENT_SIZE]; extern int exit_on_signal; +extern void (*cleanup)(void); unsigned long old_fsbase, old_gsbase; unsigned short old_fs, old_gs; @@ -72,6 +73,15 @@ static int setup_data_segments() return 0; } +static void cleanup_segments(void) +{ + asm volatile("movw %0,%%fs" : :"m" (old_fs)); + asm volatile("movw %0, %%gs" : : "m" (old_gs)); + + syscall(SYS_arch_prctl, ARCH_SET_FS, old_fsbase); + syscall(SYS_arch_prctl, ARCH_SET_GS, old_gsbase); +} + int main(void) { int ret; @@ -92,6 +102,7 @@ int main(void) exit_on_signal = 2; #endif + cleanup = cleanup_segments; if (sigaction(SIGSEGV, &action, NULL) < 0) { pr_error(test_errors, "Could not set the signal handler!"); goto err_out; @@ -149,11 +160,7 @@ int main(void) :"m"(test_fs), "m"(test_gs), "m"(code) ); - asm volatile("movw %0,%%fs" : :"m" (old_fs)); - asm volatile("movw %0, %%gs" : : "m" (old_gs)); - - syscall(SYS_arch_prctl, ARCH_SET_FS, old_fsbase); - syscall(SYS_arch_prctl, ARCH_SET_GS, old_gsbase); + cleanup_segments(); printf("===Test results===\n"); check_results(); From 840bb1ef888bbef692e13cddbabf807fb3889c4a Mon Sep 17 00:00:00 2001 From: Pengfei Xu Date: Tue, 10 Oct 2017 18:06:12 -0700 Subject: [PATCH 101/109] umip: Makefile: add -m32 to compile 32bit and 16bit .o and program Add -m32 when "make i686_minimal" or "make i686_emul_all" for 32bit and 16bit .o and program. Signed-off-by: Pengfei Xu Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/Makefile b/meta-luv/recipes-core/umip/files/Makefile index f6dc503a7f6..3b07b1026f0 100644 --- a/meta-luv/recipes-core/umip/files/Makefile +++ b/meta-luv/recipes-core/umip/files/Makefile @@ -26,7 +26,7 @@ x86_64_emul_all: x86_64_minimal x86_64: x86_64_minimal x86_64_emul_all -i686_minimal: CFLAGS += -fno-omit-frame-pointer +i686_minimal: CFLAGS += -fno-omit-frame-pointer -m32 i686_minimal: $(CC) $(CFLAGS) -c umip_utils.c -o umip_utils_32.o @@ -44,7 +44,7 @@ i686_minimal: $(CC) $(CFLAGS) -c umip_ldt_16.c $(CC) $(CFLAGS) -o umip_ldt_16 test_umip_ldt_16.o umip_ldt_16.o umip_utils_32.o -i686_emul_all: CFLAGS += -DEMULATE_ALL -fno-omit-frame-pointer +i686_emul_all: CFLAGS += -DEMULATE_ALL -fno-omit-frame-pointer -m32 i686_emul_all: i686_minimal $(CC) $(CFLAGS) -c umip_utils.c -o umip_utils_32.o From 66c8e15724abe346fc9f4216a61f09f043bb56d6 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Fri, 20 Oct 2017 12:43:46 -0700 Subject: [PATCH 102/109] umip: remove -m32 when building via bitake These test programs can be built either stand-alone or via bitbake. The -m32 option for 32-bit programs is only needed when building in stand- alone mode as we need to tell gcc to cross-compile and use 32-bit libraries. When building via bitbake, bitbake takes care of building with the appropriate native 32-bit libraries. Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/umip-tests_0.1.bb | 1 + 1 file changed, 1 insertion(+) diff --git a/meta-luv/recipes-core/umip/umip-tests_0.1.bb b/meta-luv/recipes-core/umip/umip-tests_0.1.bb index 6900182085e..7dbbe319f44 100644 --- a/meta-luv/recipes-core/umip/umip-tests_0.1.bb +++ b/meta-luv/recipes-core/umip/umip-tests_0.1.bb @@ -29,6 +29,7 @@ EXTRA_OEMAKE += " ${TARGET_ARCH}" S = "${WORKDIR}" do_compile() { + sed -i 's/\-m32//g' ${S}/Makefile oe_runmake } From 999e5c5616c862c376606009f1cc8202e803df04 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Fri, 20 Oct 2017 15:12:23 -0700 Subject: [PATCH 103/109] umip_ldt_64: preserve %r[8-15] registers Some local variables are tracked using the %r[8-15] registers. Thus, we need to restore their value after testing. Otherwise, errors such as segmentation faults can be observed. Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/umip_ldt_64.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/meta-luv/recipes-core/umip/files/umip_ldt_64.c b/meta-luv/recipes-core/umip/files/umip_ldt_64.c index ac51b30985f..7ba03de7bcf 100644 --- a/meta-luv/recipes-core/umip/files/umip_ldt_64.c +++ b/meta-luv/recipes-core/umip/files/umip_ldt_64.c @@ -143,12 +143,28 @@ int main(void) "push %%rdi\n\t" "push %%rsi\n\t" "push %%rbp\n\t" + "push %%r8\n\t" + "push %%r9\n\t" + "push %%r10\n\t" + "push %%r11\n\t" + "push %%r12\n\t" + "push %%r13\n\t" + "push %%r14\n\t" + "push %%r15\n\t" /* set new data segment */ /* jump to test code */ "call *%2\n\t" /* After running tests, we return here */ "finish_testing:\n\t" /* restore everything */ + "pop %%r15\n\t" + "pop %%r14\n\t" + "pop %%r13\n\t" + "pop %%r12\n\t" + "pop %%r11\n\t" + "pop %%r10\n\t" + "pop %%r9\n\t" + "pop %%r8\n\t" "pop %%rbp\n\t" "pop %%rsi\n\t" "pop %%rdi\n\t" From c5c0973b448aa7093e85c034db13e2c105f7769c Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Fri, 20 Oct 2017 15:15:58 -0700 Subject: [PATCH 104/109] umip_ldt_64: remove unneeded arguments in test prep code test_fs and test_gs are not used in the assembly code that prepares everything for testing. Remove them. Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/umip_ldt_64.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/umip_ldt_64.c b/meta-luv/recipes-core/umip/files/umip_ldt_64.c index 7ba03de7bcf..df2551c7dbe 100644 --- a/meta-luv/recipes-core/umip/files/umip_ldt_64.c +++ b/meta-luv/recipes-core/umip/files/umip_ldt_64.c @@ -153,7 +153,7 @@ int main(void) "push %%r15\n\t" /* set new data segment */ /* jump to test code */ - "call *%2\n\t" + "call *%0\n\t" /* After running tests, we return here */ "finish_testing:\n\t" /* restore everything */ @@ -173,7 +173,7 @@ int main(void) "pop %%rbx\n\t" "pop %%rax\n\t" : - :"m"(test_fs), "m"(test_gs), "m"(code) + :"m"(code) ); cleanup_segments(); From 2cb5c5992f58e559df5d2548601dfdf75d3597f6 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Fri, 20 Oct 2017 15:17:28 -0700 Subject: [PATCH 105/109] umip: README: Start a list of things to do. In order to not forget, start a list of things that need to be done. Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/UMIP_README | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/meta-luv/recipes-core/umip/files/UMIP_README b/meta-luv/recipes-core/umip/files/UMIP_README index bc94f4e9e6a..72fd2b21dff 100644 --- a/meta-luv/recipes-core/umip/files/UMIP_README +++ b/meta-luv/recipes-core/umip/files/UMIP_README @@ -11,6 +11,7 @@ III. Testing UMIP d) Testing 32-bit processes e) Testing output format f) The test programs + g) Things to do ============================================================ I. For the impatient @@ -183,3 +184,9 @@ Refer to section III.a) for the naming convention. purpose of this test is to verify that the emulation code is capable of succesfully computing linear address out of the instruction operand and segment descriptor. + +g) Things to do + +* In umip_ldt_64, test %rsp and %r12 +* Add tests for address size override prefixes +* Add test to include more than one segment override prefix From db2804643886e3caab792ead607126c4560209a7 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Wed, 8 Nov 2017 17:19:25 -0800 Subject: [PATCH 106/109] umip: umip_ldt_16/32: Initialize segment memory correctly In order to verify that code that emulates the UMIP instructions works correctly, we initialize the memory arrays we use for segments with a known pattern. We were failing to correctly initialize data_fs and data_gs. Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/umip_ldt_16.c | 4 ++-- meta-luv/recipes-core/umip/files/umip_ldt_32.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/meta-luv/recipes-core/umip/files/umip_ldt_16.c b/meta-luv/recipes-core/umip/files/umip_ldt_16.c index 6cbfca117bf..cf30ce86414 100644 --- a/meta-luv/recipes-core/umip/files/umip_ldt_16.c +++ b/meta-luv/recipes-core/umip/files/umip_ldt_16.c @@ -139,7 +139,7 @@ static int setup_data_segments() desc.entry_number = DATA_FS_DESC_INDEX; desc.base_addr = (unsigned long)&data_fs; - memset(data_es, 0x66, SEGMENT_SIZE); + memset(data_fs, 0x66, SEGMENT_SIZE); ret = syscall(SYS_modify_ldt, 1, &desc, sizeof(desc)); if (ret) { @@ -150,7 +150,7 @@ static int setup_data_segments() desc.entry_number = DATA_GS_DESC_INDEX; desc.base_addr = (unsigned long)&data_gs; - memset(data_es, 0x55, SEGMENT_SIZE); + memset(data_gs, 0x55, SEGMENT_SIZE); ret = syscall(SYS_modify_ldt, 1, &desc, sizeof(desc)); if (ret) { diff --git a/meta-luv/recipes-core/umip/files/umip_ldt_32.c b/meta-luv/recipes-core/umip/files/umip_ldt_32.c index c45fdd452e1..1e5ea94a08a 100644 --- a/meta-luv/recipes-core/umip/files/umip_ldt_32.c +++ b/meta-luv/recipes-core/umip/files/umip_ldt_32.c @@ -87,7 +87,7 @@ static int setup_data_segments() desc.entry_number = DATA_FS_DESC_INDEX; desc.base_addr = (unsigned long)&data_fs; - memset(data_es, 0x66, SEGMENT_SIZE); + memset(data_fs, 0x66, SEGMENT_SIZE); ret = syscall(SYS_modify_ldt, 1, &desc, sizeof(desc)); if (ret) { @@ -98,7 +98,7 @@ static int setup_data_segments() desc.entry_number = DATA_GS_DESC_INDEX; desc.base_addr = (unsigned long)&data_gs; - memset(data_es, 0x55, SEGMENT_SIZE); + memset(data_gs, 0x55, SEGMENT_SIZE); ret = syscall(SYS_modify_ldt, 1, &desc, sizeof(desc)); if (ret) { From b4fb29e830dc1616d30924174a2a0ebaf2520e76 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Thu, 16 Nov 2017 18:14:29 -0800 Subject: [PATCH 107/109] luv-efi: do not include BITS ramdisk when not building bits This causes grub to fail to load any ramdisk and then Linux does not boot.y Signed-off-by: Ricardo Neri --- meta-luv/classes/luv-efi.bbclass | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/meta-luv/classes/luv-efi.bbclass b/meta-luv/classes/luv-efi.bbclass index 2fa84bc49c2..0dd21550c89 100644 --- a/meta-luv/classes/luv-efi.bbclass +++ b/meta-luv/classes/luv-efi.bbclass @@ -42,7 +42,7 @@ def extra_initrd(d): target = d.getVar('TARGET_ARCH', True) if re.search('86', target): - return '/boot/bitsrd' + return '' else: return '' From 2ea5803d687d48720ed06cf3e28be868ba664111 Mon Sep 17 00:00:00 2001 From: Ricardo Neri Date: Fri, 1 Jun 2018 18:29:23 -0700 Subject: [PATCH 108/109] umip: Add a tool to dump CPUID leaves Add a tool that prints the contents of CPUID leaves. Leaves are selected in the command line as values of the EAX and ECX registers. Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/Makefile | 8 ++- meta-luv/recipes-core/umip/files/cpuid.c | 57 ++++++++++++++++++++ meta-luv/recipes-core/umip/umip-tests_0.1.bb | 2 + 3 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 meta-luv/recipes-core/umip/files/cpuid.c diff --git a/meta-luv/recipes-core/umip/files/Makefile b/meta-luv/recipes-core/umip/files/Makefile index 3b07b1026f0..99b577be520 100644 --- a/meta-luv/recipes-core/umip/files/Makefile +++ b/meta-luv/recipes-core/umip/files/Makefile @@ -1,3 +1,7 @@ +cpuid: CFLAGS += -fno-omit-frame-pointer +cpuid: + $(CC) $(CFLAGS) -o umip_cpuid cpuid.c + x86_64_minimal: CFLAGS += -fno-omit-frame-pointer x86_64_minimal: $(CC) $(CFLAGS) -c umip_utils.c -o umip_utils_64.o @@ -24,7 +28,7 @@ x86_64_emul_all: x86_64_minimal $(CC) $(CFLAGS) -c umip_ldt_64.c $(CC) $(CFLAGS) -o umip_ldt_64_emul_all test_umip_ldt_64.o umip_ldt_64.o umip_utils_64.o -x86_64: x86_64_minimal x86_64_emul_all +x86_64: x86_64_minimal x86_64_emul_all cpuid i686_minimal: CFLAGS += -fno-omit-frame-pointer -m32 i686_minimal: @@ -62,7 +66,7 @@ i686_emul_all: i686_minimal $(CC) $(CFLAGS) -c umip_ldt_16.c $(CC) $(CFLAGS) -o umip_ldt_16_emul_all test_umip_ldt_16.o umip_ldt_16.o umip_utils_32.o -i686: i686_minimal i686_emul_all +i686: i686_minimal i686_emul_all cpuid i586: i686 .PHONY: clean diff --git a/meta-luv/recipes-core/umip/files/cpuid.c b/meta-luv/recipes-core/umip/files/cpuid.c new file mode 100644 index 00000000000..d3b466bd8fc --- /dev/null +++ b/meta-luv/recipes-core/umip/files/cpuid.c @@ -0,0 +1,57 @@ +#include +#include + +#define UMIP_MASK 0x4 + +void usage(void) +{ + printf("Usage:\n"); + printf("cpuid [eax] [ecx]\n"); + printf("Example:\n"); + printf("cpuid 0x4 0x0\n"); +} + +void print_umip(unsigned int ecx) +{ + printf("CPU has UMIP? %s\n", ecx & UMIP_MASK ? "yes" : "no"); +} + +void get_cpuid_leaf(unsigned int eax, unsigned int ecx, unsigned int *new_eax, + unsigned int *new_ecx, unsigned *new_edx) +{ + printf("Getting CPUID leaf for eax=0x%x ecx=0x%x\n", eax, ecx); + + asm volatile("mov %3, %%eax\n" + "mov %4, %%ecx\n" + "cpuid\n" + "mov %%eax, %0\n" + "mov %%ecx, %1\n" + "mov %%edx, %2\n" + : "=m"(*new_eax), "=m"(*new_ecx), "=m"(*new_edx) + : "m"(eax), "m"(ecx) + : "eax", "ecx", "edx"); +} + +int main(int argc, char **argv) +{ + unsigned int eax, ecx, edx; + unsigned int new_eax = 0xabababab, new_ecx = 0xcbcbcbcb; + unsigned int new_edx = 0xefefefef; + + if (argc != 3) { + usage(); + return 1; + } + + eax = strtoul(argv[1], NULL, 0); + ecx = strtoul(argv[2], NULL, 0); + + get_cpuid_leaf(eax, ecx, &new_eax, &new_ecx, &new_edx); + + printf("CPUID Result:\n"); + printf("eax = 0x%x\n", new_eax); + printf("ecx = 0x%x\n", new_ecx); + printf("edx = 0x%x\n", new_edx); + + print_umip(new_ecx); +} diff --git a/meta-luv/recipes-core/umip/umip-tests_0.1.bb b/meta-luv/recipes-core/umip/umip-tests_0.1.bb index 7dbbe319f44..fd39a167611 100644 --- a/meta-luv/recipes-core/umip/umip-tests_0.1.bb +++ b/meta-luv/recipes-core/umip/umip-tests_0.1.bb @@ -18,6 +18,7 @@ SRC_URI="file://umip_test_defs.h \ file://umip_test_gen_16.py \ file://umip_ldt_64.c \ file://umip_test_gen_64.py \ + file://cpuid.c \ file://UMIP_README \ " @@ -70,6 +71,7 @@ do_install() { install -m 755 ${WORKDIR}/umip_ldt_32_emul_all ${D}${bindir} install -m 755 ${WORKDIR}/umip_ldt_16_emul_all ${D}${bindir} fi + install -m 755 ${WORKDIR}/umip_cpuid ${D}${bindir} } From fbb026618a62fea54f3bc8091301d587c7b2a939 Mon Sep 17 00:00:00 2001 From: Pengfei Xu Date: Fri, 18 Oct 2019 17:22:31 -0700 Subject: [PATCH 109/109] umip: umip_utils.c: Avoid umip infinite loop in some abnormal situation There was some abonormal situation, which caused umip test tool in infinite loop, added one judgement: if signal handling reached 1000 times(some normal test would reach 60+ times signal handling), would stop the test and return failed information, which would not cause some confusing in usage. Signed-off-by: Pengfei Xu Signed-off-by: Ricardo Neri --- meta-luv/recipes-core/umip/files/umip_utils.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/meta-luv/recipes-core/umip/files/umip_utils.c b/meta-luv/recipes-core/umip/files/umip_utils.c index 1807e2136ac..b945e620222 100644 --- a/meta-luv/recipes-core/umip/files/umip_utils.c +++ b/meta-luv/recipes-core/umip/files/umip_utils.c @@ -10,6 +10,7 @@ #include "umip_test_defs.h" extern int test_passed, test_failed, test_errors; +static int step_add=0; void (*cleanup)(void) = NULL; sig_atomic_t got_signal, got_sigcode; @@ -145,8 +146,21 @@ void signal_handler(int signum, siginfo_t *info, void *ctx_void) * safely in case we overestimate the size of the instruction. */ #ifdef __x86_64__ + pr_info("Before add 10: ctx->uc_mcontext.gregs[REG_RIP:%d]:%lld\n", + REG_RIP, ctx->uc_mcontext.gregs[REG_RIP]); ctx->uc_mcontext.gregs[REG_RIP] += 10; + pr_info("ctx->uc_mcontext.gregs[REG_RIP:%d]:%lld\n", + REG_RIP, ctx->uc_mcontext.gregs[REG_RIP]); #else + pr_info("Before add 10: ctx->uc_mcontext.gregs[REG_EIP:%d]:%d\n", + REG_EIP, ctx->uc_mcontext.gregs[REG_EIP]); ctx->uc_mcontext.gregs[REG_EIP] += 10; + pr_info("REG_EIP:%d, ctx->uc_mcontext.gregs[REG_EIP]:%d\n", + REG_EIP, ctx->uc_mcontext.gregs[REG_EIP]); #endif + step_add++; + if(step_add > 1000) { + pr_fail(test_failed,"uc_mcontext.gregs[REG_R/EIP] add 1000 times!\n"); + exit(1); + } }