aboutsummaryrefslogtreecommitdiffstats
path: root/tools/include/getline.h
Commit message (Expand)AuthorAgeFilesLines
* tools: host/include/getline.h: fix FreeBSD 8.0+ supportImre Kaloz2015-05-081-1/+5
* build: BSD compile fixesFelix Fietkau2013-03-071-1/+1
* host/include/getline.h - Mac OS X 10.7 (Lion) fixLars-Peter Clausen2011-08-051-1/+1
* more cygwin related build fixesFlorian Fainelli2009-04-141-1/+7
* finally move buildroot-ng to trunkFelix Fietkau2016-03-201-0/+62
='n69' href='#n69'>69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
/******************************************************************************
 * mm/hypervisor.c
 * 
 * Update page tables via the hypervisor.
 * 
 * Copyright (c) 2002-2004, K A Fraser
 * 
 * This file may be distributed separately from the Linux kernel, or
 * incorporated into other software packages, subject to the following license:
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this source file (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy, modify,
 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

#include <linux/config.h>
#include <linux/sched.h>
#include <linux/mm.h>
#include <linux/vmalloc.h>
#include <asm/page.h>
#include <asm/pgtable.h>
#include <asm-xen/hypervisor.h>
#include <asm-xen/multicall.h>
#include <asm-xen/balloon.h>
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
#include <linux/percpu.h>
#endif

/*
 * This suffices to protect us if we ever move to SMP domains.
 * Further, it protects us against interrupts. At the very least, this is
 * required for the network driver which flushes the update queue before
 * pushing new receive buffers.
 */
static spinlock_t update_lock = SPIN_LOCK_UNLOCKED;

#define QUEUE_SIZE 128
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
#define pte_offset_kernel pte_offset
#define pud_t pgd_t
#define pud_offset(d, va) d
#else
#define pmd_val_ma(v) (v).pud.pgd.pgd;
#endif

DEFINE_PER_CPU(mmu_update_t, update_queue[QUEUE_SIZE]);
DEFINE_PER_CPU(unsigned int, mmu_update_queue_idx);

/*
 * MULTICALL_flush_page_update_queue:
 *   This is a version of the flush which queues as part of a multicall.
 */
void MULTICALL_flush_page_update_queue(void)
{
    int cpu = smp_processor_id();
    int idx;
    unsigned long flags;
    unsigned int _idx;
    spin_lock_irqsave(&update_lock, flags);
    idx = per_cpu(mmu_update_queue_idx, cpu);
    if ( (_idx = idx) != 0 ) 
    {
        per_cpu(mmu_update_queue_idx, cpu) = 0;
        wmb(); /* Make sure index is cleared first to avoid double updates. */
        queue_multicall3(__HYPERVISOR_mmu_update, 
                         (unsigned long)&per_cpu(update_queue[0], cpu), 
                         (unsigned long)_idx, 
                         (unsigned long)NULL);
    }
    spin_unlock_irqrestore(&update_lock, flags);
}

static inline void __flush_page_update_queue(void)
{
    int cpu = smp_processor_id();
    unsigned int _idx = per_cpu(mmu_update_queue_idx, cpu);
    per_cpu(mmu_update_queue_idx, cpu) = 0;
    wmb(); /* Make sure index is cleared first to avoid double updates. */
    if ( unlikely(HYPERVISOR_mmu_update(&per_cpu(update_queue[0], cpu), _idx, NULL) < 0) )
    {
        printk(KERN_ALERT "Failed to execute MMU updates.\n");
        BUG();
    }
}

void _flush_page_update_queue(void)
{
    int cpu = smp_processor_id();
    unsigned long flags;
    spin_lock_irqsave(&update_lock, flags);
    if ( per_cpu(mmu_update_queue_idx, cpu) != 0 ) __flush_page_update_queue();
    spin_unlock_irqrestore(&update_lock, flags);
}

