OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Crashpad Authors. All rights reserved. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 #include "snapshot/cpu_context.h" |
| 16 |
| 17 #include <stdint.h> |
| 18 #include <string.h> |
| 19 |
| 20 #include "gtest/gtest.h" |
| 21 |
| 22 namespace crashpad { |
| 23 namespace test { |
| 24 namespace { |
| 25 |
| 26 enum ExponentValue { |
| 27 kExponentAllZero = 0, |
| 28 kExponentAllOne, |
| 29 kExponentNormal, |
| 30 }; |
| 31 |
| 32 enum FractionValue { |
| 33 kFractionAllZero = 0, |
| 34 kFractionNormal, |
| 35 }; |
| 36 |
| 37 //! \brief Initializes an x87 register to a known bit pattern. |
| 38 //! |
| 39 //! \param[out] st_mm The x87 register to initialize. The reserved portion of |
| 40 //! the register is always zeroed out. |
| 41 //! \param[in] exponent_value The bit pattern to use for the exponent. If this |
| 42 //! is kExponentAllZero, the sign bit will be set to `1`, and if this is |
| 43 //! kExponentAllOne, the sign bit will be set to `0`. This tests that the |
| 44 //! implementation doesn’t erroneously consider the sign bit to be part of |
| 45 //! the exponent. This may also be kExponentNormal, indicating that the |
| 46 //! exponent shall neither be all zeroes nor all ones. |
| 47 //! \param[in] j_bit The value to use for the “J bit” (“integer bit”). |
| 48 //! \param[in] fraction_value If kFractionAllZero, the fraction will be zeroed |
| 49 //! out. If kFractionNormal, the fraction will not be all zeroes. |
| 50 void SetX87Register(CPUContextX86::X87OrMMXRegister* st_mm, |
| 51 ExponentValue exponent_value, |
| 52 bool j_bit, |
| 53 FractionValue fraction_value) { |
| 54 switch (exponent_value) { |
| 55 case kExponentAllZero: |
| 56 st_mm->st[9] = 0x80; |
| 57 st_mm->st[8] = 0; |
| 58 break; |
| 59 case kExponentAllOne: |
| 60 st_mm->st[9] = 0x7f; |
| 61 st_mm->st[8] = 0xff; |
| 62 break; |
| 63 case kExponentNormal: |
| 64 st_mm->st[9] = 0x55; |
| 65 st_mm->st[8] = 0x55; |
| 66 break; |
| 67 } |
| 68 |
| 69 uint8_t fraction_pattern = fraction_value == kFractionAllZero ? 0 : 0x55; |
| 70 memset(&st_mm->st[0], fraction_pattern, 8); |
| 71 |
| 72 if (j_bit) { |
| 73 st_mm->st[7] |= 0x80; |
| 74 } else { |
| 75 st_mm->st[7] &= ~0x80; |
| 76 } |
| 77 |
| 78 memset(st_mm->st_reserved, 0, sizeof(st_mm->st_reserved)); |
| 79 } |
| 80 |
| 81 TEST(CPUContextX86, FxsaveToFsaveTagWord) { |
| 82 // The fsave tag word uses bit pattern 00 for valid, 01 for zero, 10 for |
| 83 // “special”, and 11 for empty. Like the fxsave tag word, it is arranged by |
| 84 // physical register. The fxsave tag word determines whether a register is |
| 85 // empty, and analysis of the x87 register content distinguishes between |
| 86 // valid, zero, and special. In the initializations below, comments show |
| 87 // whether a register is expected to be considered valid, zero, or special, |
| 88 // except where the tag word is expected to indicate that it is empty. Each |
| 89 // combination appears twice: once where the fxsave tag word indicates a |
| 90 // nonempty register, and once again where it indicates an empty register. |
| 91 |
| 92 uint16_t fsw = 0 << 11; // top = 0: logical 0-7 maps to physical 0-7 |
| 93 uint8_t fxsave_tag = 0x0f; // physical 4-7 (logical 4-7) empty |
| 94 CPUContextX86::X87OrMMXRegister st_mm[8]; |
| 95 SetX87Register(&st_mm[0], kExponentNormal, false, kFractionNormal); // spec. |
| 96 SetX87Register(&st_mm[1], kExponentNormal, true, kFractionNormal); // valid |
| 97 SetX87Register(&st_mm[2], kExponentNormal, false, kFractionAllZero); // spec. |
| 98 SetX87Register(&st_mm[3], kExponentNormal, true, kFractionAllZero); // valid |
| 99 SetX87Register(&st_mm[4], kExponentNormal, false, kFractionNormal); |
| 100 SetX87Register(&st_mm[5], kExponentNormal, true, kFractionNormal); |
| 101 SetX87Register(&st_mm[6], kExponentNormal, false, kFractionAllZero); |
| 102 SetX87Register(&st_mm[7], kExponentNormal, true, kFractionAllZero); |
| 103 EXPECT_EQ(0xff22, |
| 104 CPUContextX86::FxsaveToFsaveTagWord(fsw, fxsave_tag, st_mm)); |
| 105 |
| 106 fsw = 2 << 11; // top = 2: logical 0-7 maps to physical 2-7, 0-1 |
| 107 fxsave_tag = 0xf0; // physical 0-3 (logical 6-7, 0-1) empty |
| 108 SetX87Register(&st_mm[0], kExponentAllZero, false, kFractionNormal); |
| 109 SetX87Register(&st_mm[1], kExponentAllZero, true, kFractionNormal); |
| 110 SetX87Register(&st_mm[2], kExponentAllZero, false, kFractionAllZero); // zero |
| 111 SetX87Register(&st_mm[3], kExponentAllZero, true, kFractionAllZero); // spec. |
| 112 SetX87Register(&st_mm[4], kExponentAllZero, false, kFractionNormal); // spec. |
| 113 SetX87Register(&st_mm[5], kExponentAllZero, true, kFractionNormal); // spec. |
| 114 SetX87Register(&st_mm[6], kExponentAllZero, false, kFractionAllZero); |
| 115 SetX87Register(&st_mm[7], kExponentAllZero, true, kFractionAllZero); |
| 116 EXPECT_EQ(0xa9ff, |
| 117 CPUContextX86::FxsaveToFsaveTagWord(fsw, fxsave_tag, st_mm)); |
| 118 |
| 119 fsw = 5 << 11; // top = 5: logical 0-7 maps to physical 5-7, 0-4 |
| 120 fxsave_tag = 0x5a; // physical 0, 2, 5, and 7 (logical 5, 0, 2, and 3) empty |
| 121 SetX87Register(&st_mm[0], kExponentAllOne, false, kFractionNormal); |
| 122 SetX87Register(&st_mm[1], kExponentAllOne, true, kFractionNormal); // spec. |
| 123 SetX87Register(&st_mm[2], kExponentAllOne, false, kFractionAllZero); |
| 124 SetX87Register(&st_mm[3], kExponentAllOne, true, kFractionAllZero); |
| 125 SetX87Register(&st_mm[4], kExponentAllOne, false, kFractionNormal); // spec. |
| 126 SetX87Register(&st_mm[5], kExponentAllOne, true, kFractionNormal); |
| 127 SetX87Register(&st_mm[6], kExponentAllOne, false, kFractionAllZero); // spec. |
| 128 SetX87Register(&st_mm[7], kExponentAllOne, true, kFractionAllZero); // spec. |
| 129 EXPECT_EQ(0xeebb, |
| 130 CPUContextX86::FxsaveToFsaveTagWord(fsw, fxsave_tag, st_mm)); |
| 131 |
| 132 // This set set is just a mix of all of the possible tag types in a single |
| 133 // register file. |
| 134 fsw = 1 << 11; // top = 1: logical 0-7 maps to physical 1-7, 0 |
| 135 fxsave_tag = 0x1f; // physical 5-7 (logical 4-6) empty |
| 136 SetX87Register(&st_mm[0], kExponentNormal, true, kFractionAllZero); // valid |
| 137 SetX87Register(&st_mm[1], kExponentAllZero, false, kFractionAllZero); // zero |
| 138 SetX87Register(&st_mm[2], kExponentAllOne, true, kFractionAllZero); // spec. |
| 139 SetX87Register(&st_mm[3], kExponentAllOne, true, kFractionNormal); // spec. |
| 140 SetX87Register(&st_mm[4], kExponentAllZero, false, kFractionAllZero); |
| 141 SetX87Register(&st_mm[5], kExponentAllZero, false, kFractionAllZero); |
| 142 SetX87Register(&st_mm[6], kExponentAllZero, false, kFractionAllZero); |
| 143 SetX87Register(&st_mm[7], kExponentNormal, true, kFractionNormal); // valid |
| 144 EXPECT_EQ(0xfe90, |
| 145 CPUContextX86::FxsaveToFsaveTagWord(fsw, fxsave_tag, st_mm)); |
| 146 } |
| 147 |
| 148 } // namespace |
| 149 } // namespace test |
| 150 } // namespace crashpad |
OLD | NEW |