Path: blob/master/arch/powerpc/mm/book3s32/mmu_context.c
26481 views
// SPDX-License-Identifier: GPL-2.0-or-later1/*2* This file contains the routines for handling the MMU on those3* PowerPC implementations where the MMU substantially follows the4* architecture specification. This includes the 6xx, 7xx, 7xxx,5* and 8260 implementations but excludes the 8xx and 4xx.6* -- paulus7*8* Derived from arch/ppc/mm/init.c:9* Copyright (C) 1995-1996 Gary Thomas ([email protected])10*11* Modifications by Paul Mackerras (PowerMac) ([email protected])12* and Cort Dougan (PReP) ([email protected])13* Copyright (C) 1996 Paul Mackerras14*15* Derived from "arch/i386/mm/init.c"16* Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds17*/1819#include <linux/mm.h>20#include <linux/init.h>21#include <linux/export.h>2223#include <asm/mmu_context.h>2425/*26* Room for two PTE pointers, usually the kernel and current user pointers27* to their respective root page table.28*/29void *abatron_pteptrs[2];3031/*32* On 32-bit PowerPC 6xx/7xx/7xxx CPUs, we use a set of 16 VSIDs33* (virtual segment identifiers) for each context. Although the34* hardware supports 24-bit VSIDs, and thus >1 million contexts,35* we only use 32,768 of them. That is ample, since there can be36* at most around 30,000 tasks in the system anyway, and it means37* that we can use a bitmap to indicate which contexts are in use.38* Using a bitmap means that we entirely avoid all of the problems39* that we used to have when the context number overflowed,40* particularly on SMP systems.41* -- paulus.42*/43#define NO_CONTEXT ((unsigned long) -1)44#define LAST_CONTEXT 3276745#define FIRST_CONTEXT 14647static unsigned long next_mmu_context;48static unsigned long context_map[LAST_CONTEXT / BITS_PER_LONG + 1];4950unsigned long __init_new_context(void)51{52unsigned long ctx = next_mmu_context;5354while (test_and_set_bit(ctx, context_map)) {55ctx = find_next_zero_bit(context_map, LAST_CONTEXT+1, ctx);56if (ctx > LAST_CONTEXT)57ctx = 0;58}59next_mmu_context = (ctx + 1) & LAST_CONTEXT;6061return ctx;62}63EXPORT_SYMBOL_GPL(__init_new_context);6465/*66* Set up the context for a new address space.67*/68int init_new_context(struct task_struct *t, struct mm_struct *mm)69{70mm->context.id = __init_new_context();71mm->context.sr0 = CTX_TO_VSID(mm->context.id, 0);7273if (IS_ENABLED(CONFIG_PPC_KUEP))74mm->context.sr0 |= SR_NX;75if (!kuap_is_disabled())76mm->context.sr0 |= SR_KS;7778return 0;79}8081/*82* Free a context ID. Make sure to call this with preempt disabled!83*/84void __destroy_context(unsigned long ctx)85{86clear_bit(ctx, context_map);87}88EXPORT_SYMBOL_GPL(__destroy_context);8990/*91* We're finished using the context for an address space.92*/93void destroy_context(struct mm_struct *mm)94{95preempt_disable();96if (mm->context.id != NO_CONTEXT) {97__destroy_context(mm->context.id);98mm->context.id = NO_CONTEXT;99}100preempt_enable();101}102103/*104* Initialize the context management stuff.105*/106void __init mmu_context_init(void)107{108/* Reserve context 0 for kernel use */109context_map[0] = (1 << FIRST_CONTEXT) - 1;110next_mmu_context = FIRST_CONTEXT;111}112113void switch_mmu_context(struct mm_struct *prev, struct mm_struct *next, struct task_struct *tsk)114{115long id = next->context.id;116117if (id < 0)118panic("mm_struct %p has no context ID", next);119120isync();121122update_user_segments(next->context.sr0);123124if (IS_ENABLED(CONFIG_BDI_SWITCH))125abatron_pteptrs[1] = next->pgd;126127if (!mmu_has_feature(MMU_FTR_HPTE_TABLE))128mtspr(SPRN_SDR1, rol32(__pa(next->pgd), 4) & 0xffff01ff);129130mb(); /* sync */131isync();132}133EXPORT_SYMBOL(switch_mmu_context);134135136