11f5207bJohn Levon/*
21f5207bJohn Levon * Copyright (C) 2015 Oracle.
31f5207bJohn Levon *
41f5207bJohn Levon * This program is free software; you can redistribute it and/or
51f5207bJohn Levon * modify it under the terms of the GNU General Public License
61f5207bJohn Levon * as published by the Free Software Foundation; either version 2
71f5207bJohn Levon * of the License, or (at your option) any later version.
81f5207bJohn Levon *
91f5207bJohn Levon * This program is distributed in the hope that it will be useful,
101f5207bJohn Levon * but WITHOUT ANY WARRANTY; without even the implied warranty of
121f5207bJohn Levon * GNU General Public License for more details.
131f5207bJohn Levon *
141f5207bJohn Levon * You should have received a copy of the GNU General Public License
151f5207bJohn Levon * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
161f5207bJohn Levon */
171f5207bJohn Levon
181f5207bJohn Levon#include "smatch.h"
191f5207bJohn Levon#include "smatch_slist.h"
201f5207bJohn Levon#include "smatch_extra.h"
211f5207bJohn Levon
221f5207bJohn Levonstatic int my_id;
231f5207bJohn Levon
241f5207bJohn LevonSTATE(positive);
251f5207bJohn LevonSTATE(ok);
261f5207bJohn Levon
271f5207bJohn Levonstatic void ok_to_use(struct sm_state *sm, struct expression *mod_expr)
281f5207bJohn Levon{
291f5207bJohn Levon	if (sm->state != &ok)
301f5207bJohn Levon		set_state(my_id, sm->name, sm->sym, &ok);
311f5207bJohn Levon}
321f5207bJohn Levon
331f5207bJohn Levonstatic void match_assign(const char *fn, struct expression *expr, void *unused)
341f5207bJohn Levon{
35efe51d0John Levon	struct range_list *rl;
36efe51d0John Levon
37efe51d0John Levon	if (!get_implied_rl(expr->right, &rl))
38efe51d0John Levon		return;
39efe51d0John Levon	if (rl_max(rl).value != 1)
40efe51d0John Levon		return;
411f5207bJohn Levon	set_state_expr(my_id, expr->left, &positive);
421f5207bJohn Levon}
431f5207bJohn Levon
441f5207bJohn Levonstatic void match_condition(struct expression *expr)
451f5207bJohn Levon{
461f5207bJohn Levon	if (!get_state_expr(my_id, expr))
471f5207bJohn Levon		return;
481f5207bJohn Levon	/* If the variable is zero that's ok */
491f5207bJohn Levon	set_true_false_states_expr(my_id, expr, NULL, &ok);
501f5207bJohn Levon}
511f5207bJohn Levon
521f5207bJohn Levonstatic void match_return(struct expression *ret_value)
531f5207bJohn Levon{
541f5207bJohn Levon	struct smatch_state *state;
551f5207bJohn Levon	struct sm_state *sm;
561f5207bJohn Levon	sval_t min;
571f5207bJohn Levon
581f5207bJohn Levon	sm = get_sm_state_expr(my_id, ret_value);
591f5207bJohn Levon	if (!sm)
601f5207bJohn Levon		return;
611f5207bJohn Levon	if (!slist_has_state(sm->possible, &positive))
621f5207bJohn Levon		return;
631f5207bJohn Levon	state = get_state_expr(SMATCH_EXTRA, ret_value);
641f5207bJohn Levon	if (!state)
651f5207bJohn Levon		return;
661f5207bJohn Levon	if (!get_absolute_min(ret_value, &min))
671f5207bJohn Levon		return;
681f5207bJohn Levon	if (min.value == 0)
691f5207bJohn Levon		return;
701f5207bJohn Levon	sm_warning("dma_mapping_error() doesn't return an error code");
711f5207bJohn Levon}
721f5207bJohn Levon
731f5207bJohn Levonvoid check_dma_mapping_error(int id)
741f5207bJohn Levon{
751f5207bJohn Levon	if (option_project != PROJ_KERNEL)
761f5207bJohn Levon		return;
771f5207bJohn Levon
781f5207bJohn Levon	my_id = id;
791f5207bJohn Levon	add_function_assign_hook("dma_mapping_error", &match_assign, NULL);
801f5207bJohn Levon	add_function_assign_hook("pci_dma_mapping_error", &match_assign, NULL);
811f5207bJohn Levon	add_hook(&match_condition, CONDITION_HOOK);
821f5207bJohn Levon	add_hook(&match_return, RETURN_HOOK);
831f5207bJohn Levon	add_modification_hook(my_id, &ok_to_use);
841f5207bJohn Levon}