-
Notifications
You must be signed in to change notification settings - Fork 12
/
mem.go
152 lines (116 loc) · 3.34 KB
/
mem.go
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
// Copyright (c) WithSecure Corporation
// https://foundry.withsecure.com
//
// Use of this source code is governed by the license
// that can be found in the LICENSE file.
package cmd
import (
"encoding/binary"
"encoding/hex"
"fmt"
"log"
"math"
"math/rand"
"regexp"
"runtime"
"runtime/debug"
"strconv"
"golang.org/x/term"
"github.com/usbarmory/tamago/dma"
)
const (
runs = 9
chunksMax = 50
fillSize = 160 * 1024 * 1024
)
func init() {
Add(Cmd{
Name: "peek",
Args: 2,
Pattern: regexp.MustCompile(`^peek ([[:xdigit:]]+) (\d+)$`),
Syntax: "<hex offset> <size>",
Help: "memory display (use with caution)",
Fn: memReadCmd,
})
Add(Cmd{
Name: "poke",
Args: 2,
Pattern: regexp.MustCompile(`^poke ([[:xdigit:]]+) ([[:xdigit:]]+)$`),
Syntax: "<hex offset> <hex value>",
Help: "memory write (use with caution)",
Fn: memWriteCmd,
})
}
func memCopy(start uint, size int, w []byte) (b []byte) {
mem, err := dma.NewRegion(start, size, true)
if err != nil {
panic("could not allocate memory copy DMA")
}
start, buf := mem.Reserve(size, 0)
defer mem.Release(start)
if len(w) > 0 {
copy(buf, w)
} else {
b = make([]byte, size)
copy(b, buf)
}
return
}
func memReadCmd(_ *Interface, _ *term.Terminal, arg []string) (res string, err error) {
addr, err := strconv.ParseUint(arg[0], 16, 32)
if err != nil {
return "", fmt.Errorf("invalid address, %v", err)
}
size, err := strconv.ParseUint(arg[1], 10, 32)
if err != nil {
return "", fmt.Errorf("invalid size, %v", err)
}
if (addr%4) != 0 || (size%4) != 0 {
return "", fmt.Errorf("only 32-bit aligned accesses are supported")
}
if size > maxBufferSize {
return "", fmt.Errorf("size argument must be <= %d", maxBufferSize)
}
return hex.Dump(mem(uint(addr), int(size), nil)), nil
}
func memWriteCmd(_ *Interface, _ *term.Terminal, arg []string) (res string, err error) {
addr, err := strconv.ParseUint(arg[0], 16, 32)
if err != nil {
return "", fmt.Errorf("invalid address, %v", err)
}
val, err := strconv.ParseUint(arg[1], 16, 32)
if err != nil {
return "", fmt.Errorf("invalid data, %v", err)
}
buf := make([]byte, 4)
binary.BigEndian.PutUint32(buf, uint32(val))
mem(uint(addr), 4, buf)
return
}
func memTest() {
var memstats runtime.MemStats
chunks := rand.Intn(chunksMax) + 1
chunkSize := fillSize / chunks
// This test gets close to the end of available RAM, for this reason we
// take advantage of Go soft memory limit to avoid running out of
// memory.
//
// This is not something unique to `GOOS=tamago` but more evident as,
// when running on bare metal, there is no swap or OS virtual memory.
ramStart, ramEnd := runtime.MemRegion()
memoryLimit := float64(ramEnd-ramStart) * 0.90
debug.SetMemoryLimit(int64(math.Round(memoryLimit)))
msg("memory allocation (%d runs)", runs)
for run := 1; run <= runs; run++ {
log.Printf("allocating %d * %d MiB chunks (%d/%d)", chunks, chunkSize/(1024*1024), run, runs)
buf := make([][]byte, chunks)
for i := 0; i <= chunks-1; i++ {
buf[i] = make([]byte, chunkSize)
}
}
runtime.GC()
runtime.ReadMemStats(&memstats)
totalAllocated := uint64(runs) * uint64(chunks) * uint64(chunkSize)
log.Printf("%d MiB allocated (Mallocs: %d Frees: %d HeapSys: %d NumGC:%d)",
totalAllocated/(1024*1024), memstats.Mallocs, memstats.Frees, memstats.HeapSys, memstats.NumGC)
}