-
Notifications
You must be signed in to change notification settings - Fork 613
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[include][reg.h] define new mmio_read/write accessors
To work properly with some hypervisors on various architectures (ARM, ARM64, x86), add global routines to allow access to MMIO registers via architecturally defined accessors. Add accessors for ARM, ARM64, and x86-32/64. Have the other arches default to just using whatever the compiler emits. Will need to generally move things off the legacy REG*() accessors since they're really not safe going forward with what compilers emit.
- Loading branch information
Showing
9 changed files
with
275 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright (c) 2024 Travis Geiselbrecht | ||
* | ||
* Use of this source code is governed by a MIT-style | ||
* license that can be found in the LICENSE file or at | ||
* https://opensource.org/licenses/MIT | ||
*/ | ||
#pragma once | ||
|
||
#include <stdint.h> | ||
|
||
// Accessor routines for this architecture, used in include/reg.h | ||
// | ||
// Simple load/store instructions with a basic addressing mode of either | ||
// [reg], [reg, #imm] or [reg, reg], with no writeback are guaranteed to be | ||
// fully decoded into ESR_EL2 for hypervisor assist. | ||
|
||
#define ARCH_MMIO_READ_WRITE_OVERRIDE 1 | ||
|
||
#define _ARCH_MMIO_READ8(addr) ({ \ | ||
uint8_t val; \ | ||
__asm__ volatile("ldrb %0, %1" : "=r"(val) : "m"(*(addr)) : "memory"); \ | ||
val; \ | ||
}) | ||
#define _ARCH_MMIO_READ16(addr) ({ \ | ||
uint16_t val; \ | ||
__asm__ volatile("ldrh %0, %1" : "=r"(val) : "m"(*(addr)) : "memory"); \ | ||
val; \ | ||
}) | ||
#define _ARCH_MMIO_READ32(addr) ({ \ | ||
uint32_t val; \ | ||
__asm__ volatile("ldr %0, %1" : "=r"(val) : "m"(*(addr)) : "memory"); \ | ||
val; \ | ||
}) | ||
|
||
#define _ARCH_MMIO_WRITE8(addr, val) \ | ||
__asm__ volatile("strb %1, %0" : "=m"(*(addr)) : "r"(val) : "memory") | ||
#define _ARCH_MMIO_WRITE16(addr, val) \ | ||
__asm__ volatile("strh %1, %0" : "=m"(*(addr)) : "r"(val) : "memory") | ||
#define _ARCH_MMIO_WRITE32(addr, val) \ | ||
__asm__ volatile("str %1, %0" : "=m"(*(addr)) : "r"(val) : "memory") | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright (c) 2024 Travis Geiselbrecht | ||
* | ||
* Use of this source code is governed by a MIT-style | ||
* license that can be found in the LICENSE file or at | ||
* https://opensource.org/licenses/MIT | ||
*/ | ||
#pragma once | ||
|
||
#include <stdint.h> | ||
|
||
// Accessor routines for this architecture, used in include/reg.h | ||
// | ||
// Simple load/store instructions with a basic addressing mode of either | ||
// [reg], [reg, #imm] or [reg, reg], with no writeback are guaranteed to be | ||
// fully decoded into ESR_EL2 for hypervisor assist. | ||
|
||
#define ARCH_MMIO_READ_WRITE_OVERRIDE 1 | ||
|
||
#define _ARCH_MMIO_READ8(addr) ({ \ | ||
uint8_t val; \ | ||
__asm__ volatile("ldrb %w0, %1" : "=r"(val) : "m"(*(addr)) : "memory"); \ | ||
val; \ | ||
}) | ||
#define _ARCH_MMIO_READ16(addr) ({ \ | ||
uint16_t val; \ | ||
__asm__ volatile("ldrh %w0, %1" : "=r"(val) : "m"(*(addr)) : "memory"); \ | ||
val; \ | ||
}) | ||
#define _ARCH_MMIO_READ32(addr) ({ \ | ||
uint32_t val; \ | ||
__asm__ volatile("ldr %w0, %1" : "=r"(val) : "m"(*(addr)) : "memory"); \ | ||
val; \ | ||
}) | ||
#define _ARCH_MMIO_READ64(addr) ({ \ | ||
uint64_t val; \ | ||
__asm__ volatile("ldr %0, %1" : "=r"(val) : "m"(*(addr)) : "memory"); \ | ||
val; \ | ||
}) | ||
|
||
#define _ARCH_MMIO_WRITE8(addr, val) \ | ||
__asm__ volatile("strb %w1, %0" : "=m"(*(addr)) : "r"(val) : "memory") | ||
#define _ARCH_MMIO_WRITE16(addr, val) \ | ||
__asm__ volatile("strh %w1, %0" : "=m"(*(addr)): "r"(val) : "memory") | ||
#define _ARCH_MMIO_WRITE32(addr, val) \ | ||
__asm__ volatile("str %w1, %0" : "=m"(*(addr)) : "r"(val) : "memory") | ||
#define _ARCH_MMIO_WRITE64(addr, val) \ | ||
__asm__ volatile("str %1, %0" : "=m"(*(addr)) : "r"(val) : "memory") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* | ||
* Copyright (c) 2024 Travis Geiselbrecht | ||
* | ||
* Use of this source code is governed by a MIT-style | ||
* license that can be found in the LICENSE file or at | ||
* https://opensource.org/licenses/MIT | ||
*/ | ||
#pragma once | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* | ||
* Copyright (c) 2024 Travis Geiselbrecht | ||
* | ||
* Use of this source code is governed by a MIT-style | ||
* license that can be found in the LICENSE file or at | ||
* https://opensource.org/licenses/MIT | ||
*/ | ||
#pragma once | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* | ||
* Copyright (c) 2024 Travis Geiselbrecht | ||
* | ||
* Use of this source code is governed by a MIT-style | ||
* license that can be found in the LICENSE file or at | ||
* https://opensource.org/licenses/MIT | ||
*/ | ||
#pragma once | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* | ||
* Copyright (c) 2024 Travis Geiselbrecht | ||
* | ||
* Use of this source code is governed by a MIT-style | ||
* license that can be found in the LICENSE file or at | ||
* https://opensource.org/licenses/MIT | ||
*/ | ||
#pragma once | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* | ||
* Copyright (c) 2024 Travis Geiselbrecht | ||
* | ||
* Use of this source code is governed by a MIT-style | ||
* license that can be found in the LICENSE file or at | ||
* https://opensource.org/licenses/MIT | ||
*/ | ||
#pragma once | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright (c) 2024 Travis Geiselbrecht | ||
* | ||
* Use of this source code is governed by a MIT-style | ||
* license that can be found in the LICENSE file or at | ||
* https://opensource.org/licenses/MIT | ||
*/ | ||
#pragma once | ||
|
||
#include <stdint.h> | ||
|
||
// Accessor routines for this architecture, used in include/reg.h | ||
// | ||
// Simple mov instructions that directly access the mmio address using the | ||
// precise width needed in one instruction. | ||
// | ||
// NOTE: this may not be strict enough, and it may be necessary to load the | ||
// address into a register and use a simple (reg) dereference, instead of a more | ||
// complicated #imm(reg, reg, #imm) addressing mode. | ||
|
||
#define ARCH_MMIO_READ_WRITE_OVERRIDE 1 | ||
|
||
#define _ARCH_MMIO_READ8(addr) ({ \ | ||
uint8_t val; \ | ||
__asm__ volatile("movb %1, %0" : "=q"(val) : "m"(*(addr)) : "memory"); \ | ||
val; \ | ||
}) | ||
#define _ARCH_MMIO_READ16(addr) ({ \ | ||
uint16_t val; \ | ||
__asm__ volatile("movw %1, %0" : "=r"(val) : "m"(*(addr)) : "memory"); \ | ||
val; \ | ||
}) | ||
#define _ARCH_MMIO_READ32(addr) ({ \ | ||
uint32_t val; \ | ||
__asm__ volatile("movl %1, %0" : "=r"(val) : "m"(*(addr)) : "memory"); \ | ||
val; \ | ||
}) | ||
#if _LP64 | ||
#define _ARCH_MMIO_READ64(addr) ({ \ | ||
uint64_t val; \ | ||
__asm__ volatile("movq %1, %0" : "=r"(val) : "m"(*(addr)) : "memory"); \ | ||
val; \ | ||
}) | ||
#endif | ||
|
||
#define _ARCH_MMIO_WRITE8(addr, val) \ | ||
__asm__ volatile("movb %1, %0" : "=m"(*(addr)) : "iq"(val) : "memory") | ||
#define _ARCH_MMIO_WRITE16(addr, val) \ | ||
__asm__ volatile("movw %1, %0" : "=m"(*(addr)) : "ir"(val) : "memory") | ||
#define _ARCH_MMIO_WRITE32(addr, val) \ | ||
__asm__ volatile("movl %1, %0" : "=m"(*(addr)) : "ir"(val) : "memory") | ||
#if _LP64 | ||
#define _ARCH_MMIO_WRITE64(addr, val) \ | ||
__asm__ volatile("movq %1, %0" : "=m"(*(addr)) : "er"(val) : "memory") | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters