code-patching.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /*
  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. #include <linux/kernel.h>
  10. #include <asm/code-patching.h>
  11. void patch_instruction(unsigned int *addr, unsigned int instr)
  12. {
  13. *addr = instr;
  14. asm ("dcbst 0, %0; sync; icbi 0,%0; sync; isync" : : "r" (addr));
  15. }
  16. void patch_branch(unsigned int *addr, unsigned long target, int flags)
  17. {
  18. patch_instruction(addr, create_branch(addr, target, flags));
  19. }
  20. unsigned int create_branch(const unsigned int *addr,
  21. unsigned long target, int flags)
  22. {
  23. unsigned int instruction;
  24. long offset;
  25. offset = target;
  26. if (! (flags & BRANCH_ABSOLUTE))
  27. offset = offset - (unsigned long)addr;
  28. /* Check we can represent the target in the instruction format */
  29. if (offset < -0x2000000 || offset > 0x1fffffc || offset & 0x3)
  30. return 0;
  31. /* Mask out the flags and target, so they don't step on each other. */
  32. instruction = 0x48000000 | (flags & 0x3) | (offset & 0x03FFFFFC);
  33. return instruction;
  34. }