1*57718be8SEnji Cooper /* $NetBSD: busypage.c,v 1.5 2011/08/07 14:03:15 rmind Exp $ */
2*57718be8SEnji Cooper
3*57718be8SEnji Cooper /*-
4*57718be8SEnji Cooper * Copyright (c) 2010 The NetBSD Foundation, Inc.
5*57718be8SEnji Cooper * All rights reserved.
6*57718be8SEnji Cooper *
7*57718be8SEnji Cooper * Redistribution and use in source and binary forms, with or without
8*57718be8SEnji Cooper * modification, are permitted provided that the following conditions
9*57718be8SEnji Cooper * are met:
10*57718be8SEnji Cooper * 1. Redistributions of source code must retain the above copyright
11*57718be8SEnji Cooper * notice, this list of conditions and the following disclaimer.
12*57718be8SEnji Cooper * 2. Redistributions in binary form must reproduce the above copyright
13*57718be8SEnji Cooper * notice, this list of conditions and the following disclaimer in the
14*57718be8SEnji Cooper * documentation and/or other materials provided with the distribution.
15*57718be8SEnji Cooper *
16*57718be8SEnji Cooper * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17*57718be8SEnji Cooper * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18*57718be8SEnji Cooper * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19*57718be8SEnji Cooper * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20*57718be8SEnji Cooper * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21*57718be8SEnji Cooper * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*57718be8SEnji Cooper * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23*57718be8SEnji Cooper * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*57718be8SEnji Cooper * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25*57718be8SEnji Cooper * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26*57718be8SEnji Cooper * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27*57718be8SEnji Cooper * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*57718be8SEnji Cooper */
29*57718be8SEnji Cooper
30*57718be8SEnji Cooper #include <sys/cdefs.h>
31*57718be8SEnji Cooper #if !defined(lint)
32*57718be8SEnji Cooper __RCSID("$NetBSD: busypage.c,v 1.5 2011/08/07 14:03:15 rmind Exp $");
33*57718be8SEnji Cooper #endif /* !lint */
34*57718be8SEnji Cooper
35*57718be8SEnji Cooper #include <sys/param.h>
36*57718be8SEnji Cooper #include <sys/condvar.h>
37*57718be8SEnji Cooper #include <sys/kthread.h>
38*57718be8SEnji Cooper #include <sys/mutex.h>
39*57718be8SEnji Cooper #include <sys/proc.h>
40*57718be8SEnji Cooper
41*57718be8SEnji Cooper #include <uvm/uvm.h>
42*57718be8SEnji Cooper
43*57718be8SEnji Cooper #include "kernspace.h"
44*57718be8SEnji Cooper
45*57718be8SEnji Cooper static struct uvm_object *uobj;
46*57718be8SEnji Cooper static struct vm_page *testpg;
47*57718be8SEnji Cooper static kcondvar_t tcv;
48*57718be8SEnji Cooper
49*57718be8SEnji Cooper static bool threadrun = false;
50*57718be8SEnji Cooper
51*57718be8SEnji Cooper static void
thread(void * arg)52*57718be8SEnji Cooper thread(void *arg)
53*57718be8SEnji Cooper {
54*57718be8SEnji Cooper
55*57718be8SEnji Cooper mutex_enter(uobj->vmobjlock);
56*57718be8SEnji Cooper threadrun = true;
57*57718be8SEnji Cooper cv_signal(&tcv);
58*57718be8SEnji Cooper testpg->flags |= PG_WANTED;
59*57718be8SEnji Cooper UVM_UNLOCK_AND_WAIT(testpg, uobj->vmobjlock, false, "tw", 0);
60*57718be8SEnji Cooper kthread_exit(0);
61*57718be8SEnji Cooper }
62*57718be8SEnji Cooper
63*57718be8SEnji Cooper void
rumptest_busypage()64*57718be8SEnji Cooper rumptest_busypage()
65*57718be8SEnji Cooper {
66*57718be8SEnji Cooper struct lwp *newl;
67*57718be8SEnji Cooper int rv;
68*57718be8SEnji Cooper
69*57718be8SEnji Cooper cv_init(&tcv, "napina");
70*57718be8SEnji Cooper
71*57718be8SEnji Cooper uobj = uao_create(1, 0);
72*57718be8SEnji Cooper mutex_enter(uobj->vmobjlock);
73*57718be8SEnji Cooper testpg = uvm_pagealloc(uobj, 0, NULL, 0);
74*57718be8SEnji Cooper mutex_exit(uobj->vmobjlock);
75*57718be8SEnji Cooper if (testpg == NULL)
76*57718be8SEnji Cooper panic("couldn't create vm page");
77*57718be8SEnji Cooper
78*57718be8SEnji Cooper rv = kthread_create(PRI_NONE, KTHREAD_MUSTJOIN | KTHREAD_MPSAFE, NULL,
79*57718be8SEnji Cooper thread, NULL, &newl, "jointest");
80*57718be8SEnji Cooper if (rv)
81*57718be8SEnji Cooper panic("thread creation failed: %d", rv);
82*57718be8SEnji Cooper
83*57718be8SEnji Cooper mutex_enter(uobj->vmobjlock);
84*57718be8SEnji Cooper while (!threadrun)
85*57718be8SEnji Cooper cv_wait(&tcv, uobj->vmobjlock);
86*57718be8SEnji Cooper
87*57718be8SEnji Cooper uvm_page_unbusy(&testpg, 1);
88*57718be8SEnji Cooper mutex_exit(uobj->vmobjlock);
89*57718be8SEnji Cooper
90*57718be8SEnji Cooper rv = kthread_join(newl);
91*57718be8SEnji Cooper if (rv)
92*57718be8SEnji Cooper panic("thread join failed: %d", rv);
93*57718be8SEnji Cooper
94*57718be8SEnji Cooper }
95