1*1912643fSad /* $NetBSD: busypage.c,v 1.8 2020/03/17 18:31:39 ad Exp $ */
29a733049Spooka
39a733049Spooka /*-
49a733049Spooka * Copyright (c) 2010 The NetBSD Foundation, Inc.
59a733049Spooka * All rights reserved.
69a733049Spooka *
79a733049Spooka * Redistribution and use in source and binary forms, with or without
89a733049Spooka * modification, are permitted provided that the following conditions
99a733049Spooka * are met:
109a733049Spooka * 1. Redistributions of source code must retain the above copyright
119a733049Spooka * notice, this list of conditions and the following disclaimer.
129a733049Spooka * 2. Redistributions in binary form must reproduce the above copyright
139a733049Spooka * notice, this list of conditions and the following disclaimer in the
149a733049Spooka * documentation and/or other materials provided with the distribution.
159a733049Spooka *
169a733049Spooka * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
179a733049Spooka * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
189a733049Spooka * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
199a733049Spooka * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
209a733049Spooka * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
219a733049Spooka * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
229a733049Spooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
239a733049Spooka * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
249a733049Spooka * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
259a733049Spooka * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
269a733049Spooka * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
279a733049Spooka * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
289a733049Spooka */
299a733049Spooka
309a733049Spooka #include <sys/cdefs.h>
319a733049Spooka #if !defined(lint)
32*1912643fSad __RCSID("$NetBSD: busypage.c,v 1.8 2020/03/17 18:31:39 ad Exp $");
339a733049Spooka #endif /* !lint */
349a733049Spooka
359a733049Spooka #include <sys/param.h>
369a733049Spooka #include <sys/condvar.h>
379a733049Spooka #include <sys/kthread.h>
389a733049Spooka #include <sys/mutex.h>
399a733049Spooka #include <sys/proc.h>
409a733049Spooka
419a733049Spooka #include <uvm/uvm.h>
429a733049Spooka
439a733049Spooka #include "kernspace.h"
449a733049Spooka
459a733049Spooka static struct uvm_object *uobj;
469a733049Spooka static struct vm_page *testpg;
479a733049Spooka static kcondvar_t tcv;
489a733049Spooka
499a733049Spooka static bool threadrun = false;
509a733049Spooka
519a733049Spooka static void
thread(void * arg)529a733049Spooka thread(void *arg)
539a733049Spooka {
549a733049Spooka
5537049e7aSad mutex_enter(&testpg->interlock);
569a733049Spooka threadrun = true;
579a733049Spooka cv_signal(&tcv);
5837049e7aSad mutex_exit(&testpg->interlock);
5937049e7aSad
6037049e7aSad rw_enter(uobj->vmobjlock, RW_READER);
6137049e7aSad uvm_pagewait(testpg, uobj->vmobjlock, "tw");
6237049e7aSad
639a733049Spooka kthread_exit(0);
649a733049Spooka }
659a733049Spooka
669a733049Spooka void
rumptest_busypage()679a733049Spooka rumptest_busypage()
689a733049Spooka {
699a733049Spooka struct lwp *newl;
709a733049Spooka int rv;
719a733049Spooka
729a733049Spooka cv_init(&tcv, "napina");
739a733049Spooka
749a733049Spooka uobj = uao_create(1, 0);
75d2a0ebb6Sad rw_enter(uobj->vmobjlock, RW_WRITER);
769a733049Spooka testpg = uvm_pagealloc(uobj, 0, NULL, 0);
77d2a0ebb6Sad rw_exit(uobj->vmobjlock);
789a733049Spooka if (testpg == NULL)
799a733049Spooka panic("couldn't create vm page");
809a733049Spooka
81a0ffc02aSrmind rv = kthread_create(PRI_NONE, KTHREAD_MUSTJOIN | KTHREAD_MPSAFE, NULL,
829a733049Spooka thread, NULL, &newl, "jointest");
839a733049Spooka if (rv)
849a733049Spooka panic("thread creation failed: %d", rv);
859a733049Spooka
8637049e7aSad kpause("lolgic", false, mstohz(100), NULL);
879a733049Spooka
8837049e7aSad mutex_enter(&testpg->interlock);
8937049e7aSad while (!threadrun)
9037049e7aSad cv_wait(&tcv, &testpg->interlock);
9137049e7aSad mutex_exit(&testpg->interlock);
9237049e7aSad
9337049e7aSad rw_enter(uobj->vmobjlock, RW_WRITER);
94*1912643fSad uvm_page_unbusy(&testpg, 1);
95d2a0ebb6Sad rw_exit(uobj->vmobjlock);
969a733049Spooka
979a733049Spooka rv = kthread_join(newl);
989a733049Spooka if (rv)
999a733049Spooka panic("thread join failed: %d", rv);
1009a733049Spooka
1019a733049Spooka }
102