blob: 430f4c15d7862f13bda92ef823b985781062304f [file] [log] [blame]
Michael Ellermanaaddd3e2008-06-24 11:32:21 +10001/*
2 * Copyright 2008 Michael Ellerman, IBM Corporation.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 */
9
10#include <linux/kernel.h>
11#include <asm/code-patching.h>
12
13
Michael Ellermane7a57272008-06-24 11:32:22 +100014void patch_instruction(unsigned int *addr, unsigned int instr)
Michael Ellermanaaddd3e2008-06-24 11:32:21 +100015{
Michael Ellermane7a57272008-06-24 11:32:22 +100016 *addr = instr;
17 asm ("dcbst 0, %0; sync; icbi 0,%0; sync; isync" : : "r" (addr));
Michael Ellermanaaddd3e2008-06-24 11:32:21 +100018}
19
Michael Ellermane7a57272008-06-24 11:32:22 +100020void patch_branch(unsigned int *addr, unsigned long target, int flags)
21{
22 patch_instruction(addr, create_branch(addr, target, flags));
23}
24
25unsigned int create_branch(const unsigned int *addr,
26 unsigned long target, int flags)
Michael Ellermanaaddd3e2008-06-24 11:32:21 +100027{
28 unsigned int instruction;
Michael Ellerman053a8582008-06-24 11:32:24 +100029 long offset;
Michael Ellermanaaddd3e2008-06-24 11:32:21 +100030
Michael Ellerman053a8582008-06-24 11:32:24 +100031 offset = target;
Michael Ellermanaaddd3e2008-06-24 11:32:21 +100032 if (! (flags & BRANCH_ABSOLUTE))
Michael Ellerman053a8582008-06-24 11:32:24 +100033 offset = offset - (unsigned long)addr;
34
35 /* Check we can represent the target in the instruction format */
36 if (offset < -0x2000000 || offset > 0x1fffffc || offset & 0x3)
37 return 0;
Michael Ellermanaaddd3e2008-06-24 11:32:21 +100038
39 /* Mask out the flags and target, so they don't step on each other. */
Michael Ellerman053a8582008-06-24 11:32:24 +100040 instruction = 0x48000000 | (flags & 0x3) | (offset & 0x03FFFFFC);
Michael Ellermanaaddd3e2008-06-24 11:32:21 +100041
Michael Ellermane7a57272008-06-24 11:32:22 +100042 return instruction;
Michael Ellermanaaddd3e2008-06-24 11:32:21 +100043}