linux/arch/powerpc/lib/feature-fixups.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 *  Copyright (C) 2001 Ben. Herrenschmidt (benh@kernel.crashing.org)
   4 *
   5 *  Modifications for ppc64:
   6 *      Copyright (C) 2003 Dave Engebretsen <engebret@us.ibm.com>
   7 *
   8 *  Copyright 2008 Michael Ellerman, IBM Corporation.
   9 */
  10
  11#include <linux/types.h>
  12#include <linux/jump_label.h>
  13#include <linux/kernel.h>
  14#include <linux/string.h>
  15#include <linux/init.h>
  16#include <linux/sched/mm.h>
  17#include <asm/cputable.h>
  18#include <asm/code-patching.h>
  19#include <asm/page.h>
  20#include <asm/sections.h>
  21#include <asm/setup.h>
  22#include <asm/security_features.h>
  23#include <asm/firmware.h>
  24#include <asm/inst.h>
  25
  26struct fixup_entry {
  27        unsigned long   mask;
  28        unsigned long   value;
  29        long            start_off;
  30        long            end_off;
  31        long            alt_start_off;
  32        long            alt_end_off;
  33};
  34
  35static struct ppc_inst *calc_addr(struct fixup_entry *fcur, long offset)
  36{
  37        /*
  38         * We store the offset to the code as a negative offset from
  39         * the start of the alt_entry, to support the VDSO. This
  40         * routine converts that back into an actual address.
  41         */
  42        return (struct ppc_inst *)((unsigned long)fcur + offset);
  43}
  44
  45static int patch_alt_instruction(struct ppc_inst *src, struct ppc_inst *dest,
  46                                 struct ppc_inst *alt_start, struct ppc_inst *alt_end)
  47{
  48        int err;
  49        struct ppc_inst instr;
  50
  51        instr = ppc_inst_read(src);
  52
  53        if (instr_is_relative_branch(*src)) {
  54                struct ppc_inst *target = (struct ppc_inst *)branch_target(src);
  55
  56                /* Branch within the section doesn't need translating */
  57                if (target < alt_start || target > alt_end) {
  58                        err = translate_branch(&instr, dest, src);
  59                        if (err)
  60                                return 1;
  61                }
  62        }
  63
  64        raw_patch_instruction(dest, instr);
  65
  66        return 0;
  67}
  68
  69static int patch_feature_section(unsigned long value, struct fixup_entry *fcur)
  70{
  71        struct ppc_inst *start, *end, *alt_start, *alt_end, *src, *dest, nop;
  72
  73        start = calc_addr(fcur, fcur->start_off);
  74        end = calc_addr(fcur, fcur->end_off);
  75        alt_start = calc_addr(fcur, fcur->alt_start_off);
  76        alt_end = calc_addr(fcur, fcur->alt_end_off);
  77
  78        if ((alt_end - alt_start) > (end - start))
  79                return 1;
  80
  81        if ((value & fcur->mask) == fcur->value)
  82                return 0;
  83
  84        src = alt_start;
  85        dest = start;
  86
  87        for (; src < alt_end; src = ppc_inst_next(src, src),
  88                              dest = ppc_inst_next(dest, dest)) {
  89                if (patch_alt_instruction(src, dest, alt_start, alt_end))
  90                        return 1;
  91        }
  92
  93        nop = ppc_inst(PPC_INST_NOP);
  94        for (; dest < end; dest = ppc_inst_next(dest, &nop))
  95                raw_patch_instruction(dest, nop);
  96
  97        return 0;
  98}
  99
 100void do_feature_fixups(unsigned long value, void *fixup_start, void *fixup_end)
 101{
 102        struct fixup_entry *fcur, *fend;
 103
 104        fcur = fixup_start;
 105        fend = fixup_end;
 106
 107        for (; fcur < fend; fcur++) {
 108                if (patch_feature_section(value, fcur)) {
 109                        WARN_ON(1);
 110                        printk("Unable to patch feature section at %p - %p" \
 111                                " with %p - %p\n",
 112                                calc_addr(fcur, fcur->start_off),
 113                                calc_addr(fcur, fcur->end_off),
 114                                calc_addr(fcur, fcur->alt_start_off),
 115                                calc_addr(fcur, fcur->alt_end_off));
 116                }
 117        }
 118}
 119
 120#ifdef CONFIG_PPC_BOOK3S_64
 121static void do_stf_entry_barrier_fixups(enum stf_barrier_type types)
 122{
 123        unsigned int instrs[3], *dest;
 124        long *start, *end;
 125        int i;
 126
 127        start = PTRRELOC(&__start___stf_entry_barrier_fixup),
 128        end = PTRRELOC(&__stop___stf_entry_barrier_fixup);
 129
 130        instrs[0] = 0x60000000; /* nop */
 131        instrs[1] = 0x60000000; /* nop */
 132        instrs[2] = 0x60000000; /* nop */
 133
 134        i = 0;
 135        if (types & STF_BARRIER_FALLBACK) {
 136                instrs[i++] = 0x7d4802a6; /* mflr r10           */
 137                instrs[i++] = 0x60000000; /* branch patched below */
 138                instrs[i++] = 0x7d4803a6; /* mtlr r10           */
 139        } else if (types & STF_BARRIER_EIEIO) {
 140                instrs[i++] = 0x7e0006ac; /* eieio + bit 6 hint */
 141        } else if (types & STF_BARRIER_SYNC_ORI) {
 142                instrs[i++] = 0x7c0004ac; /* hwsync             */
 143                instrs[i++] = 0xe94d0000; /* ld r10,0(r13)      */
 144                instrs[i++] = 0x63ff0000; /* ori 31,31,0 speculation barrier */
 145        }
 146
 147        for (i = 0; start < end; start++, i++) {
 148                dest = (void *)start + *start;
 149
 150                pr_devel("patching dest %lx\n", (unsigned long)dest);
 151
 152                patch_instruction((struct ppc_inst *)dest, ppc_inst(instrs[0]));
 153
 154                if (types & STF_BARRIER_FALLBACK)
 155                        patch_branch((struct ppc_inst *)(dest + 1),
 156                                     (unsigned long)&stf_barrier_fallback,
 157                                     BRANCH_SET_LINK);
 158                else
 159                        patch_instruction((struct ppc_inst *)(dest + 1),
 160                                          ppc_inst(instrs[1]));
 161
 162                patch_instruction((struct ppc_inst *)(dest + 2), ppc_inst(instrs[2]));
 163        }
 164
 165        printk(KERN_DEBUG "stf-barrier: patched %d entry locations (%s barrier)\n", i,
 166                (types == STF_BARRIER_NONE)                  ? "no" :
 167                (types == STF_BARRIER_FALLBACK)              ? "fallback" :
 168                (types == STF_BARRIER_EIEIO)                 ? "eieio" :
 169                (types == (STF_BARRIER_SYNC_ORI))            ? "hwsync"
 170                                                           : "unknown");
 171}
 172
 173static void do_stf_exit_barrier_fixups(enum stf_barrier_type types)
 174{
 175        unsigned int instrs[6], *dest;
 176        long *start, *end;
 177        int i;
 178
 179        start = PTRRELOC(&__start___stf_exit_barrier_fixup),
 180        end = PTRRELOC(&__stop___stf_exit_barrier_fixup);
 181
 182        instrs[0] = 0x60000000; /* nop */
 183        instrs[1] = 0x60000000; /* nop */
 184        instrs[2] = 0x60000000; /* nop */
 185        instrs[3] = 0x60000000; /* nop */
 186        instrs[4] = 0x60000000; /* nop */
 187        instrs[5] = 0x60000000; /* nop */
 188
 189        i = 0;
 190        if (types & STF_BARRIER_FALLBACK || types & STF_BARRIER_SYNC_ORI) {
 191                if (cpu_has_feature(CPU_FTR_HVMODE)) {
 192                        instrs[i++] = 0x7db14ba6; /* mtspr 0x131, r13 (HSPRG1) */
 193                        instrs[i++] = 0x7db04aa6; /* mfspr r13, 0x130 (HSPRG0) */
 194                } else {
 195                        instrs[i++] = 0x7db243a6; /* mtsprg 2,r13       */
 196                        instrs[i++] = 0x7db142a6; /* mfsprg r13,1    */
 197                }
 198                instrs[i++] = 0x7c0004ac; /* hwsync             */
 199                instrs[i++] = 0xe9ad0000; /* ld r13,0(r13)      */
 200                instrs[i++] = 0x63ff0000; /* ori 31,31,0 speculation barrier */
 201                if (cpu_has_feature(CPU_FTR_HVMODE)) {
 202                        instrs[i++] = 0x7db14aa6; /* mfspr r13, 0x131 (HSPRG1) */
 203                } else {
 204                        instrs[i++] = 0x7db242a6; /* mfsprg r13,2 */
 205                }
 206        } else if (types & STF_BARRIER_EIEIO) {
 207                instrs[i++] = 0x7e0006ac; /* eieio + bit 6 hint */
 208        }
 209
 210        for (i = 0; start < end; start++, i++) {
 211                dest = (void *)start + *start;
 212
 213                pr_devel("patching dest %lx\n", (unsigned long)dest);
 214
 215                patch_instruction((struct ppc_inst *)dest, ppc_inst(instrs[0]));
 216                patch_instruction((struct ppc_inst *)(dest + 1), ppc_inst(instrs[1]));
 217                patch_instruction((struct ppc_inst *)(dest + 2), ppc_inst(instrs[2]));
 218                patch_instruction((struct ppc_inst *)(dest + 3), ppc_inst(instrs[3]));
 219                patch_instruction((struct ppc_inst *)(dest + 4), ppc_inst(instrs[4]));
 220                patch_instruction((struct ppc_inst *)(dest + 5), ppc_inst(instrs[5]));
 221        }
 222        printk(KERN_DEBUG "stf-barrier: patched %d exit locations (%s barrier)\n", i,
 223                (types == STF_BARRIER_NONE)                  ? "no" :
 224                (types == STF_BARRIER_FALLBACK)              ? "fallback" :
 225                (types == STF_BARRIER_EIEIO)                 ? "eieio" :
 226                (types == (STF_BARRIER_SYNC_ORI))            ? "hwsync"
 227                                                           : "unknown");
 228}
 229
 230
 231void do_stf_barrier_fixups(enum stf_barrier_type types)
 232{
 233        do_stf_entry_barrier_fixups(types);
 234        do_stf_exit_barrier_fixups(types);
 235}
 236
 237void do_rfi_flush_fixups(enum l1d_flush_type types)
 238{
 239        unsigned int instrs[3], *dest;
 240        long *start, *end;
 241        int i;
 242
 243        start = PTRRELOC(&__start___rfi_flush_fixup),
 244        end = PTRRELOC(&__stop___rfi_flush_fixup);
 245
 246        instrs[0] = 0x60000000; /* nop */
 247        instrs[1] = 0x60000000; /* nop */
 248        instrs[2] = 0x60000000; /* nop */
 249
 250        if (types & L1D_FLUSH_FALLBACK)
 251                /* b .+16 to fallback flush */
 252                instrs[0] = 0x48000010;
 253
 254        i = 0;
 255        if (types & L1D_FLUSH_ORI) {
 256                instrs[i++] = 0x63ff0000; /* ori 31,31,0 speculation barrier */
 257                instrs[i++] = 0x63de0000; /* ori 30,30,0 L1d flush*/
 258        }
 259
 260        if (types & L1D_FLUSH_MTTRIG)
 261                instrs[i++] = 0x7c12dba6; /* mtspr TRIG2,r0 (SPR #882) */
 262
 263        for (i = 0; start < end; start++, i++) {
 264                dest = (void *)start + *start;
 265
 266                pr_devel("patching dest %lx\n", (unsigned long)dest);
 267
 268                patch_instruction((struct ppc_inst *)dest, ppc_inst(instrs[0]));
 269                patch_instruction((struct ppc_inst *)(dest + 1), ppc_inst(instrs[1]));
 270                patch_instruction((struct ppc_inst *)(dest + 2), ppc_inst(instrs[2]));
 271        }
 272
 273        printk(KERN_DEBUG "rfi-flush: patched %d locations (%s flush)\n", i,
 274                (types == L1D_FLUSH_NONE)       ? "no" :
 275                (types == L1D_FLUSH_FALLBACK)   ? "fallback displacement" :
 276                (types &  L1D_FLUSH_ORI)        ? (types & L1D_FLUSH_MTTRIG)
 277                                                        ? "ori+mttrig type"
 278                                                        : "ori type" :
 279                (types &  L1D_FLUSH_MTTRIG)     ? "mttrig type"
 280                                                : "unknown");
 281}
 282
 283void do_barrier_nospec_fixups_range(bool enable, void *fixup_start, void *fixup_end)
 284{
 285        unsigned int instr, *dest;
 286        long *start, *end;
 287        int i;
 288
 289        start = fixup_start;
 290        end = fixup_end;
 291
 292        instr = 0x60000000; /* nop */
 293
 294        if (enable) {
 295                pr_info("barrier-nospec: using ORI speculation barrier\n");
 296                instr = 0x63ff0000; /* ori 31,31,0 speculation barrier */
 297        }
 298
 299        for (i = 0; start < end; start++, i++) {
 300                dest = (void *)start + *start;
 301
 302                pr_devel("patching dest %lx\n", (unsigned long)dest);
 303                patch_instruction((struct ppc_inst *)dest, ppc_inst(instr));
 304        }
 305
 306        printk(KERN_DEBUG "barrier-nospec: patched %d locations\n", i);
 307}
 308
 309#endif /* CONFIG_PPC_BOOK3S_64 */
 310
 311#ifdef CONFIG_PPC_BARRIER_NOSPEC
 312void do_barrier_nospec_fixups(bool enable)
 313{
 314        void *start, *end;
 315
 316        start = PTRRELOC(&__start___barrier_nospec_fixup),
 317        end = PTRRELOC(&__stop___barrier_nospec_fixup);
 318
 319        do_barrier_nospec_fixups_range(enable, start, end);
 320}
 321#endif /* CONFIG_PPC_BARRIER_NOSPEC */
 322
 323#ifdef CONFIG_PPC_FSL_BOOK3E
 324void do_barrier_nospec_fixups_range(bool enable, void *fixup_start, void *fixup_end)
 325{
 326        unsigned int instr[2], *dest;
 327        long *start, *end;
 328        int i;
 329
 330        start = fixup_start;
 331        end = fixup_end;
 332
 333        instr[0] = PPC_INST_NOP;
 334        instr[1] = PPC_INST_NOP;
 335
 336        if (enable) {
 337                pr_info("barrier-nospec: using isync; sync as speculation barrier\n");
 338                instr[0] = PPC_INST_ISYNC;
 339                instr[1] = PPC_INST_SYNC;
 340        }
 341
 342        for (i = 0; start < end; start++, i++) {
 343                dest = (void *)start + *start;
 344
 345                pr_devel("patching dest %lx\n", (unsigned long)dest);
 346                patch_instruction((struct ppc_inst *)dest, ppc_inst(instr[0]));
 347                patch_instruction((struct ppc_inst *)(dest + 1), ppc_inst(instr[1]));
 348        }
 349
 350        printk(KERN_DEBUG "barrier-nospec: patched %d locations\n", i);
 351}
 352
 353static void patch_btb_flush_section(long *curr)
 354{
 355        unsigned int *start, *end;
 356
 357        start = (void *)curr + *curr;
 358        end = (void *)curr + *(curr + 1);
 359        for (; start < end; start++) {
 360                pr_devel("patching dest %lx\n", (unsigned long)start);
 361                patch_instruction((struct ppc_inst *)start, ppc_inst(PPC_INST_NOP));
 362        }
 363}
 364
 365void do_btb_flush_fixups(void)
 366{
 367        long *start, *end;
 368
 369        start = PTRRELOC(&__start__btb_flush_fixup);
 370        end = PTRRELOC(&__stop__btb_flush_fixup);
 371
 372        for (; start < end; start += 2)
 373                patch_btb_flush_section(start);
 374}
 375#endif /* CONFIG_PPC_FSL_BOOK3E */
 376
 377void do_lwsync_fixups(unsigned long value, void *fixup_start, void *fixup_end)
 378{
 379        long *start, *end;
 380        struct ppc_inst *dest;
 381
 382        if (!(value & CPU_FTR_LWSYNC))
 383                return ;
 384
 385        start = fixup_start;
 386        end = fixup_end;
 387
 388        for (; start < end; start++) {
 389                dest = (void *)start + *start;
 390                raw_patch_instruction(dest, ppc_inst(PPC_INST_LWSYNC));
 391        }
 392}
 393
 394static void do_final_fixups(void)
 395{
 396#if defined(CONFIG_PPC64) && defined(CONFIG_RELOCATABLE)
 397        struct ppc_inst inst, *src, *dest, *end;
 398
 399        if (PHYSICAL_START == 0)
 400                return;
 401
 402        src = (struct ppc_inst *)(KERNELBASE + PHYSICAL_START);
 403        dest = (struct ppc_inst *)KERNELBASE;
 404        end = (void *)src + (__end_interrupts - _stext);
 405
 406        while (src < end) {
 407                inst = ppc_inst_read(src);
 408                raw_patch_instruction(dest, inst);
 409                src = ppc_inst_next(src, src);
 410                dest = ppc_inst_next(dest, dest);
 411        }
 412#endif
 413}
 414
 415static unsigned long __initdata saved_cpu_features;
 416static unsigned int __initdata saved_mmu_features;
 417#ifdef CONFIG_PPC64
 418static unsigned long __initdata saved_firmware_features;
 419#endif
 420
 421void __init apply_feature_fixups(void)
 422{
 423        struct cpu_spec *spec = PTRRELOC(*PTRRELOC(&cur_cpu_spec));
 424
 425        *PTRRELOC(&saved_cpu_features) = spec->cpu_features;
 426        *PTRRELOC(&saved_mmu_features) = spec->mmu_features;
 427
 428        /*
 429         * Apply the CPU-specific and firmware specific fixups to kernel text
 430         * (nop out sections not relevant to this CPU or this firmware).
 431         */
 432        do_feature_fixups(spec->cpu_features,
 433                          PTRRELOC(&__start___ftr_fixup),
 434                          PTRRELOC(&__stop___ftr_fixup));
 435
 436        do_feature_fixups(spec->mmu_features,
 437                          PTRRELOC(&__start___mmu_ftr_fixup),
 438                          PTRRELOC(&__stop___mmu_ftr_fixup));
 439
 440        do_lwsync_fixups(spec->cpu_features,
 441                         PTRRELOC(&__start___lwsync_fixup),
 442                         PTRRELOC(&__stop___lwsync_fixup));
 443
 444#ifdef CONFIG_PPC64
 445        saved_firmware_features = powerpc_firmware_features;
 446        do_feature_fixups(powerpc_firmware_features,
 447                          &__start___fw_ftr_fixup, &__stop___fw_ftr_fixup);
 448#endif
 449        do_final_fixups();
 450}
 451
 452void __init setup_feature_keys(void)
 453{
 454        /*
 455         * Initialise jump label. This causes all the cpu/mmu_has_feature()
 456         * checks to take on their correct polarity based on the current set of
 457         * CPU/MMU features.
 458         */
 459        jump_label_init();
 460        cpu_feature_keys_init();
 461        mmu_feature_keys_init();
 462}
 463
 464static int __init check_features(void)
 465{
 466        WARN(saved_cpu_features != cur_cpu_spec->cpu_features,
 467             "CPU features changed after feature patching!\n");
 468        WARN(saved_mmu_features != cur_cpu_spec->mmu_features,
 469             "MMU features changed after feature patching!\n");
 470#ifdef CONFIG_PPC64
 471        WARN(saved_firmware_features != powerpc_firmware_features,
 472             "Firmware features changed after feature patching!\n");
 473#endif
 474
 475        return 0;
 476}
 477late_initcall(check_features);
 478
 479#ifdef CONFIG_FTR_FIXUP_SELFTEST
 480
 481#define check(x)        \
 482        if (!(x)) printk("feature-fixups: test failed at line %d\n", __LINE__);
 483
 484/* This must be after the text it fixes up, vmlinux.lds.S enforces that atm */
 485static struct fixup_entry fixup;
 486
 487static long calc_offset(struct fixup_entry *entry, unsigned int *p)
 488{
 489        return (unsigned long)p - (unsigned long)entry;
 490}
 491
 492static void test_basic_patching(void)
 493{
 494        extern unsigned int ftr_fixup_test1[];
 495        extern unsigned int end_ftr_fixup_test1[];
 496        extern unsigned int ftr_fixup_test1_orig[];
 497        extern unsigned int ftr_fixup_test1_expected[];
 498        int size = 4 * (end_ftr_fixup_test1 - ftr_fixup_test1);
 499
 500        fixup.value = fixup.mask = 8;
 501        fixup.start_off = calc_offset(&fixup, ftr_fixup_test1 + 1);
 502        fixup.end_off = calc_offset(&fixup, ftr_fixup_test1 + 2);
 503        fixup.alt_start_off = fixup.alt_end_off = 0;
 504
 505        /* Sanity check */
 506        check(memcmp(ftr_fixup_test1, ftr_fixup_test1_orig, size) == 0);
 507
 508        /* Check we don't patch if the value matches */
 509        patch_feature_section(8, &fixup);
 510        check(memcmp(ftr_fixup_test1, ftr_fixup_test1_orig, size) == 0);
 511
 512        /* Check we do patch if the value doesn't match */
 513        patch_feature_section(0, &fixup);
 514        check(memcmp(ftr_fixup_test1, ftr_fixup_test1_expected, size) == 0);
 515
 516        /* Check we do patch if the mask doesn't match */
 517        memcpy(ftr_fixup_test1, ftr_fixup_test1_orig, size);
 518        check(memcmp(ftr_fixup_test1, ftr_fixup_test1_orig, size) == 0);
 519        patch_feature_section(~8, &fixup);
 520        check(memcmp(ftr_fixup_test1, ftr_fixup_test1_expected, size) == 0);
 521}
 522
 523static void test_alternative_patching(void)
 524{
 525        extern unsigned int ftr_fixup_test2[];
 526        extern unsigned int end_ftr_fixup_test2[];
 527        extern unsigned int ftr_fixup_test2_orig[];
 528        extern unsigned int ftr_fixup_test2_alt[];
 529        extern unsigned int ftr_fixup_test2_expected[];
 530        int size = 4 * (end_ftr_fixup_test2 - ftr_fixup_test2);
 531
 532        fixup.value = fixup.mask = 0xF;
 533        fixup.start_off = calc_offset(&fixup, ftr_fixup_test2 + 1);
 534        fixup.end_off = calc_offset(&fixup, ftr_fixup_test2 + 2);
 535        fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_test2_alt);
 536        fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_test2_alt + 1);
 537
 538        /* Sanity check */
 539        check(memcmp(ftr_fixup_test2, ftr_fixup_test2_orig, size) == 0);
 540
 541        /* Check we don't patch if the value matches */
 542        patch_feature_section(0xF, &fixup);
 543        check(memcmp(ftr_fixup_test2, ftr_fixup_test2_orig, size) == 0);
 544
 545        /* Check we do patch if the value doesn't match */
 546        patch_feature_section(0, &fixup);
 547        check(memcmp(ftr_fixup_test2, ftr_fixup_test2_expected, size) == 0);
 548
 549        /* Check we do patch if the mask doesn't match */
 550        memcpy(ftr_fixup_test2, ftr_fixup_test2_orig, size);
 551        check(memcmp(ftr_fixup_test2, ftr_fixup_test2_orig, size) == 0);
 552        patch_feature_section(~0xF, &fixup);
 553        check(memcmp(ftr_fixup_test2, ftr_fixup_test2_expected, size) == 0);
 554}
 555
 556static void test_alternative_case_too_big(void)
 557{
 558        extern unsigned int ftr_fixup_test3[];
 559        extern unsigned int end_ftr_fixup_test3[];
 560        extern unsigned int ftr_fixup_test3_orig[];
 561        extern unsigned int ftr_fixup_test3_alt[];
 562        int size = 4 * (end_ftr_fixup_test3 - ftr_fixup_test3);
 563
 564        fixup.value = fixup.mask = 0xC;
 565        fixup.start_off = calc_offset(&fixup, ftr_fixup_test3 + 1);
 566        fixup.end_off = calc_offset(&fixup, ftr_fixup_test3 + 2);
 567        fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_test3_alt);
 568        fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_test3_alt + 2);
 569
 570        /* Sanity check */
 571        check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
 572
 573        /* Expect nothing to be patched, and the error returned to us */
 574        check(patch_feature_section(0xF, &fixup) == 1);
 575        check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
 576        check(patch_feature_section(0, &fixup) == 1);
 577        check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
 578        check(patch_feature_section(~0xF, &fixup) == 1);
 579        check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
 580}
 581
 582static void test_alternative_case_too_small(void)
 583{
 584        extern unsigned int ftr_fixup_test4[];
 585        extern unsigned int end_ftr_fixup_test4[];
 586        extern unsigned int ftr_fixup_test4_orig[];
 587        extern unsigned int ftr_fixup_test4_alt[];
 588        extern unsigned int ftr_fixup_test4_expected[];
 589        int size = 4 * (end_ftr_fixup_test4 - ftr_fixup_test4);
 590        unsigned long flag;
 591
 592        /* Check a high-bit flag */
 593        flag = 1UL << ((sizeof(unsigned long) - 1) * 8);
 594        fixup.value = fixup.mask = flag;
 595        fixup.start_off = calc_offset(&fixup, ftr_fixup_test4 + 1);
 596        fixup.end_off = calc_offset(&fixup, ftr_fixup_test4 + 5);
 597        fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_test4_alt);
 598        fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_test4_alt + 2);
 599
 600        /* Sanity check */
 601        check(memcmp(ftr_fixup_test4, ftr_fixup_test4_orig, size) == 0);
 602
 603        /* Check we don't patch if the value matches */
 604        patch_feature_section(flag, &fixup);
 605        check(memcmp(ftr_fixup_test4, ftr_fixup_test4_orig, size) == 0);
 606
 607        /* Check we do patch if the value doesn't match */
 608        patch_feature_section(0, &fixup);
 609        check(memcmp(ftr_fixup_test4, ftr_fixup_test4_expected, size) == 0);
 610
 611        /* Check we do patch if the mask doesn't match */
 612        memcpy(ftr_fixup_test4, ftr_fixup_test4_orig, size);
 613        check(memcmp(ftr_fixup_test4, ftr_fixup_test4_orig, size) == 0);
 614        patch_feature_section(~flag, &fixup);
 615        check(memcmp(ftr_fixup_test4, ftr_fixup_test4_expected, size) == 0);
 616}
 617
 618static void test_alternative_case_with_branch(void)
 619{
 620        extern unsigned int ftr_fixup_test5[];
 621        extern unsigned int end_ftr_fixup_test5[];
 622        extern unsigned int ftr_fixup_test5_expected[];
 623        int size = 4 * (end_ftr_fixup_test5 - ftr_fixup_test5);
 624
 625        check(memcmp(ftr_fixup_test5, ftr_fixup_test5_expected, size) == 0);
 626}
 627
 628static void test_alternative_case_with_external_branch(void)
 629{
 630        extern unsigned int ftr_fixup_test6[];
 631        extern unsigned int end_ftr_fixup_test6[];
 632        extern unsigned int ftr_fixup_test6_expected[];
 633        int size = 4 * (end_ftr_fixup_test6 - ftr_fixup_test6);
 634
 635        check(memcmp(ftr_fixup_test6, ftr_fixup_test6_expected, size) == 0);
 636}
 637
 638static void test_alternative_case_with_branch_to_end(void)
 639{
 640        extern unsigned int ftr_fixup_test7[];
 641        extern unsigned int end_ftr_fixup_test7[];
 642        extern unsigned int ftr_fixup_test7_expected[];
 643        int size = 4 * (end_ftr_fixup_test7 - ftr_fixup_test7);
 644
 645        check(memcmp(ftr_fixup_test7, ftr_fixup_test7_expected, size) == 0);
 646}
 647
 648static void test_cpu_macros(void)
 649{
 650        extern u8 ftr_fixup_test_FTR_macros[];
 651        extern u8 ftr_fixup_test_FTR_macros_expected[];
 652        unsigned long size = ftr_fixup_test_FTR_macros_expected -
 653                             ftr_fixup_test_FTR_macros;
 654
 655        /* The fixups have already been done for us during boot */
 656        check(memcmp(ftr_fixup_test_FTR_macros,
 657                     ftr_fixup_test_FTR_macros_expected, size) == 0);
 658}
 659
 660static void test_fw_macros(void)
 661{
 662#ifdef CONFIG_PPC64
 663        extern u8 ftr_fixup_test_FW_FTR_macros[];
 664        extern u8 ftr_fixup_test_FW_FTR_macros_expected[];
 665        unsigned long size = ftr_fixup_test_FW_FTR_macros_expected -
 666                             ftr_fixup_test_FW_FTR_macros;
 667
 668        /* The fixups have already been done for us during boot */
 669        check(memcmp(ftr_fixup_test_FW_FTR_macros,
 670                     ftr_fixup_test_FW_FTR_macros_expected, size) == 0);
 671#endif
 672}
 673
 674static void test_lwsync_macros(void)
 675{
 676        extern u8 lwsync_fixup_test[];
 677        extern u8 end_lwsync_fixup_test[];
 678        extern u8 lwsync_fixup_test_expected_LWSYNC[];
 679        extern u8 lwsync_fixup_test_expected_SYNC[];
 680        unsigned long size = end_lwsync_fixup_test -
 681                             lwsync_fixup_test;
 682
 683        /* The fixups have already been done for us during boot */
 684        if (cur_cpu_spec->cpu_features & CPU_FTR_LWSYNC) {
 685                check(memcmp(lwsync_fixup_test,
 686                             lwsync_fixup_test_expected_LWSYNC, size) == 0);
 687        } else {
 688                check(memcmp(lwsync_fixup_test,
 689                             lwsync_fixup_test_expected_SYNC, size) == 0);
 690        }
 691}
 692
 693#ifdef CONFIG_PPC64
 694static void __init test_prefix_patching(void)
 695{
 696        extern unsigned int ftr_fixup_prefix1[];
 697        extern unsigned int end_ftr_fixup_prefix1[];
 698        extern unsigned int ftr_fixup_prefix1_orig[];
 699        extern unsigned int ftr_fixup_prefix1_expected[];
 700        int size = sizeof(unsigned int) * (end_ftr_fixup_prefix1 - ftr_fixup_prefix1);
 701
 702        fixup.value = fixup.mask = 8;
 703        fixup.start_off = calc_offset(&fixup, ftr_fixup_prefix1 + 1);
 704        fixup.end_off = calc_offset(&fixup, ftr_fixup_prefix1 + 3);
 705        fixup.alt_start_off = fixup.alt_end_off = 0;
 706
 707        /* Sanity check */
 708        check(memcmp(ftr_fixup_prefix1, ftr_fixup_prefix1_orig, size) == 0);
 709
 710        patch_feature_section(0, &fixup);
 711        check(memcmp(ftr_fixup_prefix1, ftr_fixup_prefix1_expected, size) == 0);
 712        check(memcmp(ftr_fixup_prefix1, ftr_fixup_prefix1_orig, size) != 0);
 713}
 714
 715static void __init test_prefix_alt_patching(void)
 716{
 717        extern unsigned int ftr_fixup_prefix2[];
 718        extern unsigned int end_ftr_fixup_prefix2[];
 719        extern unsigned int ftr_fixup_prefix2_orig[];
 720        extern unsigned int ftr_fixup_prefix2_expected[];
 721        extern unsigned int ftr_fixup_prefix2_alt[];
 722        int size = sizeof(unsigned int) * (end_ftr_fixup_prefix2 - ftr_fixup_prefix2);
 723
 724        fixup.value = fixup.mask = 8;
 725        fixup.start_off = calc_offset(&fixup, ftr_fixup_prefix2 + 1);
 726        fixup.end_off = calc_offset(&fixup, ftr_fixup_prefix2 + 3);
 727        fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_prefix2_alt);
 728        fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_prefix2_alt + 2);
 729        /* Sanity check */
 730        check(memcmp(ftr_fixup_prefix2, ftr_fixup_prefix2_orig, size) == 0);
 731
 732        patch_feature_section(0, &fixup);
 733        check(memcmp(ftr_fixup_prefix2, ftr_fixup_prefix2_expected, size) == 0);
 734        check(memcmp(ftr_fixup_prefix2, ftr_fixup_prefix2_orig, size) != 0);
 735}
 736
 737static void __init test_prefix_word_alt_patching(void)
 738{
 739        extern unsigned int ftr_fixup_prefix3[];
 740        extern unsigned int end_ftr_fixup_prefix3[];
 741        extern unsigned int ftr_fixup_prefix3_orig[];
 742        extern unsigned int ftr_fixup_prefix3_expected[];
 743        extern unsigned int ftr_fixup_prefix3_alt[];
 744        int size = sizeof(unsigned int) * (end_ftr_fixup_prefix3 - ftr_fixup_prefix3);
 745
 746        fixup.value = fixup.mask = 8;
 747        fixup.start_off = calc_offset(&fixup, ftr_fixup_prefix3 + 1);
 748        fixup.end_off = calc_offset(&fixup, ftr_fixup_prefix3 + 4);
 749        fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_prefix3_alt);
 750        fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_prefix3_alt + 3);
 751        /* Sanity check */
 752        check(memcmp(ftr_fixup_prefix3, ftr_fixup_prefix3_orig, size) == 0);
 753
 754        patch_feature_section(0, &fixup);
 755        check(memcmp(ftr_fixup_prefix3, ftr_fixup_prefix3_expected, size) == 0);
 756        patch_feature_section(0, &fixup);
 757        check(memcmp(ftr_fixup_prefix3, ftr_fixup_prefix3_orig, size) != 0);
 758}
 759#else
 760static inline void test_prefix_patching(void) {}
 761static inline void test_prefix_alt_patching(void) {}
 762static inline void test_prefix_word_alt_patching(void) {}
 763#endif /* CONFIG_PPC64 */
 764
 765static int __init test_feature_fixups(void)
 766{
 767        printk(KERN_DEBUG "Running feature fixup self-tests ...\n");
 768
 769        test_basic_patching();
 770        test_alternative_patching();
 771        test_alternative_case_too_big();
 772        test_alternative_case_too_small();
 773        test_alternative_case_with_branch();
 774        test_alternative_case_with_external_branch();
 775        test_alternative_case_with_branch_to_end();
 776        test_cpu_macros();
 777        test_fw_macros();
 778        test_lwsync_macros();
 779        test_prefix_patching();
 780        test_prefix_alt_patching();
 781        test_prefix_word_alt_patching();
 782
 783        return 0;
 784}
 785late_initcall(test_feature_fixups);
 786
 787#endif /* CONFIG_FTR_FIXUP_SELFTEST */
 788