forked from u-root/u-root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
purg_load.go
103 lines (89 loc) · 3.28 KB
/
purg_load.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
// Copyright 2022 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:generate go run genpurg.go purgatories.go
// Package purgatory provides several purgatories for use with kexec_load
// system call.
//
// The kernel's contract on x86_64 is that kexec will jump to the entry point
// in 64bit mode with identity-mapped page tables and unspecified garbage in
// registers.
//
// To drop into the right addressing mode and pass arguments to the next
// kernel, some additional code is placed before the execution of the new
// kernel called a purgatory. So it will go: old kernel jumps to purgatory
// jumps to new kernel.
//
// Our purgatory will only have these three responsibilities:
// - drop to the intended addressing mode (32bit or 64bit)
// - set the right rsi parameters (for Linux kernels)
// - jump to the entry point.
//
// Several purgatories written in Assembly are part of this package: one that
// sets up the Linux args and remains in 64bit mode, and one that sets up the
// Linux args and drops to 32bit mode.
package purgatory
import (
"bytes"
"debug/elf"
"encoding/binary"
"fmt"
"log"
"github.com/u-root/u-root/pkg/align"
"github.com/u-root/u-root/pkg/boot/kexec"
)
const defaultPurgatory = "default"
var (
// Debug is called to print out verbose debug info.
//
// Set this to appropriate output stream for display
// of useful debug info.
Debug = log.Printf // func(string, ...interface{}) {}
curPurgatory = Purgatories[defaultPurgatory]
)
// Select picks a purgatory, returning an error if none is found.
func Select(name string) error {
p, ok := Purgatories[name]
if !ok {
var s []string
for i := range Purgatories {
s = append(s, i)
}
return fmt.Errorf("%s: no such purgatory, try one of %v", name, s)
}
curPurgatory = p
return nil
}
// Load loads the selected purgatory into kmem, instructing it to jump to entry
// with RSI set to rsi.
func Load(kmem *kexec.Memory, entry, rsi uintptr) (uintptr, error) {
elfFile, err := elf.NewFile(bytes.NewReader(curPurgatory.Code))
if err != nil {
return 0, fmt.Errorf("parse purgatory ELF file from ELF buffer: %v", err)
}
log.Printf("Elf file: %#v, %d Progs", elfFile, len(elfFile.Progs))
if len(elfFile.Progs) != 1 {
return 0, fmt.Errorf("parse purgatory ELF file: can only handle one Prog, not %d", len(elfFile.Progs))
}
p := elfFile.Progs[0]
// the package really wants things page-sized, and rather than
// deal with all the bugs that arise from that, just keep it happy.
p.Memsz = uint64(align.UpPage(uint(p.Memsz)))
b := make([]byte, p.Memsz)
if _, err := p.ReadAt(b[:p.Filesz], 0); err != nil {
return 0, err
}
elfEntry := uintptr(elfFile.Entry)
//Debug("Start is %#x, param is %#x", start, param)
binary.LittleEndian.PutUint64(b[8:], uint64(entry))
binary.LittleEndian.PutUint64(b[16:], uint64(rsi))
// TODO: Shouldn't the purgatories be relocatable?
min := uintptr(p.Vaddr)
max := uintptr(p.Vaddr + uint64(len(b)))
phyRange, err := kmem.ReservePhys(uint(len(b)), kexec.RangeFromInterval(min, max))
if err != nil {
return 0, fmt.Errorf("purgatory: reserve phys ram of size %d between range(%d, %d): %v", len(b), min, max, err)
}
kmem.Segments.Insert(kexec.NewSegment(b, phyRange))
return elfEntry, nil
}