static inline void increment_index(void)
{
    int cpu = smp_processor_id();
    per_cpu(mmu_update_queue_idx, cpu)++;
    if ( unlikely(per_cpu(mmu_update_queue_idx, cpu) == QUEUE_SIZE) ) __flush_page_update_queue();
}

static inline void increment_index_and_flush(void)
{
    int cpu = smp_processor_id();
    per_cpu(mmu_update_queue_idx, cpu)++;
    __flush_page_update_queue();
}

void queue_l1_entry_update(pte_t *ptr, unsigned long val)
{
    int cpu = smp_processor_id();
    int idx;
    unsigned long flags;
    spin_lock_irqsave(&update_lock, flags);
    idx = per_cpu(mmu_update_queue_idx, cpu);
    per_cpu(update_queue[idx], cpu).ptr = virt_to_machine(ptr);
    per_cpu(update_queue[idx], cpu).val = val;
    increment_index();
    spin_unlock_irqrestore(&update_lock, flags);
}

void queue_l2_entry_update(pmd_t *ptr, pmd_t val)
{
    int cpu = smp_processor_id();
    int idx;
    unsigned long flags;
    spin_lock_irqsave(&update_lock, flags);
    idx = per_cpu(mmu_update_queue_idx, cpu);
    per_cpu(update_queue[idx], cpu).ptr = virt_to_machine(ptr);
    per_cpu(update_queue[idx], cpu).val = pmd_val_ma(val);
    increment_index();
    spin_unlock_irqrestore(&update_lock, flags);
}

void queue_pt_switch(unsigned long ptr)
{
    int cpu = smp_processor_id();
    int idx;
    unsigned long flags;
    spin_lock_irqsave(&update_lock, flags);
    idx = per_cpu(mmu_update_queue_idx, cpu);
    per_cpu(update_queue[idx], cpu).ptr  = phys_to_machine(ptr);
    per_cpu(update_queue[idx], cpu).ptr |= MMU_EXTENDED_COMMAND;
    per_cpu(update_queue[idx], cpu).val  = MMUEXT_NEW_BASEPTR;
    increment_index();
    spin_unlock_irqrestore(&update_lock, flags);
}

void queue_tlb_flush(void)
{
    int cpu = smp_processor_id();
    int idx;
    unsigned long flags;
    spin_lock_irqsave(&update_lock, flags);
    idx = per_cpu(mmu_update_queue_idx, cpu);
    per_cpu(update_queue[idx], cpu).ptr  = MMU_EXTENDED_COMMAND;
    per_cpu(update_queue[idx], cpu).val  = MMUEXT_TLB_FLUSH;
    increment_index();
    spin_unlock_irqrestore(&update_lock, flags);
}

void queue_invlpg(unsigned long ptr)
{
    int cpu = smp_processor_id();
    int idx;
    unsigned long flags;
    spin_lock_irqsave(&update_lock, flags);
    idx = per_cpu(mmu_update_queue_idx, cpu);
    per_cpu(update_queue[idx], cpu).ptr  = MMU_EXTENDED_COMMAND;
    per_cpu(update_queue[idx], cpu).ptr |= ptr & PAGE_MASK;
    per_cpu(update_queue[idx], cpu).val  = MMUEXT_INVLPG;
    increment_index();
    spin_unlock_irqrestore(&update_lock, flags);
}

void queue_pgd_pin(unsigned long ptr)
{
    int cpu = smp_processor_id();
    int idx;
    unsigned long flags;
    spin_lock_irqsave(&update_lock, flags);
    idx = per_cpu(mmu_update_queue_idx, cpu);
    per_cpu(update_queue[idx], cpu).ptr  = phys_to_machine(ptr);
    per_cpu(update_queue[idx], cpu).ptr |= MMU_EXTENDED_COMMAND;
    per_cpu(update_queue[idx], cpu).val  = MMUEXT_PIN_L2_TABLE;
    increment_index();
    spin_unlock_irqrestore(&update_lock, flags);
}

