-
Notifications
You must be signed in to change notification settings - Fork 2
/
PhysicalMemory.jl
265 lines (232 loc) · 7.84 KB
/
PhysicalMemory.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
include("hw/IODev.jl")
const PAGE_BITS = 12
const PAGE_SIZE = (1 << PAGE_BITS)
const PAGE_MASK = UInt64((PAGE_SIZE - 1))
type PhysicalMemory
size:: UInt64
array:: Array{UInt8}
baseptr:: Ptr{UInt8}
#= Memory-mapped I/O : devices that can be
accessed through the processor’s physical-memory address space.
See Vol.1 16.3.1 =#
iomap:: Array{Bool}
iomap_dev:: Array{IODev}
iomap_r64:: Array{Function}
iomap_r32:: Array{Function}
iomap_r16:: Array{Function}
iomap_r8:: Array{Function}
iomap_w64:: Array{Function}
iomap_w32:: Array{Function}
iomap_w16:: Array{Function}
iomap_w8:: Array{Function}
function PhysicalMemory(size:: UInt64)
# Extra space for buggy manipulation on the last word
# (This should never happen since MMU will performance
# boundary check before accessing physical memory.)
m = new(size,
Array(UInt8, size + PAGE_SIZE),
0,
Array(Bool, 1 << 20),
Array(IODev, 1 << 20),
Array(Function, 1 << 20),
Array(Function, 1 << 20),
Array(Function, 1 << 20),
Array(Function, 1 << 20),
Array(Function, 1 << 20),
Array(Function, 1 << 20),
Array(Function, 1 << 20),
Array(Function, 1 << 20),
)
fill!(m.array, 0)
fill!(m.iomap, false)
m.baseptr = convert(Ptr{UInt8}, pointer(m.array))
println(m.baseptr)
return m
end
end
function register_phys_io_map(
memory:: PhysicalMemory, start:: UInt64, size:: UInt64,
device:: IODev,
f_r64:: Function, f_r32:: Function, f_r16:: Function, f_r8:: Function,
f_w64:: Function, f_w32:: Function, f_w16:: Function, f_w8:: Function)
if (((start | size) & PAGE_MASK) != 0)
error("start and size must be page-aligned")
end
for i = (start >>> PAGE_BITS) + 1 : ((start + size) >>> PAGE_BITS)
memory.iomap[i] = true
memory.iomap_dev[i] = device
memory.iomap_r64[i] = f_r64
memory.iomap_r32[i] = f_r32
memory.iomap_r16[i] = f_r16
memory.iomap_r8[i] = f_r8
memory.iomap_w64[i] = f_w64
memory.iomap_w32[i] = f_w32
memory.iomap_w16[i] = f_w16
memory.iomap_w8[i] = f_w8
end
device.mmiobase = start
end
#=
The following R/W functions should be only called by MMU functions.
E.g. ru64, ru43, wu64, etc. Therefore, it is assumed the cross-page
case has been taken care by MMU functions. Never call these functions
unless the above assumption is satisfied.
If address exceed physical size, we let I/O system to take over
since it eventually throws exception.
=#
# Read access functions
# 64-bit
@noinline function io_r64(memory:: PhysicalMemory, addr:: UInt64)
seq = ((addr % UInt32) >>> PAGE_BITS)
return memory.iomap_r64[@ZB(seq)](memory.iomap_dev[@ZB(seq)], addr)
end
@noinline function phys_read_u64(memory:: PhysicalMemory, addr:: UInt64)
seq = ((addr % UInt32) >>> PAGE_BITS)
@inbounds isIO = memory.iomap[@ZB(seq)] && addr <= 0xffffffff
if !isIO && addr < memory.size
return unsafe_load(convert(Ptr{UInt64}, memory.baseptr + addr), 1)
end
return UInt64(io_r64(memory, addr))
end
# 32-bit
@noinline function io_r32(memory:: PhysicalMemory, addr:: UInt64)
seq = ((addr % UInt32) >>> PAGE_BITS)
return memory.iomap_r32[@ZB(seq)](memory.iomap_dev[@ZB(seq)], addr)
end
function phys_read_u32(memory:: PhysicalMemory, addr:: UInt64)
seq = ((addr % UInt32) >>> PAGE_BITS)
@inbounds isIO = memory.iomap[@ZB(seq)] && addr <= 0xffffffff
if !isIO && addr < memory.size
return unsafe_load(convert(Ptr{UInt32}, memory.baseptr + addr), 1)
end
return UInt32(io_r32(memory, addr))
end
# 16-bit
@noinline function io_r16(memory:: PhysicalMemory, addr:: UInt64)
seq = ((addr % UInt32) >>> PAGE_BITS)
return memory.iomap_r16[@ZB(seq)](memory.iomap_dev[@ZB(seq)], addr)
end
function phys_read_u16(memory:: PhysicalMemory, addr:: UInt64)
seq = ((addr % UInt32) >>> PAGE_BITS)
@inbounds isIO = memory.iomap[@ZB(seq)] && addr <= 0xffffffff
if !isIO && addr < memory.size
return unsafe_load(convert(Ptr{UInt16}, memory.baseptr + addr), 1);
end
return UInt16(io_r16(memory, addr))
end
# 8-bit
@noinline function io_r8(memory:: PhysicalMemory, addr:: UInt64)
seq = ((addr % UInt32) >>> PAGE_BITS)
return UInt8(memory.iomap_r8[@ZB(seq)](memory.iomap_dev[@ZB(seq)], addr))
end
function phys_read_u8(memory:: PhysicalMemory, addr:: UInt64)
seq = ((addr % UInt32) >>> PAGE_BITS)
@inbounds isIO = memory.iomap[@ZB(seq)] && addr <= 0xffffffff
if !isIO && addr < memory.size
return unsafe_load(convert(Ptr{UInt8}, memory.baseptr + addr), 1);
end
return io_r8(memory, addr)
end
function phys_read_u8_debug(memory:: PhysicalMemory, addr:: UInt64)
# Bypass I/O mapping
return memory.array[@ZB(addr)];
end
# Copy physical RAM into buffer
function phys_ram_to_buffer(addr:: UInt64, len:: UInt64, buf:: Ptr{UInt8})
while len > 0
seq = ((addr % UInt32) >>> PAGE_BITS)
@inbounds isIO = g_phys_mem.iomap[@ZB(seq)] && addr <= 0xffffffff
if isIO
unsafe_store!(buf, io_r8(g_phys_mem, addr), 1)
len -= 1
buf += 1
addr += 1
else
l = min(((addr + PAGE_SIZE) & ~PAGE_MASK) - addr, len)
ccall(("memcpy", "libc"), Ptr{Void}, (Ptr{Void}, Ptr{Void}, Csize_t,), buf, convert(Ptr{Void}, g_phys_mem.baseptr + addr), l)
len -= l
buf += l
addr += l
end
end
end
# Write access functions
# 64-bit
@noinline function io_w64(memory:: PhysicalMemory, addr:: UInt64, data:: UInt64)
seq = ((addr % UInt32) >>> PAGE_BITS)
memory.iomap_w64[@ZB(seq)](memory.iomap_dev[@ZB(seq)], addr, data)
end
function phys_write_u64(memory:: PhysicalMemory, addr:: UInt64, data:: UInt64)
seq = ((addr % UInt32) >>> PAGE_BITS)
@inbounds isIO = memory.iomap[@ZB(seq)] && addr <= 0xffffffff
if !isIO && addr < memory.size
unsafe_store!(convert(Ptr{UInt64}, memory.baseptr + addr), data, 1);
return
end
io_w64(memory, addr, data)
end
# 32-bit
@noinline function io_w32(memory:: PhysicalMemory, addr:: UInt64, data:: UInt32)
seq = ((addr % UInt32) >>> PAGE_BITS)
memory.iomap_w32[@ZB(seq)](memory.iomap_dev[@ZB(seq)], addr, data)
end
function phys_write_u32(memory:: PhysicalMemory, addr:: UInt64, data:: UInt32)
seq = ((addr % UInt32) >>> PAGE_BITS)
@inbounds isIO = memory.iomap[@ZB(seq)] && addr <= 0xffffffff
if !isIO && addr < memory.size
unsafe_store!(convert(Ptr{UInt32}, memory.baseptr + addr), data, 1);
return
end
io_w32(memory, addr, data)
end
# 16-bit
@noinline function io_w16(memory:: PhysicalMemory, addr:: UInt64, data:: UInt16)
seq = ((addr % UInt32) >>> PAGE_BITS)
memory.iomap_w16[@ZB(seq)](memory.iomap_dev[@ZB(seq)], addr, data)
end
function phys_write_u16(memory:: PhysicalMemory, addr:: UInt64, data:: UInt16)
seq = ((addr % UInt32) >>> PAGE_BITS)
@inbounds isIO = memory.iomap[@ZB(seq)] && addr <= 0xffffffff
if !isIO && addr < memory.size
unsafe_store!(convert(Ptr{UInt16}, memory.baseptr + addr), data, 1);
return
end
io_w16(memory, addr, data)
end
# 8-bit
@noinline function io_w8(memory:: PhysicalMemory, addr:: UInt64, data:: UInt8)
seq = ((addr % UInt32) >>> PAGE_BITS)
memory.iomap_w8[@ZB(seq)](memory.iomap_dev[@ZB(seq)], addr, data)
end
function phys_write_u8(memory:: PhysicalMemory, addr:: UInt64, data:: UInt8)
seq = ((addr % UInt32) >>> PAGE_BITS)
@inbounds isIO = memory.iomap[@ZB(seq)] && addr <= 0xffffffff
if !isIO && addr < memory.size
unsafe_store!(convert(Ptr{UInt8}, memory.baseptr + addr), data, 1);
return
end
io_w8(memory, addr, data)
end
function phys_write_u8_debug(memory:: PhysicalMemory, addr:: UInt64, data:: UInt8)
# Bypass I/O mapping
memory.array[@ZB(addr)] = data;
end
# Copy buffer into physical RAM
function buffer_to_phys_ram(addr:: UInt64, len:: UInt64, buf:: Ptr{UInt8})
while len > 0
seq = ((addr % UInt32) >>> PAGE_BITS)
@inbounds isIO = g_phys_mem.iomap[@ZB(seq)] && addr <= 0xffffffff
if isIO
io_w8(g_phys_mem, addr, unsafe_load(buf, 1))
len -= 1
buf += 1
addr += 1
else
l = min(((addr + PAGE_SIZE) & ~PAGE_MASK) - addr, len)
ccall(("memcpy", "libc"), Ptr{Void}, (Ptr{Void}, Ptr{Void}, Csize_t,), convert(Ptr{Void}, g_phys_mem.baseptr + addr), buf, l)
len -= l
buf += l
addr += l
end
end
end