| 1 |
// Copyright 2016-2017 VMware, Inc. All Rights Reserved. |
| 2 |
// Copyright 2020 Google LLC |
| 3 |
// |
| 4 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
// you may not use this file except in compliance with the License. |
| 6 |
// You may obtain a copy of the License at |
| 7 |
// |
| 8 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
// |
| 10 |
// Unless required by applicable law or agreed to in writing, software |
| 11 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
// See the License for the specific language governing permissions and |
| 14 |
// limitations under the License. |
| 15 |
|
| 16 |
// +build ignore |
| 17 |
|
| 18 |
package main |
| 19 |
|
| 20 |
import ( |
| 21 |
"fmt" |
| 22 |
"strings" |
| 23 |
|
| 24 |
. "github.com/mmcloughlin/avo/build" |
| 25 |
"github.com/mmcloughlin/avo/reg" |
| 26 |
|
| 27 |
"github.com/vmware/vmw-guestinfo/asm" |
| 28 |
) |
| 29 |
|
| 30 |
// Doc of the golang plan9 assembler |
| 31 |
// http://p9.nyx.link/labs/sys/doc/asm.html |
| 32 |
// |
| 33 |
// A good primer of how to write golang with some plan9 flavored assembly |
| 34 |
// http://www.doxsey.net/blog/go-and-assembly |
| 35 |
// |
| 36 |
// Some x86 references |
| 37 |
// http://www.eecg.toronto.edu/~amza/www.mindsec.com/files/x86regs.html |
| 38 |
// https://cseweb.ucsd.edu/classes/sp10/cse141/pdf/02/S01_x86_64.key.pdf |
| 39 |
// https://en.wikibooks.org/wiki/X86_Assembly/Other_Instructions |
| 40 |
// |
| 41 |
// (This one is invaluable. Has a working example of how a standard function |
| 42 |
// call looks on the stack with the associated assembly.) |
| 43 |
// https://www.recurse.com/blog/7-understanding-c-by-learning-assembly |
| 44 |
// |
| 45 |
// Reference with raw form of the Opcode |
| 46 |
// http://x86.renejeschke.de/html/file_module_x86_id_139.html |
| 47 |
// |
| 48 |
// Massive x86_64 reference |
| 49 |
// http://ref.x86asm.net/coder64.html#xED |
| 50 |
|
| 51 |
type config struct { |
| 52 |
wordSize uint8 |
| 53 |
registers map[string]reg.Register |
| 54 |
} |
| 55 |
|
| 56 |
func setup(ctx *asm.AvoContext) (*config, error) { |
| 57 |
switch ctx.Arch { |
| 58 |
case "amd64": |
| 59 |
return &config{ |
| 60 |
wordSize: 64, |
| 61 |
registers: map[string]reg.Register{ |
| 62 |
"ax": reg.RAX, |
| 63 |
"bx": reg.RBX, |
| 64 |
"cx": reg.RCX, |
| 65 |
"dx": reg.RDX, |
| 66 |
"si": reg.RSI, |
| 67 |
"di": reg.RDI, |
| 68 |
"bp": reg.RBP, |
| 69 |
}, |
| 70 |
}, nil |
| 71 |
case "386": |
| 72 |
return &config{ |
| 73 |
wordSize: 32, |
| 74 |
registers: map[string]reg.Register{ |
| 75 |
"ax": reg.EAX, |
| 76 |
"bx": reg.EBX, |
| 77 |
"cx": reg.ECX, |
| 78 |
"dx": reg.EDX, |
| 79 |
"si": reg.ESI, |
| 80 |
"di": reg.EDI, |
| 81 |
"bp": reg.EBP, |
| 82 |
}, |
| 83 |
}, nil |
| 84 |
default: |
| 85 |
return nil, fmt.Errorf("unsupported architecture: %s", ctx.Arch) |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
func code(ctx *asm.AvoContext) error { |
| 90 |
cfg, err := setup(ctx) |
| 91 |
if err != nil { |
| 92 |
return err |
| 93 |
} |
| 94 |
|
| 95 |
// for all the following functions, we use those 7 registers as both |
| 96 |
// input and output. |
| 97 |
regs := []string{"ax", "bx", "cx", "dx", "si", "di", "bp"} |
| 98 |
|
| 99 |
funcs := []struct { |
| 100 |
name string |
| 101 |
body func() |
| 102 |
}{ |
| 103 |
{"bdoor_inout", func() { |
| 104 |
ctx.Comment("IN to DX from AX") |
| 105 |
ctx.INL() |
| 106 |
}}, |
| 107 |
{"bdoor_hbout", func() { |
| 108 |
ctx.CLD() |
| 109 |
ctx.REP() |
| 110 |
ctx.OUTSB() |
| 111 |
}}, |
| 112 |
{"bdoor_hbin", func() { |
| 113 |
ctx.CLD() |
| 114 |
ctx.REP() |
| 115 |
ctx.INSB() |
| 116 |
}}, |
| 117 |
{"bdoor_inout_test", func() {}}, |
| 118 |
} |
| 119 |
|
| 120 |
for _, f := range funcs { |
| 121 |
// generate function signature. By convention we use: |
| 122 |
// - $REG as input |
| 123 |
// - and ret$REG as output. |
| 124 |
proto := fmt.Sprintf( |
| 125 |
"func(%s uint%d) (%s uint%d)", |
| 126 |
strings.Join(regs, ","), |
| 127 |
cfg.wordSize, |
| 128 |
"ret"+strings.Join(regs, ",ret"), |
| 129 |
cfg.wordSize, |
| 130 |
) |
| 131 |
|
| 132 |
ctx.Function(f.name) |
| 133 |
ctx.Attributes(NOSPLIT | WRAPPER) |
| 134 |
ctx.SignatureExpr(proto) |
| 135 |
|
| 136 |
// load all registers |
| 137 |
for _, r := range regs { |
| 138 |
ctx.Load(ctx.Param(r), cfg.registers[r]) |
| 139 |
} |
| 140 |
|
| 141 |
// execute backdoor function |
| 142 |
f.body() |
| 143 |
|
| 144 |
// store all registers |
| 145 |
for _, r := range regs { |
| 146 |
ctx.Store(cfg.registers[r], ctx.Return("ret"+r)) |
| 147 |
} |
| 148 |
|
| 149 |
ctx.RET() |
| 150 |
} |
| 151 |
|
| 152 |
return nil |
| 153 |
} |
| 154 |
|
| 155 |
func main() { |
| 156 |
asm.GenAsm(code) |
| 157 |
} |