1# Using LLDB On AArch64 Linux 2 3This page explains the details of debugging certain AArch64 extensions using 4LLDB. If something is not mentioned here, it likely works as you would expect. 5 6This is not a replacement for ptrace and Linux Kernel documentation. This covers 7how LLDB has chosen to use those things and how that effects your experience as 8a user. 9 10## Scalable Vector Extension (SVE) 11 12See [here](https://developer.arm.com/Architectures/Scalable%20Vector%20Extensions) 13to learn about the extension and [here](https://kernel.org/doc/html/latest/arch/arm64/sve.html) 14for the Linux Kernel's handling of it. 15 16In LLDB you will be able to see the following new registers: 17 18* `z0-z31` vector registers, each one has size equal to the vector length. 19* `p0-p15` predicate registers, each one containing 1 bit per byte in the vector 20 length. So each one is `vector length in bits / 8` bits. 21* `ffr` the first fault register, same size as a predicate register. 22* `vg`, the vector length in "granules". Each granule is 8 bytes. 23 24``` 25 Scalable Vector Extension Registers: 26 vg = 0x0000000000000002 27 z0 = {0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01 <...> } 28 <...> 29 p0 = {0xff 0xff} 30 <...> 31 ffr = {0xff 0xff} 32``` 33 34The example above has a vector length of 16 bytes. Within LLDB you will always 35see "vg" as in the `vg` register, which is 2 in this case (8*2 = 16). 36Elsewhere in kernel code or applications, you may see "vq" which is the vector 37length in quadwords (16 bytes). Where you see "vl", it is in bytes. 38 39While you can count the size of a P or Z register, it is intended that `vg` be 40used to find the current vector length. 41 42### Changing the Vector Length 43 44The `vg` register can be written during a debug session. Writing the current 45vector length changes nothing. If you increase the vector length, the registers 46will likely be reset to 0. If you decrease it, LLDB will truncate the Z 47registers but everything else will be reset to 0. 48 49You should not assume that SVE state after changing the vector length is in any 50way the same as it was previously. Whether that is done from within the 51debuggee, or by LLDB. If you need to change the vector length, do so before a 52function's first use of SVE. 53 54### Z Register Presentation 55 56LLDB makes no attempt to predict how SVE Z registers will be used. Since LLDB 57does not know what sort of elements future instructions will interpret the 58register as. It therefore does not change the visualisation of the register 59and always defaults to showing a vector of byte sized elements. 60 61If you know what format you are going to use, give a format option: 62``` 63 (lldb) register read z0 -f uint32_t[] 64 z0 = {0x01010101 0x01010101 0x01010101 0x01010101} 65``` 66 67### FPSIMD and SVE Modes 68 69Prior to the debugee's first use of SVE, it is in what the Linux Kernel terms 70SIMD mode. Only the FPU is being used. In this state LLDB will still show the 71SVE registers however the values are simply the FPU values zero extended up to 72the vector length. 73 74On first access to SVE, the process goes into SVE mode. Now the Z values are 75in the real Z registers. 76 77You can also trigger this with LLDB by writing to an SVE register. Note that 78there is no way to undo this change from within LLDB. However, the debugee 79itself could do something to end up back in SIMD mode. 80 81### Expression evaluation 82 83If you evaluate an expression, all SVE state is saved prior to, and restored 84after the expression has been evaluated. Including the register values and 85vector length. 86 87## Scalable Matrix Extension (SME) 88 89See [here](https://community.arm.com/arm-community-blogs/b/architectures-and-processors-blog/posts/scalable-matrix-extension-armv9-a-architecture) 90to learn about the extension and [here](https://kernel.org/doc/html/latest/arch/arm64/sme.html) 91for the Linux Kernel's handling of it. 92 93SME adds a "Streaming Mode" to SVE, and this mode has its own vector length 94known as the "Streaming Vector Length". 95 96In LLDB you will see the following new registers: 97 98* `tpidr2`, an extra per thread pointer reserved for use by the SME ABI. 99 This is not scalable, just pointer sized aka 64 bit. 100* `z0-z31` streaming SVE registers. These have the same names as the 101 non-streaming registers and therefore you will only see the active set in 102 LLDB. You cannot read or write the inactive mode's registers. Their size 103 is the same as the streaming vector length. 104* `za` the Array Storage register. The "Matrix" part of "Scalable Matrix 105 Extension". This is a square made up of rows of length equal to the streaming 106 vector length (svl). Meaning that the total size is svl * svl. 107* `svcr` the Streaming Vector Control Register. This is actually a pseduo 108 register but it matches the content of the architecturaly defined `SVCR`. 109 This is the register you should use to check whether streaming mode and/or 110 `za` is active. This register is read only. 111* `svg` the streaming vector length in granules. This value is not connected 112 to the vector length of non-streaming mode and may change independently. This 113 register is read only. 114 115```{note} 116 While in non-streaming mode, the `vg` register shows the non-streaming 117 vector length, and the `svg` register shows the streaming vector length. 118 When in streaming mode, both `vg` and `svg` show the streaming mode vector 119 length. Therefore it is not possible at this time to read the non-streaming 120 vector length within LLDB, while in streaming mode. This is a limitation of 121 the LLDB implementation not the architecture, which stores both lengths 122 independently. 123``` 124 125In the example below, the streaming vector length is 16 bytes and we are in 126streaming mode. Note that bits 0 and 1 of `svcr` are set, indicating that we 127are in streaming mode and ZA is active. `vg` and `svg` report the same value 128as `vg` is showing the streaming mode vector length: 129``` 130 Scalable Vector Extension Registers: 131 vg = 0x0000000000000002 132 z0 = {0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01 <...> } 133 <...> 134 p0 = {0xff 0xff} 135 <...> 136 ffr = {0xff 0xff} 137 138 <...> 139 140 Thread Local Storage Registers: 141 tpidr = 0x0000fffff7ff4320 142 tpidr2 = 0x1122334455667788 143 144 Scalable Matrix Extension Registers: 145 svg = 0x0000000000000002 146 svcr = 0x0000000000000003 147 za = {0x00 <...> 0x00} 148``` 149 150### Changing the Streaming Vector Length 151 152To reduce complexity for LLDB, `svg` is read only. This means that you can 153only change the streaming vector length using LLDB when the debugee is in 154streaming mode. 155 156As for non-streaming SVE, doing so will essentially make the content of the SVE 157registers undefined. It will also disable ZA, which follows what the Linux 158Kernel does. 159 160### Visibility of an Inactive ZA Register 161 162LLDB does not handle registers that can come and go at runtime (SVE changes 163size but it does not disappear). Therefore when `za` is not enabled, LLDB 164will return a block of 0s instead. This block will match the expected size of 165`za`: 166``` 167 (lldb) register read za svg svcr 168 za = {0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 <...> } 169 svg = 0x0000000000000002 170 svcr = 0x0000000000000001 171``` 172 173Note that `svcr` bit 2 is not set, meaning `za` is inactive. 174 175If you were to write to `za` from LLDB, `za` will be made active. There is 176no way from within LLDB to reverse this change. As for changing the vector 177length, the debugee could still do something that would disable `za` again. 178 179If you want to know whether `za` is active or not, refer to bit 2 of the 180`svcr` register, otherwise known as `SVCR.ZA`. 181 182### ZA Register Presentation 183 184As for SVE, LLDB does not know how the debugee will use `za`, and therefore 185does not know how it would be best to display it. At any time any given 186instruction could interpret its contents as many kinds and sizes of data. 187 188So LLDB will default to showing `za` as one large vector of individual bytes. 189You can override this with a format option (see the SVE example above). 190 191### Expression Evaluation 192 193The mode (streaming or non-streaming), streaming vector length and ZA state will 194be restored after expression evaluation. On top of all the things saved for SVE 195in general. 196 197## Scalable Matrix Extension (SME2) 198 199The Scalable Matrix Extension 2 is documented in the same architecture 200specification as SME, and covered by the same kernel documentation page as SME. 201 202SME2 adds 1 new register, `zt0`. This register is a fixed size 512 bit 203register that is used by new instructions added in SME2. It is shown in LLDB in 204the existing SME register set. 205 206`zt0` can be active or inactive, as `za` can. The same `SVCR.ZA` bit 207controls this. An inactive `zt0` is shown as 0s, like `za` is. Though in 208`zt0`'s case, LLDB does not need to fake the value. Ptrace already returns a 209block of 0s for an inactive `zt0`. 210 211Like `za`, writing to an inactive `zt0` will enable it and `za`. This can 212be done from within LLDB. If the write is instead to `za`, `zt0` becomes 213active but with a value of all 0s. 214 215Since `svcr` is read only, there is no way at this time to deactivate the 216registers from within LLDB (though of course a running process can still do 217this). 218 219To check whether `zt0` is active, refer to `SVCR.ZA` and not to the value of 220`zt0`. 221 222### ZT0 Register Presentation 223 224As for `za`, the meaning of `zt0` depends on the instructions used with it, 225so LLDB does not attempt to guess this and defaults to showing it as a vector of 226bytes. 227 228### Expression Evaluation 229 230`zt0`'s value and whether it is active or not will be saved prior to 231expression evaluation and restored afterwards. 232 233## Guarded Control Stack Extension (GCS) 234 235GCS support includes the following new registers: 236 237* `gcs_features_enabled` 238* `gcs_features_locked` 239* `gcspr_el0` 240 241These map to the registers ptrace provides. The first two have a `gcs_` 242prefix added as their names are too generic without it. 243 244When the GCS is enabled the kernel allocates a memory region for it. This region 245has a special attribute that LLDB will detect and presents like this: 246``` 247 (lldb) memory region --all 248 <...> 249 [0x0000fffff7a00000-0x0000fffff7e00000) rw- 250 shadow stack: yes 251 [0x0000fffff7e00000-0x0000fffff7e10000) --- 252``` 253 254`shadow stack` is a generic term used in the kernel for secure stack 255extensions like GCS. 256 257### Expression Evaluation 258 259To execute an expression when GCS is enabled, LLDB must push the return 260address of the expression wrapper (usually the entry point of the program) 261to the Guarded Control Stack. It does this by decrementing `gcspr_el0` and 262writing to the location now pointed to by `gcspr_el0` (instead of using the 263GCS push instructions). 264 265After an expression finishes, LLDB will restore the contents of all 3 266GCS registers, apart from the enable bit of `gcs_features_enabled`. This is 267because there are limits on how often and from where you can set this 268bit. 269 270GCS cannot be enabled from ptrace and it is expected that a process which 271has enabled and then disabled GCS, will not enable it again. The simplest 272choice was to not restore the enable bit at all. It is up to the user or 273program to manage that bit. 274 275The return address that LLDB pushed onto the Guarded Control Stack will be left 276in place. As will any values that were pushed to the stack by functions run 277during the expression. 278 279When the process resumes, `gcspr_el0` will be pointing to the original entry 280on the guarded control stack. So the other values will have no effect and 281likely be overwritten by future function calls. 282 283LLDB does not track and restore changes to general memory during expressions, 284so not restoring the GCS contents fits with the current behaviour. 285 286Note that if GCS is disabled and an expression enables it, LLDB will not 287be able to setup the return address and it is up to that expression to do that 288if it wants to return to LLDB correctly. 289 290If it does not do this, the expression will fail and although most process 291state will be restored, GCS will be left enabled. Which means that the program 292is very unlikely to be able to progress. 293