VirtualBox

source: vbox/trunk/include/iprt/asm-arm.h@ 106430

Last change on this file since 106430 was 106430, checked in by vboxsync, 6 weeks ago

include/iprt/asm-arm.h: Make it build on win.arm64, bugref:10392

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.8 KB
Line 
1/** @file
2 * IPRT - ARM Specific Assembly Functions.
3 */
4
5/*
6 * Copyright (C) 2015-2024 Oracle and/or its affiliates.
7 *
8 * This file is part of VirtualBox base platform packages, as
9 * available from https://www.virtualbox.org.
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation, in version 3 of the
14 * License.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, see <https://www.gnu.org/licenses>.
23 *
24 * The contents of this file may alternatively be used under the terms
25 * of the Common Development and Distribution License Version 1.0
26 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
27 * in the VirtualBox distribution, in which case the provisions of the
28 * CDDL are applicable instead of those of the GPL.
29 *
30 * You may elect to license modified versions of this file under the
31 * terms and conditions of either the GPL or the CDDL or both.
32 *
33 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
34 */
35
36#ifndef IPRT_INCLUDED_asm_arm_h
37#define IPRT_INCLUDED_asm_arm_h
38#ifndef RT_WITHOUT_PRAGMA_ONCE
39# pragma once
40#endif
41
42#include <iprt/types.h>
43#if !defined(RT_ARCH_ARM64) && !defined(RT_ARCH_ARM32)
44# error "Not on ARM64 or ARM32"
45#endif
46
47#if defined(_MSC_VER) && RT_INLINE_ASM_USES_INTRIN
48/* Emit the intrinsics at all optimization levels. */
49# include <iprt/sanitized/intrin.h>
50# pragma intrinsic(_ReadStatusReg)
51# pragma intrinsic(_WriteStatusReg)
52# pragma intrinsic(_disable)
53# pragma intrinsic(_enable)
54# pragma intrinsic(__hvc)
55
56/*
57 * MSVC insists on having these defined using ARM64_SYSREG or it will
58 * fail to compile with "error C2284: "_ReadStatusReg": invalid argument for internal function, parameter 1"
59 * if we use our own definitions from iprt/armv8.h
60 */
61# define ARM64_SYSREG_DAIF ARM64_SYSREG(3, 3, 4, 2, 1)
62# define ARM64_SYSREG_CNTFRQ_EL0 ARM64_SYSREG(3, 3, 14, 0, 0)
63# define ARM64_SYSREG_CNTCVT_EL0 ARM64_SYSREG(3, 3, 14, 0, 2)
64#endif
65
66/** @defgroup grp_rt_asm_arm ARM Specific ASM Routines
67 * @ingroup grp_rt_asm
68 * @{
69 */
70
71/**
72 * Gets the content of the CNTVCT_EL0 (or CNTPCT) register.
73 *
74 * @returns CNTVCT_EL0 value.
75 * @note We call this TSC to better fit in with existing x86/amd64 based code.
76 */
77#if RT_INLINE_ASM_EXTERNAL
78DECLASM(uint64_t) ASMReadTSC(void);
79#else
80DECLINLINE(uint64_t) ASMReadTSC(void)
81{
82# if RT_INLINE_ASM_GNU_STYLE
83 uint64_t u64;
84# ifdef RT_ARCH_ARM64
85 __asm__ __volatile__("Lstart_ASMReadTSC_%=:\n\t"
86 "isb\n\t"
87 "mrs %0, CNTVCT_EL0\n\t"
88 : "=r" (u64));
89# else
90 uint32_t u32Spill;
91 uint32_t u32Comp;
92 __asm__ __volatile__("Lstart_ASMReadTSC_%=:\n\t"
93 "isb\n"
94 "Ltry_again_ASMReadTSC_%=:\n\t"
95 "mrrc p15, 0, %[uSpill], %H[uRet], c14\n\t" /* CNTPCT high into uRet.hi */
96 "mrrc p15, 0, %[uRet], %[uSpill], c14\n\t" /* CNTPCT low into uRet.lo */
97 "mrrc p15, 0, %[uSpill], %[uHiComp], c14\n\t" /* CNTPCT high into uHiComp */
98 "cmp %H[uRet], %[uHiComp]\n\t"
99 "b.eq Ltry_again_ASMReadTSC_%=\n\t" /* Redo if high value changed. */
100 : [uRet] "=r" (u64)
101 , "=r" (uHiComp)
102 , "=r" (uSpill));
103# endif
104 return u64;
105
106#elif RT_INLINE_ASM_USES_INTRIN
107 return (uint64_t)_ReadStatusReg(ARM64_SYSREG_CNTCVT_EL0);
108# else
109# error "Unsupported compiler"
110# endif
111}
112#endif
113
114
115/**
116 * Gets the content of the CNTFRQ_EL0 register.
117 *
118 * @returns CNTFRQ_EL0 value.
119 */
120#if RT_INLINE_ASM_EXTERNAL
121DECLASM(uint64_t) ASMReadCntFrqEl0(void);
122#else
123DECLINLINE(uint64_t) ASMReadCntFrqEl0(void)
124{
125# if RT_INLINE_ASM_GNU_STYLE
126 uint64_t u64;
127# ifdef RT_ARCH_ARM64
128 __asm__ __volatile__("Lstart_ASMReadCntFrqEl0_%=:\n\t"
129 "isb\n\t"
130 "mrs %0, CNTFRQ_EL0\n\t"
131 : "=r" (u64));
132# else
133 u64 = 0;
134 __asm__ __volatile__("Lstart_ASMReadCntFrqEl0_%=:\n\t"
135 "isb\n\t"
136 "mrc p15, 0, %[uRet], c14, 0, 0\n\t" /* CNTFRQ */
137 : [uRet] "=r" (u64));
138# endif
139 return u64;
140
141#elif RT_INLINE_ASM_USES_INTRIN
142 return (uint64_t)_ReadStatusReg(ARM64_SYSREG_CNTFRQ_EL0);
143# else
144# error "Unsupported compiler"
145# endif
146}
147#endif
148
149
150/**
151 * Enables interrupts (IRQ and FIQ).
152 */
153#if RT_INLINE_ASM_EXTERNAL
154DECLASM(void) ASMIntEnable(void);
155#else
156DECLINLINE(void) ASMIntEnable(void)
157{
158# if RT_INLINE_ASM_GNU_STYLE
159# ifdef RT_ARCH_ARM64
160 __asm__ __volatile__("Lstart_ASMIntEnable_%=:\n\t"
161 "msr daifclr, #0xf\n\t");
162# else
163 RTCCUINTREG uFlags;
164 __asm__ __volatile__("Lstart_ASMIntEnable_%=:\n\t"
165 "mrs %0, cpsr\n\t"
166 "bic %0, %0, #0xc0\n\t"
167 "msr cpsr_c, %0\n\t"
168 : "=r" (uFlags));
169# endif
170# elif RT_INLINE_ASM_USES_INTRIN
171 _enable();
172# else
173# error "Unsupported compiler"
174# endif
175}
176#endif
177
178
179/**
180 * Disables interrupts (IRQ and FIQ).
181 */
182#if RT_INLINE_ASM_EXTERNAL
183DECLASM(void) ASMIntDisable(void);
184#else
185DECLINLINE(void) ASMIntDisable(void)
186{
187# if RT_INLINE_ASM_GNU_STYLE
188# ifdef RT_ARCH_ARM64
189 __asm__ __volatile__("Lstart_ASMIntDisable_%=:\n\t"
190 "msr daifset, #0xf\n\t");
191# else
192 RTCCUINTREG uFlags;
193 __asm__ __volatile__("Lstart_ASMIntDisable_%=:\n\t"
194 "mrs %0, cpsr\n\t"
195 "orr %0, %0, #0xc0\n\t"
196 "msr cpsr_c, %0\n\t"
197 : "=r" (uFlags));
198# endif
199# elif RT_INLINE_ASM_USES_INTRIN
200 _disable();
201# else
202# error "Unsupported compiler"
203# endif
204}
205#endif
206
207
208/**
209 * Disables interrupts and returns previous uFLAGS.
210 */
211#if RT_INLINE_ASM_EXTERNAL
212DECLASM(RTCCUINTREG) ASMIntDisableFlags(void);
213#else
214DECLINLINE(RTCCUINTREG) ASMIntDisableFlags(void)
215{
216 RTCCUINTREG uFlags;
217# if RT_INLINE_ASM_GNU_STYLE
218# ifdef RT_ARCH_ARM64
219 __asm__ __volatile__("Lstart_ASMIntDisableFlags_%=:\n\t"
220 "mrs %[uRet], daif\n\t"
221 "msr daifset, #0xf\n\t"
222 : [uRet] "=r" (uFlags));
223# else
224 RTCCUINTREG uNewFlags;
225 __asm__ __volatile__("Lstart_ASMIntDisableFlags_%=:\n\t"
226 "mrs %0, cpsr\n\t"
227 "orr %1, %0, #0xc0\n\t"
228 "msr cpsr_c, %1\n\t"
229 : "=r" (uFlags)
230 , "=r" (uNewFlags));
231# endif
232# elif RT_INLINE_ASM_USES_INTRIN
233 uFlags = _ReadStatusReg(ARM64_SYSREG_DAIF);
234 _disable();
235# else
236# error "Unsupported compiler"
237# endif
238 return uFlags;
239}
240#endif
241
242
243/**
244 * Get the CPSR/PSTATE register.
245 * @returns CPSR/PSTATE.
246 */
247#if RT_INLINE_ASM_EXTERNAL
248DECLASM(RTCCUINTREG) ASMGetFlags(void);
249#else
250DECLINLINE(RTCCUINTREG) ASMGetFlags(void)
251{
252 RTCCUINTREG uFlags;
253# if RT_INLINE_ASM_GNU_STYLE
254# ifdef RT_ARCH_ARM64
255 __asm__ __volatile__("Lstart_ASMGetFlags_%=:\n\t"
256 "isb\n\t"
257 "mrs %0, daif\n\t"
258 : "=r" (uFlags));
259# else
260# error "Implementation required for arm32"
261# endif
262# elif RT_INLINE_ASM_USES_INTRIN
263 uFlags = _ReadStatusReg(ARM64_SYSREG_DAIF);
264# else
265# error "Unsupported compiler"
266# endif
267 return uFlags;
268}
269#endif
270
271
272/**
273 * Get the CPSR/PSTATE register.
274 */
275#if RT_INLINE_ASM_EXTERNAL
276DECLASM(void) ASMSetFlags(RTCCUINTREG uFlags);
277#else
278DECLINLINE(void) ASMSetFlags(RTCCUINTREG uFlags)
279{
280# if RT_INLINE_ASM_GNU_STYLE
281# ifdef RT_ARCH_ARM64
282 __asm__ __volatile__("Lstart_ASMSetFlags_%=:\n\t"
283 "isb\n\t"
284 "msr daif, %[uFlags]\n\t"
285 : : [uFlags] "r" (uFlags));
286# else
287# error "Implementation required for arm32"
288# endif
289# elif RT_INLINE_ASM_USES_INTRIN
290 _WriteStatusReg(ARM64_SYSREG_DAIF, uFlags);
291# else
292# error "Unsupported compiler"
293# endif
294}
295#endif
296
297
298/**
299 * Are interrupts enabled?
300 *
301 * @returns true / false.
302 */
303DECLINLINE(bool) ASMIntAreEnabled(void)
304{
305 return ASMGetFlags() & 0xc0 /* IRQ and FIQ bits */ ? true : false;
306}
307
308
309#if 0 /* Later */
310/**
311 * Issue HVC call with a single argument.
312 */
313#if RT_INLINE_ASM_EXTERNAL
314DECLASM(void) ASMHvc(uint16_t u16Imm, uint32_t u32Arg0);
315#else
316DECLINLINE(void) ASMHvc(uint16_t u16Imm, uint32_t u32Arg0)
317{
318# if RT_INLINE_ASM_GNU_STYLE
319# error "Later"
320# elif RT_INLINE_ASM_USES_INTRIN
321 __hvc(u16Imm, u32Val);
322# else
323# error "Unsupported compiler"
324# endif
325}
326#endif
327#endif
328
329
330/**
331 * Halts the CPU until interrupted.
332 */
333#if RT_INLINE_ASM_EXTERNAL || defined(_MSC_VER)
334DECLASM(void) ASMHalt(void);
335#else
336DECLINLINE(void) ASMHalt(void)
337{
338# if RT_INLINE_ASM_GNU_STYLE
339 __asm__ __volatile__ ("Lstart_ASMHalt_%=:\n\t"
340 "wfi\n\t"); /* wait for interrupt */
341# else
342# error "Unsupported compiler"
343# endif
344}
345#endif
346
347#if 0
348/**
349 * Gets the CPU ID of the current CPU.
350 *
351 * @returns the CPU ID.
352 * @note the name of this method is a bit misleading but serves the purpose
353 * and prevents #ifdef orgies in other places.
354 */
355#if RT_INLINE_ASM_EXTERNAL
356DECLASM(uint8_t) ASMGetApicId(void);
357#else
358DECLINLINE(uint8_t) ASMGetApicId(void)
359{
360# if RT_INLINE_ASM_GNU_STYLE
361 RTCCUINTREG uCpuId;
362 __asm__ ("Lstart_ASMGetApicId_%=:\n\t"
363 "mrc p15, 0, %0, c0, c0, 5\n\t" /* CPU ID Register, privileged */
364 : "=r" (uCpuId));
365 return uCpuId;
366# else
367# error "Unsupported compiler"
368# endif
369}
370#endif
371#endif
372
373#if 0
374
375/**
376 * Invalidate page.
377 *
378 * @param pv Address of the page to invalidate.
379 */
380#if RT_INLINE_ASM_EXTERNAL
381DECLASM(void) ASMInvalidatePage(void *pv);
382#else
383DECLINLINE(void) ASMInvalidatePage(void *pv)
384{
385# if RT_INLINE_ASM_GNU_STYLE
386
387# else
388# error "Unsupported compiler"
389# endif
390}
391#endif
392
393
394/**
395 * Write back the internal caches and invalidate them.
396 */
397#if RT_INLINE_ASM_EXTERNAL
398DECLASM(void) ASMWriteBackAndInvalidateCaches(void);
399#else
400DECLINLINE(void) ASMWriteBackAndInvalidateCaches(void)
401{
402# if RT_INLINE_ASM_GNU_STYLE
403
404# else
405# error "Unsupported compiler"
406# endif
407}
408#endif
409
410
411/**
412 * Invalidate internal and (perhaps) external caches without first
413 * flushing dirty cache lines. Use with extreme care.
414 */
415#if RT_INLINE_ASM_EXTERNAL
416DECLASM(void) ASMInvalidateInternalCaches(void);
417#else
418DECLINLINE(void) ASMInvalidateInternalCaches(void)
419{
420# if RT_INLINE_ASM_GNU_STYLE
421
422# else
423# error "Unsupported compiler"
424# endif
425}
426#endif
427
428#endif
429
430
431/** @} */
432#endif /* !IPRT_INCLUDED_asm_arm_h */
433
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette