1*e094e235Spooka /* $NetBSD: alloc.c,v 1.1 2010/06/14 21:06:09 pooka Exp $ */
2*e094e235Spooka
3*e094e235Spooka /*-
4*e094e235Spooka * Copyright (c) 2010 The NetBSD Foundation, Inc.
5*e094e235Spooka * All rights reserved.
6*e094e235Spooka *
7*e094e235Spooka * Redistribution and use in source and binary forms, with or without
8*e094e235Spooka * modification, are permitted provided that the following conditions
9*e094e235Spooka * are met:
10*e094e235Spooka * 1. Redistributions of source code must retain the above copyright
11*e094e235Spooka * notice, this list of conditions and the following disclaimer.
12*e094e235Spooka * 2. Redistributions in binary form must reproduce the above copyright
13*e094e235Spooka * notice, this list of conditions and the following disclaimer in the
14*e094e235Spooka * documentation and/or other materials provided with the distribution.
15*e094e235Spooka *
16*e094e235Spooka * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17*e094e235Spooka * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18*e094e235Spooka * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19*e094e235Spooka * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20*e094e235Spooka * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21*e094e235Spooka * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*e094e235Spooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23*e094e235Spooka * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*e094e235Spooka * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25*e094e235Spooka * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26*e094e235Spooka * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27*e094e235Spooka * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*e094e235Spooka */
29*e094e235Spooka
30*e094e235Spooka #include <sys/cdefs.h>
31*e094e235Spooka #if !defined(lint)
32*e094e235Spooka __RCSID("$NetBSD: alloc.c,v 1.1 2010/06/14 21:06:09 pooka Exp $");
33*e094e235Spooka #endif /* !lint */
34*e094e235Spooka
35*e094e235Spooka #include <sys/param.h>
36*e094e235Spooka #include <sys/condvar.h>
37*e094e235Spooka #include <sys/kmem.h>
38*e094e235Spooka #include <sys/kthread.h>
39*e094e235Spooka #include <sys/mutex.h>
40*e094e235Spooka #include <sys/pool.h>
41*e094e235Spooka #include <sys/proc.h>
42*e094e235Spooka
43*e094e235Spooka #include <uvm/uvm.h>
44*e094e235Spooka
45*e094e235Spooka #include <rump/rumpuser.h>
46*e094e235Spooka #include "kernspace.h"
47*e094e235Spooka
48*e094e235Spooka static void *store[32];
49*e094e235Spooka static struct pool pp1, pp2;
50*e094e235Spooka
51*e094e235Spooka static kmutex_t mtx;
52*e094e235Spooka static kcondvar_t kcv;
53*e094e235Spooka static int curstat;
54*e094e235Spooka
55*e094e235Spooka static void
hthr(void * arg)56*e094e235Spooka hthr(void *arg)
57*e094e235Spooka {
58*e094e235Spooka int i;
59*e094e235Spooka
60*e094e235Spooka mutex_enter(&mtx);
61*e094e235Spooka curstat++;
62*e094e235Spooka cv_signal(&kcv);
63*e094e235Spooka
64*e094e235Spooka while (curstat < 2)
65*e094e235Spooka cv_wait(&kcv, &mtx);
66*e094e235Spooka mutex_exit(&mtx);
67*e094e235Spooka
68*e094e235Spooka /* try to guarantee that the sleep is triggered in PR_WAITOK */
69*e094e235Spooka while ((kernel_map->flags & VM_MAP_WANTVA) == 0)
70*e094e235Spooka kpause("take5", false, 1, NULL);
71*e094e235Spooka
72*e094e235Spooka for (i = 0; i < __arraycount(store); i++) {
73*e094e235Spooka pool_put(&pp1, store[i]);
74*e094e235Spooka }
75*e094e235Spooka
76*e094e235Spooka kthread_exit(0);
77*e094e235Spooka }
78*e094e235Spooka
79*e094e235Spooka void
rumptest_alloc(size_t thelimit)80*e094e235Spooka rumptest_alloc(size_t thelimit)
81*e094e235Spooka {
82*e094e235Spooka char *c;
83*e094e235Spooka int succ, i;
84*e094e235Spooka
85*e094e235Spooka mutex_init(&mtx, MUTEX_DEFAULT, IPL_NONE);
86*e094e235Spooka cv_init(&kcv, "venailu");
87*e094e235Spooka
88*e094e235Spooka kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL, hthr, NULL, NULL, "h");
89*e094e235Spooka
90*e094e235Spooka pool_init(&pp1, 1024, 0, 0, 0, "vara-allas",
91*e094e235Spooka &pool_allocator_nointr, IPL_NONE);
92*e094e235Spooka pool_init(&pp2, 1024, 0, 0, 0, "allas",
93*e094e235Spooka &pool_allocator_nointr, IPL_NONE);
94*e094e235Spooka
95*e094e235Spooka for (i = 0; i < __arraycount(store); i++) {
96*e094e235Spooka store[i] = pool_get(&pp1, PR_NOWAIT);
97*e094e235Spooka if (store[i] == NULL) {
98*e094e235Spooka panic("pool_get store failed");
99*e094e235Spooka }
100*e094e235Spooka }
101*e094e235Spooka
102*e094e235Spooka /* wait until other thread runs */
103*e094e235Spooka mutex_enter(&mtx);
104*e094e235Spooka while (curstat == 0)
105*e094e235Spooka cv_wait(&kcv, &mtx);
106*e094e235Spooka mutex_exit(&mtx);
107*e094e235Spooka
108*e094e235Spooka for (succ = 0;; succ++) {
109*e094e235Spooka if (succ * 1024 > thelimit)
110*e094e235Spooka panic("managed to allocate over limit");
111*e094e235Spooka if ((c = pool_get(&pp2, PR_NOWAIT)) == NULL) {
112*e094e235Spooka mutex_enter(&mtx);
113*e094e235Spooka curstat++;
114*e094e235Spooka cv_signal(&kcv);
115*e094e235Spooka mutex_exit(&mtx);
116*e094e235Spooka if (pool_get(&pp2, PR_WAITOK) == NULL)
117*e094e235Spooka panic("pool get PR_WAITOK failed");
118*e094e235Spooka break;
119*e094e235Spooka }
120*e094e235Spooka *c = 'a';
121*e094e235Spooka }
122*e094e235Spooka }
123