andi.   r10,r12,_PAGE_USER              /* User page ? */
        beq     1f                              /* nope, leave U bits empty */
        rlwimi  r11,r11,3,26,28                 /* yes, copy S bits to U */
+#ifdef CONFIG_PPC_KUEP
+0:     rlwinm  r11,r11,0,~PPC44x_TLB_SX        /* Clear SX if User page */
+       patch_site 0b, patch__tlb_44x_kuep
+#endif
 1:     tlbwe   r11,r13,PPC44x_TLB_ATTRIB       /* Write ATTRIB */
 
        /* Done...restore registers and get out of here.
        andi.   r10,r12,_PAGE_USER              /* User page ? */
        beq     1f                              /* nope, leave U bits empty */
        rlwimi  r11,r11,3,26,28                 /* yes, copy S bits to U */
+#ifdef CONFIG_PPC_KUEP
+0:     rlwinm  r11,r11,0,~PPC47x_TLB2_SX       /* Clear SX if User page */
+       patch_site 0b, patch__tlb_47x_kuep
+#endif
 1:     tlbwe   r11,r13,2
 
        /* Done...restore registers and get out of here.
 
        }
 }
 #endif /* CONFIG_SMP */
+
+#ifdef CONFIG_PPC_KUEP
+void __init setup_kuep(bool disabled)
+{
+       if (disabled)
+               patch_instruction_site(&patch__tlb_44x_kuep, ppc_inst(PPC_RAW_NOP()));
+       else
+               pr_info("Activating Kernel Userspace Execution Prevention\n");
+
+       if (IS_ENABLED(CONFIG_PPC_47x) && disabled)
+               patch_instruction_site(&patch__tlb_47x_kuep, ppc_inst(PPC_RAW_NOP()));
+}
+#endif