Skip to content

Commit 6b73044

Browse files
author
Martin Schwidefsky
committed
s390: run user space and KVM guests with modified branch prediction
Define TIF_ISOLATE_BP and TIF_ISOLATE_BP_GUEST and add the necessary plumbing in entry.S to be able to run user space and KVM guests with limited branch prediction. To switch a user space process to limited branch prediction the s390_isolate_bp() function has to be call, and to run a vCPU of a KVM guest associated with the current task with limited branch prediction call s390_isolate_bp_guest(). Signed-off-by: Martin Schwidefsky <[email protected]>
1 parent d768bd8 commit 6b73044

File tree

4 files changed

+71
-5
lines changed

4 files changed

+71
-5
lines changed

arch/s390/include/asm/processor.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,9 @@ extern void memcpy_absolute(void *, void *, size_t);
378378
memcpy_absolute(&(dest), &__tmp, sizeof(__tmp)); \
379379
} while (0)
380380

381+
extern int s390_isolate_bp(void);
382+
extern int s390_isolate_bp_guest(void);
383+
381384
#endif /* __ASSEMBLY__ */
382385

383386
#endif /* __ASM_S390_PROCESSOR_H */

arch/s390/include/asm/thread_info.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src);
5858
#define TIF_GUARDED_STORAGE 4 /* load guarded storage control block */
5959
#define TIF_PATCH_PENDING 5 /* pending live patching update */
6060
#define TIF_PGSTE 6 /* New mm's will use 4K page tables */
61+
#define TIF_ISOLATE_BP 8 /* Run process with isolated BP */
62+
#define TIF_ISOLATE_BP_GUEST 9 /* Run KVM guests with isolated BP */
6163

6264
#define TIF_31BIT 16 /* 32bit process */
6365
#define TIF_MEMDIE 17 /* is terminating due to OOM killer */
@@ -78,6 +80,8 @@ int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src);
7880
#define _TIF_UPROBE _BITUL(TIF_UPROBE)
7981
#define _TIF_GUARDED_STORAGE _BITUL(TIF_GUARDED_STORAGE)
8082
#define _TIF_PATCH_PENDING _BITUL(TIF_PATCH_PENDING)
83+
#define _TIF_ISOLATE_BP _BITUL(TIF_ISOLATE_BP)
84+
#define _TIF_ISOLATE_BP_GUEST _BITUL(TIF_ISOLATE_BP_GUEST)
8185

8286
#define _TIF_31BIT _BITUL(TIF_31BIT)
8387
#define _TIF_SINGLE_STEP _BITUL(TIF_SINGLE_STEP)

arch/s390/kernel/entry.S

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ _PIF_WORK = (_PIF_PER_TRAP | _PIF_SYSCALL_RESTART)
107107
aghi %r15,-(STACK_FRAME_OVERHEAD + __PT_SIZE)
108108
j 3f
109109
1: UPDATE_VTIME %r14,%r15,\timer
110+
BPENTER __TI_flags(%r12),_TIF_ISOLATE_BP
110111
2: lg %r15,__LC_ASYNC_STACK # load async stack
111112
3: la %r11,STACK_FRAME_OVERHEAD(%r15)
112113
.endm
@@ -187,6 +188,40 @@ _PIF_WORK = (_PIF_PER_TRAP | _PIF_SYSCALL_RESTART)
187188
.popsection
188189
.endm
189190

