brintos

brintos / linux-shallow public Read only

0
0
Text · 6.7 KiB · ab07242 Raw
196 lines · plain
1.. SPDX-License-Identifier: GPL-2.0-or-later2 3==========================================4DEXCR (Dynamic Execution Control Register)5==========================================6 7Overview8========9 10The DEXCR is a privileged special purpose register (SPR) introduced in11PowerPC ISA 3.1B (Power10) that allows per-cpu control over several dynamic12execution behaviours. These behaviours include speculation (e.g., indirect13branch target prediction) and enabling return-oriented programming (ROP)14protection instructions.15 16The execution control is exposed in hardware as up to 32 bits ('aspects') in17the DEXCR. Each aspect controls a certain behaviour, and can be set or cleared18to enable/disable the aspect. There are several variants of the DEXCR for19different purposes:20 21DEXCR22    A privileged SPR that can control aspects for userspace and kernel space23HDEXCR24    A hypervisor-privileged SPR that can control aspects for the hypervisor and25    enforce aspects for the kernel and userspace.26UDEXCR27    An optional ultravisor-privileged SPR that can control aspects for the ultravisor.28 29Userspace can examine the current DEXCR state using a dedicated SPR that30provides a non-privileged read-only view of the userspace DEXCR aspects.31There is also an SPR that provides a read-only view of the hypervisor enforced32aspects, which ORed with the userspace DEXCR view gives the effective DEXCR33state for a process.34 35 36Configuration37=============38 39prctl40-----41 42A process can control its own userspace DEXCR value using the43``PR_PPC_GET_DEXCR`` and ``PR_PPC_SET_DEXCR`` pair of44:manpage:`prctl(2)` commands. These calls have the form::45 46    prctl(PR_PPC_GET_DEXCR, unsigned long which, 0, 0, 0);47    prctl(PR_PPC_SET_DEXCR, unsigned long which, unsigned long ctrl, 0, 0);48 49The possible 'which' and 'ctrl' values are as follows. Note there is no relation50between the 'which' value and the DEXCR aspect's index.51 52.. flat-table::53   :header-rows: 154   :widths: 2 7 155 56   * - ``prctl()`` which57     - Aspect name58     - Aspect index59 60   * - ``PR_PPC_DEXCR_SBHE``61     - Speculative Branch Hint Enable (SBHE)62     - 063 64   * - ``PR_PPC_DEXCR_IBRTPD``65     - Indirect Branch Recurrent Target Prediction Disable (IBRTPD)66     - 367 68   * - ``PR_PPC_DEXCR_SRAPD``69     - Subroutine Return Address Prediction Disable (SRAPD)70     - 471 72   * - ``PR_PPC_DEXCR_NPHIE``73     - Non-Privileged Hash Instruction Enable (NPHIE)74     - 575 76.. flat-table::77   :header-rows: 178   :widths: 2 879 80   * - ``prctl()`` ctrl81     - Meaning82 83   * - ``PR_PPC_DEXCR_CTRL_EDITABLE``84     - This aspect can be configured with PR_PPC_SET_DEXCR (get only)85 86   * - ``PR_PPC_DEXCR_CTRL_SET``87     - This aspect is set / set this aspect88 89   * - ``PR_PPC_DEXCR_CTRL_CLEAR``90     - This aspect is clear / clear this aspect91 92   * - ``PR_PPC_DEXCR_CTRL_SET_ONEXEC``93     - This aspect will be set after exec / set this aspect after exec94 95   * - ``PR_PPC_DEXCR_CTRL_CLEAR_ONEXEC``96     - This aspect will be clear after exec / clear this aspect after exec97 98Note that99 100* which is a plain value, not a bitmask. Aspects must be worked with individually.101 102* ctrl is a bitmask. ``PR_PPC_GET_DEXCR`` returns both the current and onexec103  configuration. For example, ``PR_PPC_GET_DEXCR`` may return104  ``PR_PPC_DEXCR_CTRL_EDITABLE | PR_PPC_DEXCR_CTRL_SET |105  PR_PPC_DEXCR_CTRL_CLEAR_ONEXEC``. This would indicate the aspect is currently106  set, it will be cleared when you run exec, and you can change this with the107  ``PR_PPC_SET_DEXCR`` prctl.108 109* The set/clear terminology refers to setting/clearing the bit in the DEXCR.110  For example::111 112      prctl(PR_PPC_SET_DEXCR, PR_PPC_DEXCR_IBRTPD, PR_PPC_DEXCR_CTRL_SET, 0, 0);113 114  will set the IBRTPD aspect bit in the DEXCR, causing indirect branch prediction115  to be disabled.116 117* The status returned by ``PR_PPC_GET_DEXCR`` represents what value the process118  would like applied. It does not include any alternative overrides, such as if119  the hypervisor is enforcing the aspect be set. To see the true DEXCR state120  software should read the appropriate SPRs directly.121 122* The aspect state when starting a process is copied from the parent's state on123  :manpage:`fork(2)`. The state is reset to a fixed value on124  :manpage:`execve(2)`. The PR_PPC_SET_DEXCR prctl() can control both of these125  values.126 127* The ``*_ONEXEC`` controls do not change the current process's DEXCR.128 129Use ``PR_PPC_SET_DEXCR`` with one of ``PR_PPC_DEXCR_CTRL_SET`` or130``PR_PPC_DEXCR_CTRL_CLEAR`` to edit a given aspect.131 132Common error codes for both getting and setting the DEXCR are as follows:133 134.. flat-table::135   :header-rows: 1136   :widths: 2 8137 138   * - Error139     - Meaning140 141   * - ``EINVAL``142     - The DEXCR is not supported by the kernel.143 144   * - ``ENODEV``145     - The aspect is not recognised by the kernel or not supported by the146       hardware.147 148``PR_PPC_SET_DEXCR`` may also report the following error codes:149 150.. flat-table::151   :header-rows: 1152   :widths: 2 8153 154   * - Error155     - Meaning156 157   * - ``EINVAL``158     - The ctrl value contains unrecognised flags.159 160   * - ``EINVAL``161     - The ctrl value contains mutually conflicting flags (e.g.,162       ``PR_PPC_DEXCR_CTRL_SET | PR_PPC_DEXCR_CTRL_CLEAR``)163 164   * - ``EPERM``165     - This aspect cannot be modified with prctl() (check for the166       PR_PPC_DEXCR_CTRL_EDITABLE flag with PR_PPC_GET_DEXCR).167 168   * - ``EPERM``169     - The process does not have sufficient privilege to perform the operation.170       For example, clearing NPHIE on exec is a privileged operation (a process171       can still clear its own NPHIE aspect without privileges).172 173This interface allows a process to control its own DEXCR aspects, and also set174the initial DEXCR value for any children in its process tree (up to the next175child to use an ``*_ONEXEC`` control). This allows fine-grained control over the176default value of the DEXCR, for example allowing containers to run with different177default values.178 179 180coredump and ptrace181===================182 183The userspace values of the DEXCR and HDEXCR (in this order) are exposed under184``NT_PPC_DEXCR``. These are each 64 bits and readonly, and are intended to185assist with core dumps. The DEXCR may be made writable in future. The top 32186bits of both registers (corresponding to the non-userspace bits) are masked off.187 188If the kernel config ``CONFIG_CHECKPOINT_RESTORE`` is enabled, then189``NT_PPC_HASHKEYR`` is available and exposes the HASHKEYR value of the process190for reading and writing. This is a tradeoff between increased security and191checkpoint/restore support: a process should normally have no need to know its192secret key, but restoring a process requires setting its original key. The key193therefore appears in core dumps, and an attacker may be able to retrieve it from194a coredump and effectively bypass ROP protection on any threads that share this195key (potentially all threads from the same parent that have not run ``exec()``).196