2 * Copyright 2010 Tilera Corporation. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation, version 2.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
11 * NON INFRINGEMENT. See the GNU General Public License for
16 #include <linux/proc_fs.h>
17 #include <linux/seq_file.h>
18 #include <linux/rwsem.h>
19 #include <linux/kprobes.h>
20 #include <linux/sched.h>
21 #include <linux/hardirq.h>
22 #include <linux/uaccess.h>
23 #include <linux/smp.h>
24 #include <linux/cdev.h>
25 #include <linux/compat.h>
26 #include <asm/hardwall.h>
27 #include <asm/traps.h>
28 #include <asm/siginfo.h>
29 #include <asm/irq_regs.h>
31 #include <arch/interrupts.h>
32 #include <arch/spr_def.h>
36 * This data structure tracks the rectangle data, etc., associated
37 * one-to-one with a "struct file *" from opening HARDWALL_FILE.
38 * Note that the file's private data points back to this structure.
40 struct hardwall_info
{
41 struct list_head list
; /* "rectangles" list */
42 struct list_head task_head
; /* head of tasks in this hardwall */
43 int ulhc_x
; /* upper left hand corner x coord */
44 int ulhc_y
; /* upper left hand corner y coord */
45 int width
; /* rectangle width */
46 int height
; /* rectangle height */
47 int teardown_in_progress
; /* are we tearing this one down? */
50 /* Currently allocated hardwall rectangles */
51 static LIST_HEAD(rectangles
);
54 * Guard changes to the hardwall data structures.
55 * This could be finer grained (e.g. one lock for the list of hardwall
56 * rectangles, then separate embedded locks for each one's list of tasks),
57 * but there are subtle correctness issues when trying to start with
58 * a task's "hardwall" pointer and lock the correct rectangle's embedded
59 * lock in the presence of a simultaneous deactivation, so it seems
60 * easier to have a single lock, given that none of these data
61 * structures are touched very frequently during normal operation.
63 static DEFINE_SPINLOCK(hardwall_lock
);
65 /* Allow disabling UDN access. */
66 static int udn_disabled
;
67 static int __init
noudn(char *str
)
69 pr_info("User-space UDN access is disabled\n");
73 early_param("noudn", noudn
);
77 * Low-level primitives
80 /* Set a CPU bit if the CPU is online. */
81 #define cpu_online_set(cpu, dst) do { \
82 if (cpu_online(cpu)) \
83 cpumask_set_cpu(cpu, dst); \
87 /* Does the given rectangle contain the given x,y coordinate? */
88 static int contains(struct hardwall_info
*r
, int x
, int y
)
90 return (x
>= r
->ulhc_x
&& x
< r
->ulhc_x
+ r
->width
) &&
91 (y
>= r
->ulhc_y
&& y
< r
->ulhc_y
+ r
->height
);
94 /* Compute the rectangle parameters and validate the cpumask. */
95 static int setup_rectangle(struct hardwall_info
*r
, struct cpumask
*mask
)
97 int x
, y
, cpu
, ulhc
, lrhc
;
99 /* The first cpu is the ULHC, the last the LRHC. */
100 ulhc
= find_first_bit(cpumask_bits(mask
), nr_cpumask_bits
);
101 lrhc
= find_last_bit(cpumask_bits(mask
), nr_cpumask_bits
);
103 /* Compute the rectangle attributes from the cpus. */
104 r
->ulhc_x
= cpu_x(ulhc
);
105 r
->ulhc_y
= cpu_y(ulhc
);
106 r
->width
= cpu_x(lrhc
) - r
->ulhc_x
+ 1;
107 r
->height
= cpu_y(lrhc
) - r
->ulhc_y
+ 1;
109 /* Width and height must be positive */
110 if (r
->width
<= 0 || r
->height
<= 0)
113 /* Confirm that the cpumask is exactly the rectangle. */
114 for (y
= 0, cpu
= 0; y
< smp_height
; ++y
)
115 for (x
= 0; x
< smp_width
; ++x
, ++cpu
)
116 if (cpumask_test_cpu(cpu
, mask
) != contains(r
, x
, y
))
120 * Note that offline cpus can't be drained when this UDN
121 * rectangle eventually closes. We used to detect this
122 * situation and print a warning, but it annoyed users and
123 * they ignored it anyway, so now we just return without a
129 /* Do the two given rectangles overlap on any cpu? */
130 static int overlaps(struct hardwall_info
*a
, struct hardwall_info
*b
)
132 return a
->ulhc_x
+ a
->width
> b
->ulhc_x
&& /* A not to the left */
133 b
->ulhc_x
+ b
->width
> a
->ulhc_x
&& /* B not to the left */
134 a
->ulhc_y
+ a
->height
> b
->ulhc_y
&& /* A not above */
135 b
->ulhc_y
+ b
->height
> a
->ulhc_y
; /* B not above */
140 * Hardware management of hardwall setup, teardown, trapping,
141 * and enabling/disabling PL0 access to the networks.
144 /* Bit field values to mask together for writes to SPR_XDN_DIRECTION_PROTECT */
145 enum direction_protect
{
146 N_PROTECT
= (1 << 0),
147 E_PROTECT
= (1 << 1),
148 S_PROTECT
= (1 << 2),
152 static void enable_firewall_interrupts(void)
154 raw_local_irq_unmask_now(INT_UDN_FIREWALL
);
157 static void disable_firewall_interrupts(void)
159 raw_local_irq_mask_now(INT_UDN_FIREWALL
);
162 /* Set up hardwall on this cpu based on the passed hardwall_info. */
163 static void hardwall_setup_ipi_func(void *info
)
165 struct hardwall_info
*r
= info
;
166 int cpu
= smp_processor_id();
167 int x
= cpu
% smp_width
;
168 int y
= cpu
/ smp_width
;
172 if (x
== r
->ulhc_x
+ r
->width
- 1)
176 if (y
== r
->ulhc_y
+ r
->height
- 1)
179 __insn_mtspr(SPR_UDN_DIRECTION_PROTECT
, bits
);
180 enable_firewall_interrupts();
184 /* Set up all cpus on edge of rectangle to enable/disable hardwall SPRs. */
185 static void hardwall_setup(struct hardwall_info
*r
)
187 int x
, y
, cpu
, delta
;
188 struct cpumask rect_cpus
;
190 cpumask_clear(&rect_cpus
);
192 /* First include the top and bottom edges */
193 cpu
= r
->ulhc_y
* smp_width
+ r
->ulhc_x
;
194 delta
= (r
->height
- 1) * smp_width
;
195 for (x
= 0; x
< r
->width
; ++x
, ++cpu
) {
196 cpu_online_set(cpu
, &rect_cpus
);
197 cpu_online_set(cpu
+ delta
, &rect_cpus
);
200 /* Then the left and right edges */
202 delta
= r
->width
- 1;
203 for (y
= 0; y
< r
->height
; ++y
, cpu
+= smp_width
) {
204 cpu_online_set(cpu
, &rect_cpus
);
205 cpu_online_set(cpu
+ delta
, &rect_cpus
);
208 /* Then tell all the cpus to set up their protection SPR */
209 on_each_cpu_mask(&rect_cpus
, hardwall_setup_ipi_func
, r
, 1);
212 void __kprobes
do_hardwall_trap(struct pt_regs
* regs
, int fault_num
)
214 struct hardwall_info
*rect
;
215 struct task_struct
*p
;
218 int cpu
= smp_processor_id();
222 struct pt_regs
*old_regs
= set_irq_regs(regs
);
225 /* This tile trapped a network access; find the rectangle. */
228 spin_lock_irqsave(&hardwall_lock
, flags
);
229 list_for_each_entry(rect
, &rectangles
, list
) {
230 if (contains(rect
, x
, y
))
235 * It shouldn't be possible not to find this cpu on the
236 * rectangle list, since only cpus in rectangles get hardwalled.
237 * The hardwall is only removed after the UDN is drained.
239 BUG_ON(&rect
->list
== &rectangles
);
242 * If we already started teardown on this hardwall, don't worry;
243 * the abort signal has been sent and we are just waiting for things
246 if (rect
->teardown_in_progress
) {
247 pr_notice("cpu %d: detected hardwall violation %#lx"
248 " while teardown already in progress\n",
249 cpu
, (long) __insn_mfspr(SPR_UDN_DIRECTION_PROTECT
));
254 * Kill off any process that is activated in this rectangle.
255 * We bypass security to deliver the signal, since it must be
256 * one of the activated processes that generated the UDN
257 * message that caused this trap, and all the activated
258 * processes shared a single open file so are pretty tightly
259 * bound together from a security point of view to begin with.
261 rect
->teardown_in_progress
= 1;
262 wmb(); /* Ensure visibility of rectangle before notifying processes. */
263 pr_notice("cpu %d: detected hardwall violation %#lx...\n",
264 cpu
, (long) __insn_mfspr(SPR_UDN_DIRECTION_PROTECT
));
265 info
.si_signo
= SIGILL
;
267 info
.si_code
= ILL_HARDWALL
;
269 list_for_each_entry(p
, &rect
->task_head
, thread
.hardwall_list
) {
270 BUG_ON(p
->thread
.hardwall
!= rect
);
273 pr_notice("hardwall: killing %d\n", p
->pid
);
274 spin_lock(&p
->sighand
->siglock
);
275 __group_send_sig_info(info
.si_signo
, &info
, p
);
276 spin_unlock(&p
->sighand
->siglock
);
279 if (!found_processes
)
280 pr_notice("hardwall: no associated processes!\n");
283 spin_unlock_irqrestore(&hardwall_lock
, flags
);
286 * We have to disable firewall interrupts now, or else when we
287 * return from this handler, we will simply re-interrupt back to
288 * it. However, we can't clear the protection bits, since we
289 * haven't yet drained the network, and that would allow packets
290 * to cross out of the hardwall region.
292 disable_firewall_interrupts();
295 set_irq_regs(old_regs
);
298 /* Allow access from user space to the UDN. */
299 void grant_network_mpls(void)
301 __insn_mtspr(SPR_MPL_UDN_ACCESS_SET_0
, 1);
302 __insn_mtspr(SPR_MPL_UDN_AVAIL_SET_0
, 1);
303 __insn_mtspr(SPR_MPL_UDN_COMPLETE_SET_0
, 1);
304 __insn_mtspr(SPR_MPL_UDN_TIMER_SET_0
, 1);
305 #if !CHIP_HAS_REV1_XDN()
306 __insn_mtspr(SPR_MPL_UDN_REFILL_SET_0
, 1);
307 __insn_mtspr(SPR_MPL_UDN_CA_SET_0
, 1);
311 /* Deny access from user space to the UDN. */
312 void restrict_network_mpls(void)
314 __insn_mtspr(SPR_MPL_UDN_ACCESS_SET_1
, 1);
315 __insn_mtspr(SPR_MPL_UDN_AVAIL_SET_1
, 1);
316 __insn_mtspr(SPR_MPL_UDN_COMPLETE_SET_1
, 1);
317 __insn_mtspr(SPR_MPL_UDN_TIMER_SET_1
, 1);
318 #if !CHIP_HAS_REV1_XDN()
319 __insn_mtspr(SPR_MPL_UDN_REFILL_SET_1
, 1);
320 __insn_mtspr(SPR_MPL_UDN_CA_SET_1
, 1);
326 * Code to create, activate, deactivate, and destroy hardwall rectangles.
329 /* Create a hardwall for the given rectangle */
330 static struct hardwall_info
*hardwall_create(
331 size_t size
, const unsigned char __user
*bits
)
333 struct hardwall_info
*iter
, *rect
;
338 /* Reject crazy sizes out of hand, a la sys_mbind(). */
339 if (size
> PAGE_SIZE
)
340 return ERR_PTR(-EINVAL
);
342 /* Copy whatever fits into a cpumask. */
343 if (copy_from_user(&mask
, bits
, min(sizeof(struct cpumask
), size
)))
344 return ERR_PTR(-EFAULT
);
347 * If the size was short, clear the rest of the mask;
348 * otherwise validate that the rest of the user mask was zero
349 * (we don't try hard to be efficient when validating huge masks).
351 if (size
< sizeof(struct cpumask
)) {
352 memset((char *)&mask
+ size
, 0, sizeof(struct cpumask
) - size
);
353 } else if (size
> sizeof(struct cpumask
)) {
355 for (i
= sizeof(struct cpumask
); i
< size
; ++i
) {
357 if (get_user(c
, &bits
[i
]))
358 return ERR_PTR(-EFAULT
);
360 return ERR_PTR(-EINVAL
);
364 /* Allocate a new rectangle optimistically. */
365 rect
= kmalloc(sizeof(struct hardwall_info
),
366 GFP_KERNEL
| __GFP_ZERO
);
367 INIT_LIST_HEAD(&rect
->task_head
);
369 /* Compute the rectangle size and validate that it's plausible. */
370 rc
= setup_rectangle(rect
, &mask
);
376 /* Confirm it doesn't overlap and add it to the list. */
377 spin_lock_irqsave(&hardwall_lock
, flags
);
378 list_for_each_entry(iter
, &rectangles
, list
) {
379 if (overlaps(iter
, rect
)) {
380 spin_unlock_irqrestore(&hardwall_lock
, flags
);
382 return ERR_PTR(-EBUSY
);
385 list_add_tail(&rect
->list
, &rectangles
);
386 spin_unlock_irqrestore(&hardwall_lock
, flags
);
388 /* Set up appropriate hardwalling on all affected cpus. */
389 hardwall_setup(rect
);
394 /* Activate a given hardwall on this cpu for this process. */
395 static int hardwall_activate(struct hardwall_info
*rect
)
399 struct task_struct
*p
= current
;
400 struct thread_struct
*ts
= &p
->thread
;
402 /* Require a rectangle. */
406 /* Not allowed to activate a rectangle that is being torn down. */
407 if (rect
->teardown_in_progress
)
411 * Get our affinity; if we're not bound to this tile uniquely,
412 * we can't access the network registers.
414 if (cpumask_weight(&p
->cpus_allowed
) != 1)
417 /* Make sure we are bound to a cpu in this rectangle. */
418 cpu
= smp_processor_id();
419 BUG_ON(cpumask_first(&p
->cpus_allowed
) != cpu
);
422 if (!contains(rect
, x
, y
))
425 /* If we are already bound to this hardwall, it's a no-op. */
427 BUG_ON(ts
->hardwall
!= rect
);
431 /* Success! This process gets to use the user networks on this cpu. */
433 spin_lock_irqsave(&hardwall_lock
, flags
);
434 list_add(&ts
->hardwall_list
, &rect
->task_head
);
435 spin_unlock_irqrestore(&hardwall_lock
, flags
);
436 grant_network_mpls();
437 printk(KERN_DEBUG
"Pid %d (%s) activated for hardwall: cpu %d\n",
438 p
->pid
, p
->comm
, cpu
);
443 * Deactivate a task's hardwall. Must hold hardwall_lock.
444 * This method may be called from free_task(), so we don't want to
445 * rely on too many fields of struct task_struct still being valid.
446 * We assume the cpus_allowed, pid, and comm fields are still valid.
448 static void _hardwall_deactivate(struct task_struct
*task
)
450 struct thread_struct
*ts
= &task
->thread
;
452 if (cpumask_weight(&task
->cpus_allowed
) != 1) {
453 pr_err("pid %d (%s) releasing networks with"
454 " an affinity mask containing %d cpus!\n",
455 task
->pid
, task
->comm
,
456 cpumask_weight(&task
->cpus_allowed
));
460 BUG_ON(ts
->hardwall
== NULL
);
462 list_del(&ts
->hardwall_list
);
464 restrict_network_mpls();
467 /* Deactivate a task's hardwall. */
468 int hardwall_deactivate(struct task_struct
*task
)
473 spin_lock_irqsave(&hardwall_lock
, flags
);
474 activated
= (task
->thread
.hardwall
!= NULL
);
476 _hardwall_deactivate(task
);
477 spin_unlock_irqrestore(&hardwall_lock
, flags
);
482 printk(KERN_DEBUG
"Pid %d (%s) deactivated for hardwall: cpu %d\n",
483 task
->pid
, task
->comm
, smp_processor_id());
487 /* Stop a UDN switch before draining the network. */
488 static void stop_udn_switch(void *ignored
)
490 #if !CHIP_HAS_REV1_XDN()
491 /* Freeze the switch and the demux. */
492 __insn_mtspr(SPR_UDN_SP_FREEZE
,
493 SPR_UDN_SP_FREEZE__SP_FRZ_MASK
|
494 SPR_UDN_SP_FREEZE__DEMUX_FRZ_MASK
|
495 SPR_UDN_SP_FREEZE__NON_DEST_EXT_MASK
);
499 /* Drain all the state from a stopped switch. */
500 static void drain_udn_switch(void *ignored
)
502 #if !CHIP_HAS_REV1_XDN()
504 int from_tile_words
, ca_count
;
506 /* Empty out the 5 switch point fifos. */
507 for (i
= 0; i
< 5; i
++) {
509 __insn_mtspr(SPR_UDN_SP_FIFO_SEL
, i
);
510 words
= __insn_mfspr(SPR_UDN_SP_STATE
) & 0xF;
511 for (j
= 0; j
< words
; j
++)
512 (void) __insn_mfspr(SPR_UDN_SP_FIFO_DATA
);
513 BUG_ON((__insn_mfspr(SPR_UDN_SP_STATE
) & 0xF) != 0);
516 /* Dump out the 3 word fifo at top. */
517 from_tile_words
= (__insn_mfspr(SPR_UDN_DEMUX_STATUS
) >> 10) & 0x3;
518 for (i
= 0; i
< from_tile_words
; i
++)
519 (void) __insn_mfspr(SPR_UDN_DEMUX_WRITE_FIFO
);
521 /* Empty out demuxes. */
522 while (__insn_mfspr(SPR_UDN_DATA_AVAIL
) & (1 << 0))
523 (void) __tile_udn0_receive();
524 while (__insn_mfspr(SPR_UDN_DATA_AVAIL
) & (1 << 1))
525 (void) __tile_udn1_receive();
526 while (__insn_mfspr(SPR_UDN_DATA_AVAIL
) & (1 << 2))
527 (void) __tile_udn2_receive();
528 while (__insn_mfspr(SPR_UDN_DATA_AVAIL
) & (1 << 3))
529 (void) __tile_udn3_receive();
530 BUG_ON((__insn_mfspr(SPR_UDN_DATA_AVAIL
) & 0xF) != 0);
532 /* Empty out catch all. */
533 ca_count
= __insn_mfspr(SPR_UDN_DEMUX_CA_COUNT
);
534 for (i
= 0; i
< ca_count
; i
++)
535 (void) __insn_mfspr(SPR_UDN_CA_DATA
);
536 BUG_ON(__insn_mfspr(SPR_UDN_DEMUX_CA_COUNT
) != 0);
538 /* Clear demux logic. */
539 __insn_mtspr(SPR_UDN_DEMUX_CTL
, 1);
542 * Write switch state; experimentation indicates that 0xc3000
543 * is an idle switch point.
545 for (i
= 0; i
< 5; i
++) {
546 __insn_mtspr(SPR_UDN_SP_FIFO_SEL
, i
);
547 __insn_mtspr(SPR_UDN_SP_STATE
, 0xc3000);
552 /* Reset random UDN state registers at boot up and during hardwall teardown. */
553 void reset_network_state(void)
555 #if !CHIP_HAS_REV1_XDN()
556 /* Reset UDN coordinates to their standard value */
557 unsigned int cpu
= smp_processor_id();
558 unsigned int x
= cpu
% smp_width
;
559 unsigned int y
= cpu
/ smp_width
;
565 #if !CHIP_HAS_REV1_XDN()
566 __insn_mtspr(SPR_UDN_TILE_COORD
, (x
<< 18) | (y
<< 7));
568 /* Set demux tags to predefined values and enable them. */
569 __insn_mtspr(SPR_UDN_TAG_VALID
, 0xf);
570 __insn_mtspr(SPR_UDN_TAG_0
, (1 << 0));
571 __insn_mtspr(SPR_UDN_TAG_1
, (1 << 1));
572 __insn_mtspr(SPR_UDN_TAG_2
, (1 << 2));
573 __insn_mtspr(SPR_UDN_TAG_3
, (1 << 3));
576 /* Clear out other random registers so we have a clean slate. */
577 __insn_mtspr(SPR_UDN_AVAIL_EN
, 0);
578 __insn_mtspr(SPR_UDN_DEADLOCK_TIMEOUT
, 0);
579 #if !CHIP_HAS_REV1_XDN()
580 __insn_mtspr(SPR_UDN_REFILL_EN
, 0);
581 __insn_mtspr(SPR_UDN_DEMUX_QUEUE_SEL
, 0);
582 __insn_mtspr(SPR_UDN_SP_FIFO_SEL
, 0);
585 /* Start the switch and demux. */
586 #if !CHIP_HAS_REV1_XDN()
587 __insn_mtspr(SPR_UDN_SP_FREEZE
, 0);
591 /* Restart a UDN switch after draining. */
592 static void restart_udn_switch(void *ignored
)
594 reset_network_state();
596 /* Disable firewall interrupts. */
597 __insn_mtspr(SPR_UDN_DIRECTION_PROTECT
, 0);
598 disable_firewall_interrupts();
601 /* Build a struct cpumask containing all valid tiles in bounding rectangle. */
602 static void fill_mask(struct hardwall_info
*r
, struct cpumask
*result
)
606 cpumask_clear(result
);
608 cpu
= r
->ulhc_y
* smp_width
+ r
->ulhc_x
;
609 for (y
= 0; y
< r
->height
; ++y
, cpu
+= smp_width
- r
->width
) {
610 for (x
= 0; x
< r
->width
; ++x
, ++cpu
)
611 cpu_online_set(cpu
, result
);
615 /* Last reference to a hardwall is gone, so clear the network. */
616 static void hardwall_destroy(struct hardwall_info
*rect
)
618 struct task_struct
*task
;
622 /* Make sure this file actually represents a rectangle. */
627 * Deactivate any remaining tasks. It's possible to race with
628 * some other thread that is exiting and hasn't yet called
629 * deactivate (when freeing its thread_info), so we carefully
630 * deactivate any remaining tasks before freeing the
631 * hardwall_info object itself.
633 spin_lock_irqsave(&hardwall_lock
, flags
);
634 list_for_each_entry(task
, &rect
->task_head
, thread
.hardwall_list
)
635 _hardwall_deactivate(task
);
636 spin_unlock_irqrestore(&hardwall_lock
, flags
);
639 printk(KERN_DEBUG
"Clearing hardwall rectangle %dx%d %d,%d\n",
640 rect
->width
, rect
->height
, rect
->ulhc_x
, rect
->ulhc_y
);
641 fill_mask(rect
, &mask
);
642 on_each_cpu_mask(&mask
, stop_udn_switch
, NULL
, 1);
643 on_each_cpu_mask(&mask
, drain_udn_switch
, NULL
, 1);
645 /* Restart switch and disable firewall. */
646 on_each_cpu_mask(&mask
, restart_udn_switch
, NULL
, 1);
648 /* Now free the rectangle from the list. */
649 spin_lock_irqsave(&hardwall_lock
, flags
);
650 BUG_ON(!list_empty(&rect
->task_head
));
651 list_del(&rect
->list
);
652 spin_unlock_irqrestore(&hardwall_lock
, flags
);
658 * Dump hardwall state via /proc; initialized in arch/tile/sys/proc.c.
660 int proc_tile_hardwall_show(struct seq_file
*sf
, void *v
)
662 struct hardwall_info
*r
;
665 seq_printf(sf
, "%dx%d 0,0 pids:\n", smp_width
, smp_height
);
669 spin_lock_irq(&hardwall_lock
);
670 list_for_each_entry(r
, &rectangles
, list
) {
671 struct task_struct
*p
;
672 seq_printf(sf
, "%dx%d %d,%d pids:",
673 r
->width
, r
->height
, r
->ulhc_x
, r
->ulhc_y
);
674 list_for_each_entry(p
, &r
->task_head
, thread
.hardwall_list
) {
675 unsigned int cpu
= cpumask_first(&p
->cpus_allowed
);
676 unsigned int x
= cpu
% smp_width
;
677 unsigned int y
= cpu
/ smp_width
;
678 seq_printf(sf
, " %d@%d,%d", p
->pid
, x
, y
);
680 seq_printf(sf
, "\n");
682 spin_unlock_irq(&hardwall_lock
);
688 * Character device support via ioctl/close.
691 static long hardwall_ioctl(struct file
*file
, unsigned int a
, unsigned long b
)
693 struct hardwall_info
*rect
= file
->private_data
;
695 if (_IOC_TYPE(a
) != HARDWALL_IOCTL_BASE
)
698 switch (_IOC_NR(a
)) {
699 case _HARDWALL_CREATE
:
704 rect
= hardwall_create(_IOC_SIZE(a
),
705 (const unsigned char __user
*)b
);
707 return PTR_ERR(rect
);
708 file
->private_data
= rect
;
711 case _HARDWALL_ACTIVATE
:
712 return hardwall_activate(rect
);
714 case _HARDWALL_DEACTIVATE
:
715 if (current
->thread
.hardwall
!= rect
)
717 return hardwall_deactivate(current
);
725 static long hardwall_compat_ioctl(struct file
*file
,
726 unsigned int a
, unsigned long b
)
728 /* Sign-extend the argument so it can be used as a pointer. */
729 return hardwall_ioctl(file
, a
, (unsigned long)compat_ptr(b
));
733 /* The user process closed the file; revoke access to user networks. */
734 static int hardwall_flush(struct file
*file
, fl_owner_t owner
)
736 struct hardwall_info
*rect
= file
->private_data
;
737 struct task_struct
*task
, *tmp
;
742 * NOTE: if multiple threads are activated on this hardwall
743 * file, the other threads will continue having access to the
744 * UDN until they are context-switched out and back in again.
746 * NOTE: A NULL files pointer means the task is being torn
747 * down, so in that case we also deactivate it.
749 spin_lock_irqsave(&hardwall_lock
, flags
);
750 list_for_each_entry_safe(task
, tmp
, &rect
->task_head
,
751 thread
.hardwall_list
) {
752 if (task
->files
== owner
|| task
->files
== NULL
)
753 _hardwall_deactivate(task
);
755 spin_unlock_irqrestore(&hardwall_lock
, flags
);
761 /* This hardwall is gone, so destroy it. */
762 static int hardwall_release(struct inode
*inode
, struct file
*file
)
764 hardwall_destroy(file
->private_data
);
768 static const struct file_operations dev_hardwall_fops
= {
769 .unlocked_ioctl
= hardwall_ioctl
,
771 .compat_ioctl
= hardwall_compat_ioctl
,
773 .flush
= hardwall_flush
,
774 .release
= hardwall_release
,
777 static struct cdev hardwall_dev
;
779 static int __init
dev_hardwall_init(void)
784 rc
= alloc_chrdev_region(&dev
, 0, 1, "hardwall");
787 cdev_init(&hardwall_dev
, &dev_hardwall_fops
);
788 rc
= cdev_add(&hardwall_dev
, dev
, 1);
794 late_initcall(dev_hardwall_init
);