191+
.macro BPENTER tif_ptr,tif_mask
192+
.pushsection .altinstr_replacement, "ax"
193+
662: .word 0xc004, 0x0000, 0x0000 # 6 byte nop
194+
.word 0xc004, 0x0000, 0x0000 # 6 byte nop
195+
.popsection
196+
664: TSTMSK \tif_ptr,\tif_mask
197+
jz . + 8
198+
.long 0xb2e8d000
199+
.pushsection .altinstructions, "a"
200+
.long 664b - .
201+
.long 662b - .
202+
.word 82
203+
.byte 12
204+
.byte 12
205+
.popsection
206+
.endm
207+
208+
.macro BPEXIT tif_ptr,tif_mask
209+
TSTMSK \tif_ptr,\tif_mask
210+
.pushsection .altinstr_replacement, "ax"
211+
662: jnz . + 8
212+
.long 0xb2e8d000
213+
.popsection
214+
664: jz . + 8
215+
.long 0xb2e8c000
216+
.pushsection .altinstructions, "a"
217+
.long 664b - .
218+
.long 662b - .
219+
.word 82
220+
.byte 8
221+
.byte 8
222+
.popsection
223+
.endm
224+
190225
.section .kprobes.text, "ax"
191226
.Ldummy:
192227
/*
@@ -240,9 +275,11 @@ ENTRY(__switch_to)
240275
*/
241276
ENTRY(sie64a)
242277
stmg %r6,%r14,__SF_GPRS(%r15) # save kernel registers
278+
lg %r12,__LC_CURRENT
243279
stg %r2,__SF_EMPTY(%r15) # save control block pointer
244280
stg %r3,__SF_EMPTY+8(%r15) # save guest register save area
245281
xc __SF_EMPTY+16(8,%r15),__SF_EMPTY+16(%r15) # reason code = 0
282+
mvc __SF_EMPTY+24(8,%r15),__TI_flags(%r12) # copy thread flags
246283
TSTMSK __LC_CPU_FLAGS,_CIF_FPU # load guest fp/vx registers ?
247284
jno .Lsie_load_guest_gprs
248285
brasl %r14,load_fpu_regs # load guest fp/vx regs
@@ -259,11 +296,12 @@ ENTRY(sie64a)
259296
jnz .Lsie_skip
260297
TSTMSK __LC_CPU_FLAGS,_CIF_FPU
261298
jo .Lsie_skip # exit if fp/vx regs changed
262-
BPON
299+
BPEXIT __SF_EMPTY+24(%r15),(_TIF_ISOLATE_BP|_TIF_ISOLATE_BP_GUEST)
263300
.Lsie_entry:
264301
sie 0(%r14)
265302
.Lsie_exit:
266303
BPOFF
304+
BPENTER __SF_EMPTY+24(%r15),(_TIF_ISOLATE_BP|_TIF_ISOLATE_BP_GUEST)
267305
.Lsie_skip:
268306
ni __SIE_PROG0C+3(%r14),0xfe # no longer in SIE
269307
lctlg %c1,%c1,__LC_USER_ASCE # load primary asce
@@ -324,6 +362,7 @@ ENTRY(system_call)
324362
la %r11,STACK_FRAME_OVERHEAD(%r15) # pointer to pt_regs
325363
.Lsysc_vtime:
326364
UPDATE_VTIME %r8,%r9,__LC_SYNC_ENTER_TIMER
365+
BPENTER __TI_flags(%r12),_TIF_ISOLATE_BP
327366
stmg %r0,%r7,__PT_R0(%r11)
328367
# clear user controlled register to prevent speculative use
329368
xgr %r0,%r0
@@ -362,7 +401,7 @@ ENTRY(system_call)
362401
jnz .Lsysc_work # check for work
363402
TSTMSK __LC_CPU_FLAGS,_CIF_WORK
364403
jnz .Lsysc_work
365-
BPON
404+
BPEXIT __TI_flags(%r12),_TIF_ISOLATE_BP
366405
.Lsysc_restore:
367406
lg %r14,__LC_VDSO_PER_CPU
368407
lmg %r0,%r10,__PT_R0(%r11)
@@ -597,6 +636,7 @@ ENTRY(pgm_check_handler)
597636
aghi %r15,-(STACK_FRAME_OVERHEAD + __PT_SIZE)
598637
j 4f
599638
2: UPDATE_VTIME %r14,%r15,__LC_SYNC_ENTER_TIMER
639+
BPENTER __TI_flags(%r12),_TIF_ISOLATE_BP
600640
lg %r15,__LC_KERNEL_STACK
601641
lgr %r14,%r12
602642
aghi %r14,__TASK_thread # pointer to thread_struct
@@ -729,7 +769,7 @@ ENTRY(io_int_handler)
729769
mvc __LC_RETURN_PSW(16),__PT_PSW(%r11)
730770
tm __PT_PSW+1(%r11),0x01 # returning to user ?
731771
jno .Lio_exit_kernel
732-
BPON
772+
BPEXIT __TI_flags(%r12),_TIF_ISOLATE_BP
733773
.Lio_exit_timer:
734774
stpt __LC_EXIT_TIMER
735775
mvc __VDSO_ECTG_BASE(16,%r14),__LC_EXIT_TIMER
@@ -1165,7 +1205,7 @@ ENTRY(mcck_int_handler)
11651205
mvc __LC_RETURN_MCCK_PSW(16),__PT_PSW(%r11) # move return PSW
11661206
tm __LC_RETURN_MCCK_PSW+1,0x01 # returning to user ?
11671207
jno 0f
1168-
BPON
1208+
BPEXIT __TI_flags(%r12),_TIF_ISOLATE_BP
11691209
stpt __LC_EXIT_TIMER
11701210
mvc __VDSO_ECTG_BASE(16,%r14),__LC_EXIT_TIMER
11711211
0: lmg %r11,%r15,__PT_R11(%r11)
@@ -1292,7 +1332,8 @@ cleanup_critical:
12921332
clg %r9,BASED(.Lsie_crit_mcck_length)
12931333
jh 1f
12941334
oi __LC_CPU_FLAGS+7, _CIF_MCCK_GUEST
1295-
1: lg %r9,__SF_EMPTY(%r15) # get control block pointer
1335+
1: BPENTER __SF_EMPTY+24(%r15),(_TIF_ISOLATE_BP|_TIF_ISOLATE_BP_GUEST)
1336+
lg %r9,__SF_EMPTY(%r15) # get control block pointer
12961337
ni __SIE_PROG0C+3(%r9),0xfe # no longer in SIE
12971338
lctlg %c1,%c1,__LC_USER_ASCE # load primary asce
12981339
larl %r9,sie_exit # skip forward to sie_exit

arch/s390/kernel/processor.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,21 @@ const struct seq_operations cpuinfo_op = {
197197
.stop = c_stop,
198198
.show = show_cpuinfo,
199199
};
200+
201+
int s390_isolate_bp(void)
202+
{
203+
if (!test_facility(82))
204+
return -EOPNOTSUPP;
205+
set_thread_flag(TIF_ISOLATE_BP);
206+
return 0;
207+
}
208+
EXPORT_SYMBOL(s390_isolate_bp);
209+
210+
int s390_isolate_bp_guest(void)
211+
{
212+
if (!test_facility(82))
213+
return -EOPNOTSUPP;
214+
set_thread_flag(TIF_ISOLATE_BP_GUEST);
215+
return 0;
216+
}
217+
EXPORT_SYMBOL(s390_isolate_bp_guest);

0 commit comments

Comments
 (0)