1/* $OpenBSD: codepatch.h,v 1.1 2026/06/23 11:45:54 kettenis Exp $ */
2/*
3 * Copyright (c) 2014-2015 Stefan Fritsch <sf@sfritsch.de>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#ifndef _MACHINE_CODEPATCH_H_
19#define _MACHINE_CODEPATCH_H_
20
21#include <machine/param.h>
22
23#ifndef _LOCORE
24
25/* code in this section will be unmapped after boot */
26#define __cptext __attribute__((section(".cptext")))
27
28__cptext void *codepatch_maprw(vaddr_t *nva, vaddr_t dest);
29__cptext void codepatch_unmaprw(vaddr_t nva);
30__cptext void codepatch_fill_nop(void *caddr, uint16_t len);
31__cptext void codepatch_nop(uint16_t tag);
32void codepatch_disable(void);
33
34#endif /* !_LOCORE */
35
36/*
37 * Mark the start of some code snippet to be patched.
38 */
39#define CODEPATCH_START 998:
40/*
41 * Mark the end of some code to be patched, and assign the given tag.
42 */
43#define CODEPATCH_END2(startnum,tag) \
44 999: \
45 .section .codepatch, "a" ;\
46 .quad startnum##b ;\
47 .short (999b - startnum##b) ;\
48 .short tag ;\
49 .int 0 ;\
50 .previous
51#define CODEPATCH_END(tag) CODEPATCH_END2(998,tag)
52
53#define CPTAG_REPEAT_TLBI 1
54
55
56/* Would be neat if these could be in something like .cptext */
57#define CODEPATCH_CODE(symbol, instructions...) \
58 .section .rodata; \
59 .globl symbol; \
60symbol: instructions; \
61 .size symbol, . - symbol
62
63/* provide a (short) variable with the length of the patch */
64#define CODEPATCH_CODE_LEN(symbol, instructions...) \
65 CODEPATCH_CODE(symbol, instructions); \
66996: .globl symbol##_len; \
67 .align 2; \
68symbol##_len: \
69 .short 996b - symbol; \
70 .size symbol##_len, 2
71
72#endif /* _MACHINE_CODEPATCH_H_ */