|
24 | 24 | #define EMIT(instr) PLANT_INSTR(image, ctx->idx, instr) |
25 | 25 |
|
26 | 26 | /* Long jump; (unconditional 'branch') */ |
27 | | -#define PPC_JMP(dest) EMIT(PPC_INST_BRANCH | \ |
28 | | - (((dest) - (ctx->idx * 4)) & 0x03fffffc)) |
| 27 | +#define PPC_JMP(dest) \ |
| 28 | + do { \ |
| 29 | + long offset = (long)(dest) - (ctx->idx * 4); \ |
| 30 | + if (!is_offset_in_branch_range(offset)) { \ |
| 31 | + pr_err_ratelimited("Branch offset 0x%lx (@%u) out of range\n", offset, ctx->idx); \ |
| 32 | + return -ERANGE; \ |
| 33 | + } \ |
| 34 | + EMIT(PPC_INST_BRANCH | (offset & 0x03fffffc)); \ |
| 35 | + } while (0) |
| 36 | + |
29 | 37 | /* blr; (unconditional 'branch' with link) to absolute address */ |
30 | 38 | #define PPC_BL_ABS(dest) EMIT(PPC_INST_BL | \ |
31 | 39 | (((dest) - (unsigned long)(image + ctx->idx)) & 0x03fffffc)) |
32 | 40 | /* "cond" here covers BO:BI fields. */ |
33 | | -#define PPC_BCC_SHORT(cond, dest) EMIT(PPC_INST_BRANCH_COND | \ |
34 | | - (((cond) & 0x3ff) << 16) | \ |
35 | | - (((dest) - (ctx->idx * 4)) & \ |
36 | | - 0xfffc)) |
| 41 | +#define PPC_BCC_SHORT(cond, dest) \ |
| 42 | + do { \ |
| 43 | + long offset = (long)(dest) - (ctx->idx * 4); \ |
| 44 | + if (!is_offset_in_cond_branch_range(offset)) { \ |
| 45 | + pr_err_ratelimited("Conditional branch offset 0x%lx (@%u) out of range\n", offset, ctx->idx); \ |
| 46 | + return -ERANGE; \ |
| 47 | + } \ |
| 48 | + EMIT(PPC_INST_BRANCH_COND | (((cond) & 0x3ff) << 16) | (offset & 0xfffc)); \ |
| 49 | + } while (0) |
| 50 | + |
37 | 51 | /* Sign-extended 32-bit immediate load */ |
38 | 52 | #define PPC_LI32(d, i) do { \ |
39 | 53 | if ((int)(uintptr_t)(i) >= -32768 && \ |
|
0 commit comments