1*1f5207b7SJohn Levon /*
2*1f5207b7SJohn Levon * Copyright (C) 2010 Dan Carpenter.
3*1f5207b7SJohn Levon *
4*1f5207b7SJohn Levon * This program is free software; you can redistribute it and/or
5*1f5207b7SJohn Levon * modify it under the terms of the GNU General Public License
6*1f5207b7SJohn Levon * as published by the Free Software Foundation; either version 2
7*1f5207b7SJohn Levon * of the License, or (at your option) any later version.
8*1f5207b7SJohn Levon *
9*1f5207b7SJohn Levon * This program is distributed in the hope that it will be useful,
10*1f5207b7SJohn Levon * but WITHOUT ANY WARRANTY; without even the implied warranty of
11*1f5207b7SJohn Levon * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12*1f5207b7SJohn Levon * GNU General Public License for more details.
13*1f5207b7SJohn Levon *
14*1f5207b7SJohn Levon * You should have received a copy of the GNU General Public License
15*1f5207b7SJohn Levon * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
16*1f5207b7SJohn Levon */
17*1f5207b7SJohn Levon
18*1f5207b7SJohn Levon #include "smatch.h"
19*1f5207b7SJohn Levon
20*1f5207b7SJohn Levon static int my_id;
21*1f5207b7SJohn Levon
22*1f5207b7SJohn Levon /* this is stolen from the kernel but it's totally fair use dude... */
23*1f5207b7SJohn Levon #define __GFP_DMA (0x01u)
24*1f5207b7SJohn Levon #define __GFP_HIGHMEM (0x02u)
25*1f5207b7SJohn Levon #define __GFP_DMA32 (0x04u)
26*1f5207b7SJohn Levon #define __GFP_MOVABLE (0x08u)
27*1f5207b7SJohn Levon #define GFP_ZONEMASK (__GFP_DMA|__GFP_HIGHMEM|__GFP_DMA32|__GFP_MOVABLE)
28*1f5207b7SJohn Levon
match_alloc(const char * fn,struct expression * expr,void * _arg)29*1f5207b7SJohn Levon static void match_alloc(const char *fn, struct expression *expr, void *_arg)
30*1f5207b7SJohn Levon {
31*1f5207b7SJohn Levon int arg_nr = PTR_INT(_arg);
32*1f5207b7SJohn Levon struct expression *arg_expr;
33*1f5207b7SJohn Levon sval_t sval;
34*1f5207b7SJohn Levon
35*1f5207b7SJohn Levon arg_expr = get_argument_from_call_expr(expr->args, arg_nr);
36*1f5207b7SJohn Levon if (!get_value(arg_expr, &sval))
37*1f5207b7SJohn Levon return;
38*1f5207b7SJohn Levon if (sval.uvalue == 0) /* GFP_NOWAIT */
39*1f5207b7SJohn Levon return;
40*1f5207b7SJohn Levon if (!(sval.uvalue & ~GFP_ZONEMASK))
41*1f5207b7SJohn Levon sm_error("no modifiers for allocation.");
42*1f5207b7SJohn Levon }
43*1f5207b7SJohn Levon
check_gfp_dma(int id)44*1f5207b7SJohn Levon void check_gfp_dma(int id)
45*1f5207b7SJohn Levon {
46*1f5207b7SJohn Levon my_id = id;
47*1f5207b7SJohn Levon if (option_project != PROJ_KERNEL)
48*1f5207b7SJohn Levon return;
49*1f5207b7SJohn Levon add_function_hook("kmalloc", &match_alloc, INT_PTR(1));
50*1f5207b7SJohn Levon add_function_hook("kzalloc", &match_alloc, INT_PTR(1));
51*1f5207b7SJohn Levon }
52