void queue_pgd_unpin(unsigned long ptr)
{
    int cpu = smp_processor_id();
    int idx;
    unsigned long flags;
    spin_lock_irqsave(&update_lock, flags);
    idx = per_cpu(mmu_update_queue_idx, cpu);
    per_cpu(update_queue[idx], cpu).ptr  = phys_to_machine(ptr);
    per_cpu(update_queue[idx], cpu).ptr |= MMU_EXTENDED_COMMAND;
    per_cpu(update_queue[idx], cpu).val  = MMUEXT_UNPIN_TABLE;
    increment_index();
    spin_unlock_irqrestore(&update_lock, flags);
}

void queue_pte_pin(unsigned long ptr)
{
    int cpu = smp_processor_id();
    int idx;
    unsigned long flags;
    spin_lock_irqsave(&update_lock, flags);
    idx = per_cpu(mmu_update_queue_idx, cpu);
    per_cpu(update_queue[idx], cpu).ptr  = phys_to_machine(ptr);
    per_cpu(update_queue[idx], cpu).ptr |= MMU_EXTENDED_COMMAND;
    per_cpu(update_queue[idx], cpu).val  = MMUEXT_PIN_L1_TABLE;
    increment_index();
    spin_unlock_irqrestore(&update_lock, flags);
}

void queue_pte_unpin(unsigned long ptr)
{
    int cpu = smp_processor_id();
    int idx;
    unsigned long flags;
    spin_lock_irqsave(&update_lock, flags);
    idx = per_cpu(mmu_update_queue_idx, cpu);
    per_cpu(update_queue[idx], cpu).ptr  = phys_to_machine(ptr);
    per_cpu(update_queue[idx], cpu).ptr |= MMU_EXTENDED_COMMAND;
    per_cpu(update_queue[idx], cpu).val  = MMUEXT_UNPIN_TABLE;
    increment_index();
    spin_unlock_irqrestore(&update_lock, flags);
}

void queue_set_ldt(unsigned long ptr, unsigned long len)
{
    int cpu = smp_processor_id();
    int idx;
    unsigned long flags;
    spin_lock_irqsave(&update_lock, flags);
    idx = per_cpu(mmu_update_queue_idx, cpu);
    per_cpu(update_queue[idx], cpu).ptr  = MMU_EXTENDED_COMMAND | ptr;
    per_cpu(update_queue[idx], cpu).val  = MMUEXT_SET_LDT | (len << MMUEXT_CMD_SHIFT);
    increment_index();
    spin_unlock_irqrestore(&update_lock, flags);
}

void queue_machphys_update(unsigned long mfn, unsigned long pfn)
{
    int cpu = smp_processor_id();
    int idx;
    unsigned long flags;
    spin_lock_irqsave(&update_lock, flags);
    idx = per_cpu(mmu_update_queue_idx, cpu);
    per_cpu(update_queue[idx], cpu).ptr = (mfn << PAGE_SHIFT) | MMU_MACHPHYS_UPDATE;
    per_cpu(update_queue[idx], cpu).val = pfn;
    increment_index();
    spin_unlock_irqrestore(&update_lock, flags);
}

/* queue and flush versions of the above */
void xen_l1_entry_update(pte_t *ptr, unsigned long val)
{
    int cpu = smp_processor_id();
    int idx;
    unsigned long flags;
    spin_lock_irqsave(&update_lock, flags);
    idx = per_cpu(mmu_update_queue_idx, cpu);
    per_cpu(update_queue[idx], cpu).ptr = virt_to_machine(ptr);
    per_cpu(update_queue[idx], cpu).val = val;
    increment_index_and_flush();
    spin_unlock_irqrestore(&update_lock, flags);
}

