1*1f5207b7SJohn Levon /*
2*1f5207b7SJohn Levon  * Copyright (C) 2009 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 
match_dma_func(const char * fn,struct expression * expr,void * param)22*1f5207b7SJohn Levon static void match_dma_func(const char *fn, struct expression *expr, void *param)
23*1f5207b7SJohn Levon {
24*1f5207b7SJohn Levon 	struct expression *arg;
25*1f5207b7SJohn Levon 	struct symbol *sym;
26*1f5207b7SJohn Levon 	char *name;
27*1f5207b7SJohn Levon 
28*1f5207b7SJohn Levon 	arg = get_argument_from_call_expr(expr->args, PTR_INT(param));
29*1f5207b7SJohn Levon 	arg = strip_expr(arg);
30*1f5207b7SJohn Levon 	if (!arg)
31*1f5207b7SJohn Levon 		return;
32*1f5207b7SJohn Levon 	if (arg->type == EXPR_PREOP && arg->op == '&') {
33*1f5207b7SJohn Levon 		if (arg->unop->type != EXPR_SYMBOL)
34*1f5207b7SJohn Levon 			return;
35*1f5207b7SJohn Levon 		name = expr_to_str(arg);
36*1f5207b7SJohn Levon 		sm_error("doing dma on the stack (%s)", name);
37*1f5207b7SJohn Levon 		free_string(name);
38*1f5207b7SJohn Levon 		return;
39*1f5207b7SJohn Levon 	}
40*1f5207b7SJohn Levon 	if (arg->type != EXPR_SYMBOL)
41*1f5207b7SJohn Levon 		return;
42*1f5207b7SJohn Levon 	sym = get_type(arg);
43*1f5207b7SJohn Levon 	if (!sym || sym->type != SYM_ARRAY)
44*1f5207b7SJohn Levon 		return;
45*1f5207b7SJohn Levon 	if (get_param_num(arg) >= 0)
46*1f5207b7SJohn Levon 		return;
47*1f5207b7SJohn Levon 	name = expr_to_var(arg);
48*1f5207b7SJohn Levon 	sm_error("doing dma on the stack (%s)", name);
49*1f5207b7SJohn Levon 	free_string(name);
50*1f5207b7SJohn Levon }
51*1f5207b7SJohn Levon 
register_funcs_from_file(void)52*1f5207b7SJohn Levon static void register_funcs_from_file(void)
53*1f5207b7SJohn Levon {
54*1f5207b7SJohn Levon 	struct token *token;
55*1f5207b7SJohn Levon 	const char *func;
56*1f5207b7SJohn Levon 	int arg;
57*1f5207b7SJohn Levon 
58*1f5207b7SJohn Levon 	token = get_tokens_file("kernel.dma_funcs");
59*1f5207b7SJohn Levon 	if (!token)
60*1f5207b7SJohn Levon 		return;
61*1f5207b7SJohn Levon 	if (token_type(token) != TOKEN_STREAMBEGIN)
62*1f5207b7SJohn Levon 		return;
63*1f5207b7SJohn Levon 	token = token->next;
64*1f5207b7SJohn Levon 	while (token_type(token) != TOKEN_STREAMEND) {
65*1f5207b7SJohn Levon 		if (token_type(token) != TOKEN_IDENT)
66*1f5207b7SJohn Levon 			return;
67*1f5207b7SJohn Levon 		func = show_ident(token->ident);
68*1f5207b7SJohn Levon 		token = token->next;
69*1f5207b7SJohn Levon 		if (token_type(token) != TOKEN_NUMBER)
70*1f5207b7SJohn Levon 			return;
71*1f5207b7SJohn Levon 		arg = atoi(token->number);
72*1f5207b7SJohn Levon 		add_function_hook(func, &match_dma_func, INT_PTR(arg));
73*1f5207b7SJohn Levon 		token = token->next;
74*1f5207b7SJohn Levon 	}
75*1f5207b7SJohn Levon 	clear_token_alloc();
76*1f5207b7SJohn Levon }
77*1f5207b7SJohn Levon 
check_dma_on_stack(int id)78*1f5207b7SJohn Levon void check_dma_on_stack(int id)
79*1f5207b7SJohn Levon {
80*1f5207b7SJohn Levon 	if (option_project != PROJ_KERNEL)
81*1f5207b7SJohn Levon 		return;
82*1f5207b7SJohn Levon 	my_id = id;
83*1f5207b7SJohn Levon 	register_funcs_from_file();
84*1f5207b7SJohn Levon }
85