alternative.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Alternative live-patching for parisc.
  4. * Copyright (C) 2018 Helge Deller <[email protected]>
  5. *
  6. */
  7. #include <asm/processor.h>
  8. #include <asm/sections.h>
  9. #include <asm/alternative.h>
  10. #include <asm/cacheflush.h>
  11. #include <linux/module.h>
  12. static int no_alternatives;
  13. static int __init setup_no_alternatives(char *str)
  14. {
  15. no_alternatives = 1;
  16. return 1;
  17. }
  18. __setup("no-alternatives", setup_no_alternatives);
  19. void __init_or_module apply_alternatives(struct alt_instr *start,
  20. struct alt_instr *end, const char *module_name)
  21. {
  22. struct alt_instr *entry;
  23. int index = 0, applied = 0;
  24. int num_cpus = num_present_cpus();
  25. u16 cond_check;
  26. cond_check = ALT_COND_ALWAYS |
  27. ((num_cpus == 1) ? ALT_COND_NO_SMP : 0) |
  28. ((cache_info.dc_size == 0) ? ALT_COND_NO_DCACHE : 0) |
  29. ((cache_info.ic_size == 0) ? ALT_COND_NO_ICACHE : 0) |
  30. (running_on_qemu ? ALT_COND_RUN_ON_QEMU : 0) |
  31. ((split_tlb == 0) ? ALT_COND_NO_SPLIT_TLB : 0) |
  32. /*
  33. * If the PDC_MODEL capabilities has Non-coherent IO-PDIR bit
  34. * set (bit #61, big endian), we have to flush and sync every
  35. * time IO-PDIR is changed in Ike/Astro.
  36. */
  37. (((boot_cpu_data.cpu_type > pcxw_) &&
  38. ((boot_cpu_data.pdc.capabilities & PDC_MODEL_IOPDIR_FDC) == 0))
  39. ? ALT_COND_NO_IOC_FDC : 0);
  40. for (entry = start; entry < end; entry++, index++) {
  41. u32 *from, replacement;
  42. u16 cond;
  43. s16 len;
  44. from = (u32 *)((ulong)&entry->orig_offset + entry->orig_offset);
  45. len = entry->len;
  46. cond = entry->cond;
  47. replacement = entry->replacement;
  48. WARN_ON(!cond);
  49. if ((cond & ALT_COND_ALWAYS) == 0 && no_alternatives)
  50. continue;
  51. pr_debug("Check %d: Cond 0x%x, Replace %02d instructions @ 0x%px with 0x%08x\n",
  52. index, cond, len, from, replacement);
  53. /* Bounce out if none of the conditions are true. */
  54. if ((cond & cond_check) == 0)
  55. continue;
  56. /* Want to replace pdtlb by a pdtlb,l instruction? */
  57. if (replacement == INSN_PxTLB) {
  58. replacement = *from;
  59. if (boot_cpu_data.cpu_type >= pcxu) /* >= pa2.0 ? */
  60. replacement |= (1 << 10); /* set el bit */
  61. }
  62. /*
  63. * Replace instruction with NOPs?
  64. * For long distance insert a branch instruction instead.
  65. */
  66. if (replacement == INSN_NOP && len > 1)
  67. replacement = 0xe8000002 + (len-2)*8; /* "b,n .+8" */
  68. pr_debug("ALTERNATIVE %3d: Cond %2x, Replace %2d instructions to 0x%08x @ 0x%px (%pS)\n",
  69. index, cond, len, replacement, from, from);
  70. if (len < 0) {
  71. /* Replace multiple instruction by new code */
  72. u32 *source;
  73. len = -len;
  74. source = (u32 *)((ulong)&entry->replacement + entry->replacement);
  75. memcpy(from, source, 4 * len);
  76. } else {
  77. /* Replace by one instruction */
  78. *from = replacement;
  79. }
  80. applied++;
  81. }
  82. pr_info("%s%salternatives: applied %d out of %d patches\n",
  83. module_name ? : "", module_name ? " " : "",
  84. applied, index);
  85. }
  86. void __init apply_alternatives_all(void)
  87. {
  88. set_kernel_text_rw(1);
  89. apply_alternatives((struct alt_instr *) &__alt_instructions,
  90. (struct alt_instr *) &__alt_instructions_end, NULL);
  91. if (cache_info.dc_size == 0 && cache_info.ic_size == 0) {
  92. pr_info("alternatives: optimizing cache-flushes.\n");
  93. static_branch_disable(&parisc_has_cache);
  94. }
  95. if (cache_info.dc_size == 0)
  96. static_branch_disable(&parisc_has_dcache);
  97. if (cache_info.ic_size == 0)
  98. static_branch_disable(&parisc_has_icache);
  99. set_kernel_text_rw(0);
  100. }