Skip to content

Commit 354d175

Browse files
committed
modmachine: Implement physical memory access using /dev/mem (Linux, etc).
This requires root access. And on recent Linux kernels, with CONFIG_STRICT_DEVMEM option enabled, only address ranges listed in /proc/iomem can be accessed. The above compiled-time option can be however overriden with boot-time option "iomem=relaxed". This also removed separate read/write paths - there unlikely would be a case when they're different.
1 parent a0a3de6 commit 354d175

6 files changed

Lines changed: 53 additions & 26 deletions

File tree

extmod/modmachine.c

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,44 @@
2929

3030
#include "py/nlr.h"
3131
#include "py/obj.h"
32+
#if MICROPY_PLAT_DEV_MEM
33+
#include <errno.h>
34+
#include <fcntl.h>
35+
#include <sys/mman.h>
36+
#endif
3237

3338
#if MICROPY_PY_MACHINE
3439

35-
STATIC mp_uint_t get_read_addr(mp_obj_t addr_o, uint align) {
40+
#define PAGE_SIZE 4096
41+
#define PAGE_MASK (PAGE_SIZE - 1)
42+
43+
STATIC mp_uint_t get_addr(mp_obj_t addr_o, uint align) {
3644
mp_uint_t addr = mp_obj_int_get_truncated(addr_o);
3745
if ((addr & (align - 1)) != 0) {
3846
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "address %08x is not aligned to %d bytes", addr, align));
3947
}
40-
return addr;
41-
}
48+
#if MICROPY_PLAT_DEV_MEM
49+
{
50+
// Not thread-safe
51+
static int fd;
52+
static mp_uint_t last_base = (mp_uint_t)-1;
53+
static mp_uint_t map_page;
54+
if (!fd) {
55+
fd = open("/dev/mem", O_RDWR | O_SYNC);
56+
if (fd == -1) {
57+
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(errno)));
58+
}
59+
}
4260

43-
STATIC mp_uint_t get_write_addr(mp_obj_t addr_o, uint align) {
44-
mp_uint_t addr = mp_obj_int_get_truncated(addr_o);
45-
if ((addr & (align - 1)) != 0) {
46-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "address %08x is not aligned to %d bytes", addr, align));
61+
mp_uint_t cur_base = addr & ~PAGE_MASK;
62+
if (cur_base != last_base) {
63+
map_page = (mp_uint_t)mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, cur_base);
64+
last_base = cur_base;
65+
}
66+
addr = map_page + (addr & PAGE_MASK);
4767
}
68+
#endif
69+
4870
return addr;
4971
}
5072

@@ -67,7 +89,7 @@ STATIC mp_obj_t machine_mem_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t va
6789
return MP_OBJ_NULL; // op not supported
6890
} else if (value == MP_OBJ_SENTINEL) {
6991
// load
70-
mp_uint_t addr = get_read_addr(index, self->elem_size);
92+
mp_uint_t addr = get_addr(index, self->elem_size);
7193
uint32_t val;
7294
switch (self->elem_size) {
7395
case 1: val = (*(uint8_t*)addr); break;
@@ -77,7 +99,7 @@ STATIC mp_obj_t machine_mem_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t va
7799
return mp_obj_new_int(val);
78100
} else {
79101
// store
80-
mp_uint_t addr = get_write_addr(index, self->elem_size);
102+
mp_uint_t addr = get_addr(index, self->elem_size);
81103
uint32_t val = mp_obj_get_int(value);
82104
switch (self->elem_size) {
83105
case 1: (*(uint8_t*)addr) = val; break;

tests/extmod/machine1.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,8 @@
77
import sys
88
sys.exit()
99

10-
import uctypes
11-
1210
print(machine.mem8)
1311

14-
buf = bytearray(8)
15-
addr = uctypes.addressof(buf)
16-
17-
machine.mem8[addr] = 123
18-
print(machine.mem8[addr])
19-
20-
machine.mem16[addr] = 12345
21-
print(machine.mem16[addr])
22-
23-
machine.mem32[addr] = 123456789
24-
print(machine.mem32[addr])
25-
2612
try:
2713
machine.mem16[1]
2814
except ValueError:

tests/extmod/machine1.py.exp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
<8-bit memory>
2-
123
3-
12345
4-
123456789
52
ValueError
63
ValueError
74
TypeError

tests/extmod/machine_mem.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# This test requires root privilege, so is usually skipped
2+
# It also assumes x86 legacy hardware (with Video BIOS present).
3+
4+
try:
5+
import machine
6+
except ImportError:
7+
print("SKIP")
8+
import sys
9+
sys.exit()
10+
11+
try:
12+
print(hex(machine.mem16[0xc0000]))
13+
except OSError:
14+
print("SKIP")
15+
import sys
16+
sys.exit()

tests/extmod/machine_mem.py.exp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0xaa55

unix/mpconfigport.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,11 @@ void mp_unix_mark_exec(void);
179179

180180
#define MP_PLAT_PRINT_STRN(str, len) fwrite(str, 1, len, stdout)
181181

182+
#ifdef __linux__
183+
// Can access physical memory using /dev/mem
184+
#define MICROPY_PLAT_DEV_MEM (1)
185+
#endif
186+
182187
extern const struct _mp_obj_fun_builtin_t mp_builtin_input_obj;
183188
extern const struct _mp_obj_fun_builtin_t mp_builtin_open_obj;
184189
#define MICROPY_PORT_BUILTINS \

0 commit comments

Comments
 (0)