void xen_l2_entry_update(pmd_t *ptr, pmd_t val)
{
    int cpu = smp_processor_id();
    int idx;
    unsigned long flags;
    spin_lock_irqsave(&update_lock, flags);
    idx = per_cpu(mmu_update_queue_idx, cpu);
    per_cpu(update_queue[idx], cpu).ptr = virt_to_machine(ptr);
    per_cpu(update_queue[idx], cpu).val = pmd_val_ma(val);
    increment_index_and_flush();
    spin_unlock_irqrestore(&update_lock, flags);
}

void xen_pt_switch(unsigned long ptr)
{
    int cpu = smp_processor_id();
    int idx;
    unsigned long flags;
    spin_lock_irqsave(&update_lock, flags);
    idx = per_cpu(mmu_update_queue_idx, cpu);
    per_cpu(update_queue[idx], cpu).ptr  = phys_to_machine(ptr);
    per_cpu(update_queue[idx], cpu).ptr |= MMU_EXTENDED_COMMAND;
    per_cpu(update_queue[idx], cpu).val  = MMUEXT_NEW_BASEPTR;
    increment_index_and_flush();
    spin_unlock_irqrestore(&update_lock, flags);
}

void xen_tlb_flush(void)
{
    int cpu = smp_processor_id();
    int idx;
    unsigned long flags;
    spin_lock_irqsave(&update_lock, flags);
    idx = per_cpu(mmu_update_queue_idx, cpu);
    per_cpu(update_queue[idx], cpu).ptr  = MMU_EXTENDED_COMMAND;
    per_cpu(update_queue[idx], cpu).val  = MMUEXT_TLB_FLUSH;
    increment_index_and_flush();
    spin_unlock_irqrestore(&update_lock, flags);
}

void xen_invlpg(unsigned long ptr)
{
    int cpu = smp_processor_id();
    int idx;
    unsigned long flags;
    spin_lock_irqsave(&update_lock, flags);
    idx = per_cpu(mmu_update_queue_idx, cpu);
    per_cpu(update_queue[idx], cpu).ptr  = MMU_EXTENDED_COMMAND;
    per_cpu(update_queue[idx], cpu).ptr |= ptr & PAGE_MASK;
    per_cpu(update_queue[idx], cpu).val  = MMUEXT_INVLPG;
    increment_index_and_flush();
    spin_unlock_irqrestore(&update_lock, flags);
}

void xen_pgd_pin(unsigned long ptr)
{
    int cpu = smp_processor_id();
    int idx;
    unsigned long flags;
    spin_lock_irqsave(&update_lock, flags);
    idx = per_cpu(mmu_update_queue_idx, cpu);
    per_cpu(update_queue[idx], cpu).ptr  = phys_to_machine(ptr);
    per_cpu(update_queue[idx], cpu).ptr |= MMU_EXTENDED_COMMAND;
    per_cpu(update_queue[idx], cpu).val  = MMUEXT_PIN_L2_TABLE;
    increment_index_and_flush();
    spin_unlock_irqrestore(&update_lock, flags);
}

void xen_pgd_unpin(unsigned long ptr)
{
    int cpu = smp_processor_id();
    int idx;
    unsigned long flags;
    spin_lock_irqsave(&update_lock, flags);
    idx = per_cpu(mmu_update_queue_idx, cpu);
    per_cpu(update_queue[idx], cpu).ptr  = phys_to_machine(ptr);
    per_cpu(update_queue[idx], cpu).ptr |= MMU_EXTENDED_COMMAND;
    per_cpu(update_queue[idx], cpu).val  = MMUEXT_UNPIN_TABLE;
    increment_index_and_flush();
    spin_unlock_irqrestore(&update_lock, flags);
}

