summaryrefslogtreecommitdiff
path: root/design/mock/intro.py
diff options
context:
space:
mode:
Diffstat (limited to 'design/mock/intro.py')
-rw-r--r--design/mock/intro.py213
1 files changed, 160 insertions, 53 deletions
diff --git a/design/mock/intro.py b/design/mock/intro.py
index b9c7d7d..098455c 100644
--- a/design/mock/intro.py
+++ b/design/mock/intro.py
@@ -26,14 +26,14 @@ CODE_BYTES = [(0x1001920, "48 89 c1"), (0x1001923, "48 89 f8"), (0x1001926, "48
def size(n):
- for unit, k in (("TiB", 1 << 40), ("GiB", 1 << 30), ("MiB", 1 << 20), ("KiB", 1 << 10)):
+ for unit, k in (("EiB", 1 << 60), ("PiB", 1 << 50), ("TiB", 1 << 40), ("GiB", 1 << 30), ("MiB", 1 << 20), ("KiB", 1 << 10)):
if n >= k:
v = n / k
return ("%d %s" if v == int(v) or v >= 10 else "%.1f %s") % (v, unit)
return "%d B" % n
-def segments(g, x, y, w, hot=()):
+def segment_rows(g, x, y, w, hot=()):
"""`segments`: the process's address space as its mappings, low addresses on top, a row each: a bar in the
region's ink, the first address, what it is, its size; a hole between them one row, how big it is. Rows, not
a scale: the stack and the code are 127 TiB apart. hot: the mappings framed as read (a word just inked)."""
@@ -60,6 +60,104 @@ def segments(g, x, y, w, hot=()):
return y0
+# the mappings at the stop, as `ztrace show` prints them (range, permissions, file offset, file); high on top
+MAPPINGS = [(0xffffffffff600000, 0xffffffffff601000, "--x", "[vsyscall]"),
+ (0x7ffffffde000, 0x7ffffffff000, "rw-", "[stack]"),
+ (0x7ffff7ffe000, 0x7ffff7fff000, "rw-", ""), (0x7ffff7ffd000, 0x7ffff7ffe000, "rw-", "ld.so"),
+ (0x7ffff7ffb000, 0x7ffff7ffd000, "r--", "ld.so"), (0x7ffff7fec000, 0x7ffff7ffb000, "r--", "ld.so"),
+ (0x7ffff7fbf000, 0x7ffff7fec000, "r-x", "ld.so"), (0x7ffff7fbe000, 0x7ffff7fbf000, "r--", "ld.so"),
+ (0x7ffff7fbc000, 0x7ffff7fbe000, "r-x", "[vdso]"), (0x7ffff7fb6000, 0x7ffff7fbc000, "r--", "[vvar]"),
+ (0x7ffff7f75000, 0x7ffff7fb6000, "r--", "ld.so.cache"), (0x7ffff7f70000, 0x7ffff7f75000, "rw-", ""),
+ (0x7ffff7e1b000, 0x7ffff7e23000, "rw-", ""), (0x7ffff7e19000, 0x7ffff7e1b000, "rw-", "libc.so.6"),
+ (0x7ffff7e15000, 0x7ffff7e19000, "r--", "libc.so.6"), (0x7ffff7d9f000, 0x7ffff7e15000, "r--", "libc.so.6"),
+ (0x7ffff7c24000, 0x7ffff7d9f000, "r-x", "libc.so.6"), (0x7ffff7c00000, 0x7ffff7c24000, "r--", "libc.so.6"),
+ (0x1004000, 0x1025000, "rw-", "[heap]"), (0x1003000, 0x1004000, "rw-", "list"),
+ (0x1002000, 0x1003000, "r--", "list"), (0x1001000, 0x1002000, "r-x", "list"), (0x1000000, 0x1001000, "r--", "list")]
+INK = {"r-x": CODE, "--x": CODE, "r--": VELLUM, "rw-": DATA}
+WHAT = {"r-x": "code", "--x": "code", "r--": "read-only", "rw-": "data"}
+
+
+def band_h(n):
+ """A band's height: 24 at least (a label's row), and 4 a doubling past 4 KiB: schematic, not to scale."""
+ import math
+ return 24 + 4 * max(math.log2(n / 4096), 0)
+
+
+def segments(g, x, y, hot=(), rsp=None, rip=None, label_x=None):
+ """`segments`: the address space as one tall rectangle, high addresses on top, as the textbook draws it.
+ Bands in their mapping's ink (55% toward the page), a file's mappings touching, its parts split by a page
+ line; a hole a fixed 40 with a break and its size. Labels beside with leaders: the left the band's first
+ address, the right its permissions, what it is, its size; a file's parts bracketed with the file's name.
+ Live: rsp's and rip's pins at their place; a band framed when a word is inked into it; growth arrows."""
+ W = 140
+ lx = label_x or x + W + 40
+ yy, last_lo, groups, pos = y, None, [], {}
+ g.t(x - 12, yy - 6, "ffffffffffffffff", 18, FAINT, "sans", "end")
+ for lo, hi, perm, name in MAPPINGS:
+ if last_lo is not None and hi < last_lo: # a hole: compressed to 40, a break and its size
+ gap = last_lo - hi
+ g.rect(x, yy, W, 40, PAGE)
+ g.frame(x, yy - 1, W, 42, RULE, 2)
+ for dy in (14, 24):
+ g.path(" ".join(["M%d,%d" % (x - 6, yy + dy)] + ["l%d,%d" % (10, -6 if k % 2 == 0 else 6)
+ for k in range(16)]), RULE, 2)
+ for kk, (unit, k) in enumerate((("TiB", 1 << 40), ("GiB", 1 << 30), ("MiB", 1 << 20), ("KiB", 1 << 10))):
+ if gap >= k:
+ g.t(lx, yy + 26, "nothing mapped · %s" % size(gap), 18, FAINT, "sans")
+ break
+ yy += 40
+ h = band_h(hi - lo)
+ ink = INK[perm] if name not in ("[stack]", "[heap]") else (STACK if name == "[stack]" else HEAP)
+ g.rect(x, yy, W, h, mix(ink, PAGE, 0.55))
+ g.rect(x, yy, W, 2, PAGE) # a mapping's edge
+ mid = yy + h / 2
+ g.path(f"M{x + W + 4},{mid} H{lx - 8}", RULE, 2) # the leader
+ g.t(lx, mid + 7, perm, S, ink if perm != "r--" else VELLUM, MONO)
+ g.t(lx + 4 * A, mid + 7, (name if name.startswith("[") else WHAT[perm]) + ("" if name else " (anonymous)"),
+ 18, DIM, "sans")
+ g.t(lx + 4 * A + 170, mid + 7, size(hi - lo), S, FAINT, MONO)
+ C.addr(g, x - 12 - len("%x" % lo) * A, yy + h - ROW, lo, ink)
+ if lo in hot:
+ g.frame(x - 4, yy - 2, W + 8, h + 4, READ, 3)
+ pos[name, lo] = (yy, h)
+ if name and not name.startswith("[") and (not groups or groups[-1][0] != name):
+ groups.append([name, yy, yy + h])
+ elif name and groups and groups[-1][0] == name:
+ groups[-1][2] = yy + h
+ yy += h
+ last_lo = lo
+ for name, a, z in groups: # a file's mappings: one thing, with parts
+ C.bracket(g, lx + 4 * A + 280, a + 3, z - 3, DIM, 2, 6, side=-1)
+ g.t(lx + 4 * A + 292, (a + z) / 2 + 7, name, S, VELLUM, MONO)
+ g.t(x - 12, yy + 18, "0", 18, FAINT, "sans", "end")
+ # growth: the stack down from its top, the heap up from its base
+ sy, sh = pos["[stack]", 0x7ffffffde000]
+ gen.arrow(g, [(x + W - 24, sy + 8), (x + W - 24, sy + sh - 6)], BRIGHT, 2, hl=8, hw=8)
+ hy, hh = pos["[heap]", 0x1004000]
+ gen.arrow(g, [(x + W - 24, hy + hh - 6), (x + W - 24, hy + 8)], BRIGHT, 2, hl=8, hw=8)
+ if rsp: # a register's pin at its place in its band
+ f = (0x7ffffffff000 - rsp) / (0x7ffffffff000 - 0x7ffffffde000)
+ py = sy + f * sh
+ g.t(x + 8, py + 7, "rsp", S, VELLUM, BOLD)
+ g.path(f"M{x + 48},{py} l9,-6 v12 Z", fill=STACK)
+ if rip:
+ cy, ch = pos["list", 0x1001000]
+ g.t(x + 8, cy + ch / 2 + 7, "rip", S, VELLUM, BOLD)
+ g.path(f"M{x + 48},{cy + ch / 2} l9,-6 v12 Z", fill=CODE)
+ g.t(x, y - 34, "the address space", 20, VELLUM, "sansm")
+ g.t(x + 200, y - 34, "high addresses on top · not to scale: holes 40, bands by the log of their size", 18,
+ FAINT, "sans")
+ return yy
+
+
+def frame_space(name):
+ """Mock 34: the rectangle alone, every label reading."""
+ gen.W, gen.H = 1920, 1080
+ g = Svg()
+ segments(g, 560, 110, rsp=RSP, rip=0x1001926, label_x=760)
+ g.save(name)
+
+
def camera(g, k, cx, cy):
"""The camera at scale k on the point (cx, cy) of the frame: all drawing after this is through it."""
g.o.append(f'<g transform="translate({960 - cx * k},{468 - cy * k}) scale({k})">')
@@ -108,7 +206,11 @@ def frame_words(name):
k = 1 # 7fffffffece8: the return address, main+80
g.frame(12 * A + 12 - 4, ys[k] - 1, 8 * 30 + 4, ROW + 2, READ, 2)
g.frame(12 * A + 12 + 8 * 30 + 24 - 6, ys[k] - 1, 7 * A + 12, ROW + 2, READ, 2)
- segments(g, 12 * A + 12 + 8 * 30 + 24 + 13 * A + 36, -24, 560, hot={0x1001000})
+ g.o.append("</g>")
+ g.o.append('<g transform="translate(0,0)">')
+ g.o.append('<g transform="translate(1000,110) scale(0.8)">') # the stage holds it whole above the caption
+ segments(g, 200, 0, hot={0x1001000}, rsp=RSP, label_x=380)
+ g.o.append("</g>")
g.o.append("</g>")
gen.caption(g, [[("Eight bytes make a word, stored low byte first: read right to left,",)],
[("this one is ",), ("10018b0", "mono"), (", an address in the code, so it takes code's ink.",)]])
@@ -136,56 +238,60 @@ BYTES = {0x1001910: "48 85 ff", 0x1001913: "74 20", 0x1001915: "55", 0x1001916:
def aligned(g, y, first, n, cur, cur_line, bytes_=False, title="reverse"):
- """`aligned`: the source and the disassembly side by side, a table in address order: each source line level
- with the first row of its run of instructions, a line that comes again (a second run) repeated faint at that
- run, a line with no code of its own not drawn (its number missing says so), a row of no line (padding) with
- nothing beside it. The pc's row and its line are one band across both columns: the alignment is the thread."""
+ """`aligned`, one panel: the listing interleaved as objdump -S prints it. Each run of a line's instructions
+ is a group: a head row (the line on raised paper, its number in the place column) and under it its rows,
+ their addresses in the same column, indented under the line's text. A 2-unit rule over every head: where a
+ line starts and ends. A line's second run: its head again, faint; rows of no line: a faint head saying so."""
rows = [r for r in D.disasm_rows() if r[0] >= first][:n]
- h = ph(n)
- C.tag(g, 64, y, 880, h, "list.c", note="each line beside the first of its instructions")
- C.tag(g, 976, y, 880, h, "disassembly", note=title)
- y0 = y + TAG + PAD
- seen, last_line = set(), None
- lead = C.shared([r[0] for r in rows])
- i0 = 0 # each run's extent: a quiet bracket by the gutter, line to its last row
- for i in range(1, n + 1):
- if i == n or rows[i][2] != rows[i0][2]:
- if rows[i0][2] is not None:
- C.bracket(g, 932, y + TAG + PAD + i0 * ROW + 3, y + TAG + PAD + i * ROW - 3, RULE, 2, 5, side=-1)
- i0 = i
- for i, (a, func, line, mn, ops) in enumerate(rows):
- yy = y0 + i * ROW
- on = a == cur
- if on: # one band through the gutter: the line and its instruction are one
- g.rect(64 + 2, yy, 880 - 4, ROW, C.WASH)
- g.rect(944, yy, 32, ROW, C.WASH)
- g.rect(976 + 2, yy, 880 - 4, ROW, C.WASH)
- g.rect(976 + 2, yy, 24, ROW, GOLD)
- g.path(f"M{976 + 20},{yy + 12} l-10,-6 v12 Z", fill=gen.INK)
- if line is not None and line != last_line:
- again = line in seen
- if line == cur_line and on:
- g.rect(64 + 2, yy, 50, ROW, GOLD)
- g.t(64 + 46, b(yy), str(line), S, gen.INK, BOLD, "end")
+ groups, cur_g = [], None
+ for r in rows:
+ key = (r[2], r[1]) if r[2] is not None else (None, r[1])
+ if cur_g is None or cur_g[0] != key:
+ cur_g = [key, []]
+ groups.append(cur_g)
+ cur_g[1].append(r)
+ total = len(rows) + len(groups)
+ C.tag(g, 64, y, 1792, ph(total), "list.c · %s" % title, focus=True,
+ note="each line, then the instructions it compiled to · lines and addresses in one column")
+ yy, seen = y + TAG + PAD, set()
+ PLACE, TEXT = 100 + 7 * A, 220 # the place column ends at 196; the text 24 after it
+ for (line, func), grp in groups:
+ g.rect(64 + 2, yy, 1792 - 4, 2, RULE) # where a line's group starts (and the last one ends)
+ again = line in seen
+ on_line = line == cur_line and any(r[0] == cur for r in grp)
+ g.rect(64 + 2, yy + 2, 1792 - 4, ROW - 2, RAISED if not again else PAGE)
+ if line is None:
+ g.t(TEXT, b(yy), "%s · no line" % func, 18, FAINT, "sans")
+ else:
+ if on_line:
+ g.rect(PLACE - 50 + 12, yy + 2, 50, ROW - 2, GOLD)
+ g.t(PLACE + 12, b(yy), str(line), S, gen.INK if on_line else (FAINT if again else DIM), BOLD if on_line else MONO,
+ "end")
+ text = D.SRC[line - 1].strip()
+ if again:
+ g.t(TEXT, b(yy), text + " again: its second run", S, FAINT, MONO)
else:
- g.t(64 + 46, b(yy), str(line), S, FAINT, MONO, "end")
- text = D.SRC[line - 1]
- C.c_line(g, 64 + 64, yy, text, 66) if not again else g.t(64 + 64, b(yy), text.strip(), S, FAINT, MONO)
+ C.c_line(g, TEXT, yy, text, 80)
seen.add(line)
- elif line is None and (i == 0 or rows[i - 1][1] != func):
- g.t(64 + 64, b(yy), "%s · no line" % func, 18, FAINT, "sans")
- last_line = line
- C.addr(g, 976 + 40, yy, a, CODE, lead=lead)
- x = 976 + 40 + 8 * A + 12
- if bytes_:
- g.t(x, b(yy), BYTES.get(a, ""), S, BRIGHT if on else DIM, MONO)
- x += 15 * A
- g.t(x, b(yy), mn, S, FAINT if mn == "nop" else KW, MONO if mn == "nop" else BOLD)
- C.operands(g, x + 7 * A, yy, mn, ops, VELLUM)
- if on:
- C.chain(g, 976 + 600, yy, [(0x1004098, HEAP), (0x1004070, HEAP)], ("int", 9), {0: "read"} if False else None,
- room=976 + 868)
- return y0
+ yy += ROW
+ for a, f_, ln, mn, ops in grp:
+ on = a == cur
+ if on:
+ g.rect(64 + 2, yy, 1792 - 4, ROW, C.WASH)
+ g.rect(64 + 2, yy, 24, ROW, GOLD)
+ g.path(f"M{64 + 20},{yy + 12} l-10,-6 v12 Z", fill=gen.INK)
+ C.addr(g, PLACE + 12 - len("%x" % a) * A, yy, a, CODE, lead=4)
+ x = TEXT + 2 * A # indented under the line it came from
+ if bytes_:
+ g.t(x, b(yy), BYTES.get(a, ""), S, BRIGHT if on else DIM, MONO)
+ x += 15 * A
+ g.t(x, b(yy), mn, S, FAINT if mn == "nop" else KW, MONO if mn == "nop" else BOLD)
+ C.operands(g, x + 7 * A, yy, mn, ops, VELLUM)
+ if on:
+ C.chain(g, 64 + 900, yy, [(0x1004098, HEAP), (0x1004070, HEAP)], ("int", 9), None, room=64 + 1780)
+ yy += ROW
+ g.rect(64 + 2, yy, 1792 - 4, 2, RULE)
+ return yy
def frame_code(name):
@@ -203,9 +309,9 @@ def frame_aligned(name):
"""Mock 33: the `aligned` split, reverse and list_done, the pc at the load."""
gen.W, gen.H = 1920, 1080
g = Svg()
- aligned(g, 48, 0x1001910, 30, 0x1001926, 21)
- gen.caption(g, [[("Line 20, ",), ("while (head)", "mono"), (", has two runs: its test before the body",)],
- [("and again after it. Lines 23 to 25 have no code of their own.",)]])
+ aligned(g, 48, 0x1001910, 16, 0x1001926, 21)
+ gen.caption(g, [[("Line 20, ",), ("while (head)", "mono"), (", comes twice: its test before the body and",)],
+ [("again after it. Lines 23 to 25 have no code of their own: no group.",)]])
g.save(name)
@@ -215,3 +321,4 @@ if __name__ == "__main__":
frame_stack("31-intro-stack")
frame_code("32-intro-code")
frame_aligned("33-aligned")
+ frame_space("34-segments")