void xen_pte_pin(unsigned long ptr)
{
    int cpu = smp_processor_id();
    int idx;
    unsigned long flags;
    spin_lock_irqsave(&update_lock, flags);
    idx = per_cpu(mmu_update_queue_idx, cpu);
    per_cpu(update_queue[idx], cpu).ptr  = phys_to_machine(ptr);
    per_cpu(update_queue[idx], cpu).ptr |= MMU_EXTENDED_COMMAND;
    per_cpu(update_queue[idx], cpu).val  = MMUEXT_PIN_L1_TABLE;
    increment_index_and_flush();
    spin_unlock_irqrestore(&update_lock, flags);
}

void xen_pte_unpin(unsigned long ptr)
{
    int cpu = smp_processor_id();
    int idx;
    unsigned long flags;
    spin_lock_irqsave(&update_lock, flags);
    idx = per_cpu(mmu_update_queue_idx, cpu);
    per_cpu(update_queue[idx], cpu).ptr  = phys_to_machine(ptr);
    per_cpu(update_queue[idx], cpu).ptr |= MMU_EXTENDED_COMMAND;
    per_cpu(update_queue[idx], cpu).val  = MMUEXT_UNPIN_TABLE;
    increment_index_and_flush();
    spin_unlock_irqrestore(&update_lock, flags);
}

void xen_set_ldt(unsigned long ptr, unsigned long len)
{
    int cpu = smp_processor_id();
    int idx;
    unsigned long flags;
    spin_lock_irqsave(&update_lock, flags);
    idx = per_cpu(mmu_update_queue_idx, cpu);
    per_cpu(update_queue[idx], cpu).ptr  = MMU_EXTENDED_COMMAND | ptr;
    per_cpu(update_queue[idx], cpu).val  = MMUEXT_SET_LDT | (len << MMUEXT_CMD_SHIFT);
    increment_index_and_flush();
    spin_unlock_irqrestore(&update_lock, flags);
}

void xen_machphys_update(unsigned long mfn, unsigned long pfn)
{
    int cpu = smp_processor_id();
    int idx;
    unsigned long flags;
    spin_lock_irqsave(&update_lock, flags);
    idx = per_cpu(mmu_update_queue_idx, cpu);
    per_cpu(update_queue[idx], cpu).ptr = (mfn << PAGE_SHIFT) | MMU_MACHPHYS_UPDATE;
    per_cpu(update_queue[idx], cpu).val = pfn;
    increment_index_and_flush();
    spin_unlock_irqrestore(&update_lock, flags);
}

#ifdef CONFIG_XEN_PHYSDEV_ACCESS

unsigned long allocate_empty_lowmem_region(unsigned long pages)
{
    pgd_t         *pgd; 
    pud_t         *pud; 
    pmd_t         *pmd;
    pte_t         *pte;
    unsigned long *pfn_array;
    unsigned long  vstart;
    unsigned long  i;
    unsigned int   order = get_order(pages*PAGE_SIZE);

    vstart = __get_free_pages(GFP_KERNEL, order);
    if ( vstart == 0 )
        return 0UL;

    scrub_pages(vstart, 1 << order);

    pfn_array = vmalloc((1<<order) * sizeof(*pfn_array));
    if ( pfn_array == NULL )
        BUG();

    for ( i = 0; i < (1<<order); i++ )
    {
        pgd = pgd_offset_k(   (vstart + (i*PAGE_SIZE)));
        pud = pud_offset(pgd, (vstart + (i*PAGE_SIZE)));
        pmd = pmd_offset(pud, (vstart + (i*PAGE_SIZE)));
        pte = pte_offset_kernel(pmd, (vstart + (i*PAGE_SIZE))); 
        pfn_array[i] = pte->pte_low >> PAGE_SHIFT;
        queue_l1_entry_update(pte, 0);
        phys_to_machine_mapping[__pa(vstart)>>PAGE_SHIFT] = INVALID_P2M_ENTRY;
    }

    /* Flush updates through and flush the TLB. */
    xen_tlb_flush();

    balloon_put_pages(pfn_array, 1 << order);

    vfree(pfn_array);

    return vstart;
}

#endif /* CONFIG_XEN_PHYSDEV_ACCESS */